ShiftFast Docs

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].

  1. The page itself has no side effects, so prefetches can't create Stripe sessions.
  2. Once it mounts, it calls the continueToCheckout Server Action once.
  3. The action asks getCheckoutRedirect() (src/lib/billing/checkout.ts) where to go, and redirects there:
SituationDestination
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)
OtherwiseA 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:

EventWhat 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.updatedRe-resolves plan and interval from the current price and refreshes status and period dates — this is how portal plan changes arrive.
customer.subscription.deletedSets subscriptionStatus to canceled and clears the plan, if it's still the organization's current subscription.
invoice.paidRefreshes period dates and status on renewal.
invoice.payment_failedSets 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. Marked server-only.
FunctionImport fromPurpose
getOrgBilling(orgId)@/lib/billing/serverLoads an organization's billing row. Cached per request.
canStartTrial(meta)@/lib/billing/serverWhether the signed-in user can start a trial for this organization.
getCheckoutRedirect(planId, interval, requireTrial?)@/lib/billing/serverWhere a checkout request should go — see Checkout.
paymentService@/lib/billing/serverThe Stripe implementation of checkout sessions, portal sessions, and webhook handling.
hasAccess(meta)@/lib/billingWhether the organization has any access (gates the (gated) route group).
hasFeature(meta, feature)@/lib/billingWhether the current plan includes a feature key.
getPlanLimit(meta, key) / hasReachedLimit(meta, key, count)@/lib/billingRead 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/billingThe current BillingPlan, falling back to the free plan if there is one.
isSubscribed(state) / canSubscribe(state)@/lib/billingConvenience checks over a BillingState.
getPlanAction(plan, interval, trialEligible?)@/lib/billingWhat a plan's button does: 'contact' | 'free' | 'purchase' | 'trial' | 'subscribe'.
getPlanPrompt(meta, { isAdmin, trialEligible })@/lib/billingTranslation keys for the "choose a plan" heading and message.
getCheckoutPath(planId, interval, trial?)@/lib/billingThe checkout URL for a plan; trial adds ?trial=1.
getPlanById, getFreePlan, getTrialPlans@/lib/billingPlan lookups over BILLING_PLANS.
isFreePlan, isContactPlan, isOneTimePlan, offersTrial@/lib/billingWhat kind of plan a BillingPlan is.

On this page