createCheckoutSession
Creates a Stripe Checkout session for a team to subscribe to a pricing plan. This function automatically handles user authentication, redirects unauthenticated users to sign-up, and creates a subscription checkout session with a 14-day trial period.Function Signature
Parameters
The team object for which to create the checkout session. If
null, the user will be redirected to sign-up.The Team type includes:id: numbername: stringstripeCustomerId: string | nullstripeSubscriptionId: string | nullstripeProductId: string | nullplanName: string | nullsubscriptionStatus: string | null
The Stripe Price ID to subscribe to. This should be a valid Stripe price identifier (e.g.,
price_1234567890).Return Value
This function never returns normally as it always callsredirect(). It either:
- Redirects unauthenticated users to
/sign-up?redirect=checkout&priceId={priceId} - Redirects authenticated users to the Stripe Checkout session URL
Behavior
- Authentication Check: Retrieves the current user using
getUser() - Validation: If no team or user exists, redirects to sign-up with return parameters
- Session Creation: Creates a Stripe Checkout session with:
- Payment method: Card only
- Mode: Subscription
- Trial period: 14 days
- Promotion codes: Enabled
- Success URL:
/api/stripe/checkout?session_id={CHECKOUT_SESSION_ID} - Cancel URL:
/pricing
- Customer Association: Links to existing Stripe customer if
team.stripeCustomerIdexists - Redirect: Sends user to Stripe Checkout
Usage Example
Server Action Example
Checkout Session Configuration
The function creates a Stripe Checkout session with the following configuration:Set to
['card'] - only card payments are acceptedSet to
'subscription' for recurring billingSet to
14 - provides a 14-day free trial for all subscriptionsSet to
true - allows customers to enter discount codes at checkoutSet to the user’s ID for tracking the subscription origin
Environment Variables Required
STRIPE_SECRET_KEY: Your Stripe secret API keyBASE_URL: Your application’s base URL for success/cancel redirects
Error Handling
This function handles errors through redirects:- Missing team or user: Redirects to sign-up page
- Stripe API errors: Will throw and should be caught by error boundaries
checkoutAction
A pre-built server action that wrapscreateCheckoutSession() with team middleware protection. This is the recommended way to use checkout in forms.
Function Signature
/lib/payments/actions.ts:7-10
Parameters
Form data containing the
priceId fieldUsage Example
How It Works
ThecheckoutAction uses the withTeam middleware wrapper, which:
- Automatically retrieves the current user’s team
- Validates team membership
- Passes the team to
createCheckoutSession() - Handles errors if user is not authenticated or not part of a team
This action is protected by the
withTeam middleware, ensuring only authenticated users with team membership can initiate checkout.Related Functions
createCustomerPortalSession()- Manage existing subscriptionshandleSubscriptionChange()- Process webhook eventsgetStripePrices()- Fetch available pricing planscustomerPortalAction()- Server action for customer portal access