createCustomerPortalSession
Creates a Stripe Customer Portal session that allows customers to manage their subscription, update payment methods, view invoices, and cancel their subscription. The portal is automatically configured with the team’s current product and pricing options.Function Signature
Parameters
The team object for which to create the customer portal session. Must have both
stripeCustomerId and stripeProductId set.The Team type includes:id: numbername: stringstripeCustomerId: string | null (required for this function)stripeSubscriptionId: string | nullstripeProductId: string | null (required for this function)planName: string | nullsubscriptionStatus: string | null
Return Value
A Stripe Customer Portal session object containing:
The unique identifier for the portal session
The URL to redirect the customer to access the portal
The Stripe customer ID associated with this session
The URL customers will be returned to after exiting the portal (set to
/dashboard)The ID of the portal configuration used for this session
Behavior
- Validation: Checks that team has
stripeCustomerIdandstripeProductId- If missing, redirects to
/pricing
- If missing, redirects to
- Configuration Check: Looks for existing billing portal configurations
- Configuration Creation (if none exists):
- Verifies the product is active in Stripe
- Fetches all active prices for the product
- Creates a new portal configuration with:
- Subscription updates enabled (price, quantity, promotion codes)
- Subscription cancellation at period end
- Payment method updates enabled
- Cancellation reason collection
- Session Creation: Creates a portal session linked to the customer
- Return: Returns the session object (caller should redirect to
session.url)
Portal Features Enabled
Allows customers to update their subscription:
- Enabled:
true - Allowed updates:
['price', 'quantity', 'promotion_code'] - Proration behavior:
'create_prorations' - Customers can upgrade/downgrade between available prices
Allows customers to cancel their subscription:
- Enabled:
true - Mode:
'at_period_end'(cancels at the end of billing period) - Cancellation reasons:
['too_expensive', 'missing_features', 'switched_service', 'unused', 'other']
Allows customers to update their payment method:
- Enabled:
true
Usage Example
Server Action Example
API Route Example
Environment Variables Required
STRIPE_SECRET_KEY: Your Stripe secret API keyBASE_URL: Your application’s base URL (used for the return URL)
Error Handling
The function handles several error cases:- Missing Customer/Product: Redirects to
/pricingifstripeCustomerIdorstripeProductIdis null - Inactive Product: Throws error if the team’s product is not active in Stripe
- No Active Prices: Throws error if no active prices are found for the product
- Stripe API Errors: Will throw and should be caught by the caller
Error Examples
Portal Configuration
The portal configuration is created once and reused for all subsequent sessions. The configuration includes:- Business Profile Headline: “Manage your subscription”
- Return URL: Returns customers to
/dashboardafter portal actions - Proration: Creates prorations when customers upgrade/downgrade
- Cancellation: At period end, preventing immediate service disruption
customerPortalAction
A pre-built server action that wrapscreateCustomerPortalSession() with team middleware protection. This is the recommended way to access the customer portal in forms.
Function Signature
/lib/payments/actions.ts:12-15
Parameters
Form data (not used, but required by the server action signature)
Usage Example
How It Works
ThecustomerPortalAction uses the withTeam middleware wrapper, which:
- Automatically retrieves the current user’s team
- Validates team membership and subscription status
- Creates a customer portal session
- Redirects to the Stripe Customer Portal
- 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 access the customer portal.Related Functions
createCheckoutSession()- Create new subscriptionshandleSubscriptionChange()- Process subscription updates from webhooksgetStripeProducts()- Fetch available productscheckoutAction()- Server action for initiating checkout