How Billing Works
Checkout, the billing portal, webhook sync, and the billing helpers — for when you change billing code.
You don't need this page to configure plans — that's Billing. Read it when you're changing how billing itself behaves.
Checkout
Every purchase — from the pricing page, the upgrade gate, the billing page,
or the post-sign-up redirect — goes through one URL:
/app/checkout/[planId]/[interval].
- The page itself has no side effects, so prefetches can't create Stripe sessions.
- Once it mounts, it calls the
continueToCheckoutServer Action once. - The action asks
getCheckoutRedirect()(src/lib/billing/checkout.ts) where to go, and redirects there:
| Situation | Destination |
|---|---|
| No organization | /sign-in |
| Not an organization admin | /app/dashboard |
| The organization already has a plan (except for one-time purchases) | /app/organization/billing |
| Unknown plan, or an interval the plan isn't sold at | /app/dashboard |
?trial=1, the plan offers a trial, and the user can't start one | /app/dashboard (upgrade gate) |
| Otherwise | A new Stripe Checkout session |
Checkout returns users to /app/organization/billing on success and to
/app/dashboard if they back out. Requests carry an idempotency key, so a
double click or a second tab within the same minute reuses one session.
To link to checkout yourself, render
<PlanActionButton plan={plan} interval={interval} />
(src/components/shared/plan-action-button.tsx) — it handles the label,
signed-out visitors, and "Contact us" plans — or build the URL with
getCheckoutPath(planId, interval, trial).
Billing portal
openBillingPortal() in src/app/[locale]/(saas)/_actions/billing.ts is a
Server Action limited to org:admin. It opens a Stripe customer portal
session where admins update their payment method, switch plans, or cancel.
Webhook sync
src/app/api/webhooks/stripe/route.ts verifies each request's signature
against STRIPE_WEBHOOK_SECRET and hands it to
paymentService.handleWebhook() (src/lib/billing/stripe/payment-service.ts).
That is the only code that writes to the org_subscriptions table:
| Event | What it does |
|---|---|
checkout.session.completed (subscription) | Resolves the price to a plan and interval, then writes plan, interval, status, trial end, and period dates. Records the trial in trial_redemptions if one started. |
checkout.session.completed (one-time) | Cancels the organization's current subscription or trial, then writes billingInterval: 'once' and subscriptionStatus: 'one_time'. |
customer.subscription.updated | Re-resolves plan and interval from the current price and refreshes status and period dates — this is how portal plan changes arrive. |
customer.subscription.deleted | Sets subscriptionStatus to canceled and clears the plan, if it's still the organization's current subscription. |
invoice.paid | Refreshes period dates and status on renewal. |
invoice.payment_failed | Sets subscriptionStatus to past_due (unless already canceled), which shows the past-due banner in the app. |
Whenever the plan changes, the handler also updates the organization's
Clerk membership limit (src/lib/billing/members.ts).
To react to another Stripe event, handle it here and add it to the webhook
endpoint's events (and the stripe listen --events list) — there's no other
path that updates billing state.
Helper functions
Billing helpers are split by where they can run:
@/lib/billing— pure logic, safe in Client and Server Components.@/lib/billing/server— everything above, plus database and Stripe access. Markedserver-only.
| Function | Import from | Purpose |
|---|---|---|
getOrgBilling(orgId) | @/lib/billing/server | Loads an organization's billing row. Cached per request. |
canStartTrial(meta) | @/lib/billing/server | Whether the signed-in user can start a trial for this organization. |
getCheckoutRedirect(planId, interval, requireTrial?) | @/lib/billing/server | Where a checkout request should go — see Checkout. |
paymentService | @/lib/billing/server | The Stripe implementation of checkout sessions, portal sessions, and webhook handling. |
hasAccess(meta) | @/lib/billing | Whether the organization has any access (gates the (gated) route group). |
hasFeature(meta, feature) | @/lib/billing | Whether the current plan includes a feature key. |
getPlanLimit(meta, key) / hasReachedLimit(meta, key, count) | @/lib/billing | Read a numeric limit and check usage against it (-1 means unlimited, missing means 0). |
getBillingState(meta) | @/lib/billing | 'trialing' | 'active' | 'past_due' | 'one_time' | 'free' | 'none'. |
getCurrentPlan(meta) | @/lib/billing | The current BillingPlan, falling back to the free plan if there is one. |
isSubscribed(state) / canSubscribe(state) | @/lib/billing | Convenience checks over a BillingState. |
getPlanAction(plan, interval, trialEligible?) | @/lib/billing | What a plan's button does: 'contact' | 'free' | 'purchase' | 'trial' | 'subscribe'. |
getPlanPrompt(meta, { isAdmin, trialEligible }) | @/lib/billing | Translation keys for the "choose a plan" heading and message. |
getCheckoutPath(planId, interval, trial?) | @/lib/billing | The checkout URL for a plan; trial adds ?trial=1. |
getPlanById, getFreePlan, getTrialPlans | @/lib/billing | Plan lookups over BILLING_PLANS. |
isFreePlan, isContactPlan, isOneTimePlan, offersTrial | @/lib/billing | What kind of plan a BillingPlan is. |