Stripe
The Stripe API version and webhooks, how a workspace starts a paid plan, how plans stay in sync with Stripe, what moves a workspace between billing states, and how to run demos and tests in Stripe test mode.
The SaaS module manages subscriptions through your Stripe account. It creates customers, calculates invoice tax through Stripe Tax, adds usage lines, issues credit notes and mirrors invoices from webhooks. Your business stays the merchant of record, and the module does not use Stripe Managed Payments.
Follow a free-to-paid upgrade
A workspace on a local free plan moves to a paid plan when its owner chooses one from Settings › Workspace › Billing › Change plan. DMS opens Stripe Checkout, and the completion webhook resolves the recorded checkout. Closing the browser, or returning to the app, does not prove that the payment completed.
This flow in Stripe test mode needs real test keys and a webhook, a paid plan
saved so that it has a Stripe price, and a return to a host in
allowedRedirectHosts, which defaults to the host of the DMS clientBaseUrl.
The playground's placeholder keys and fake Stripe IDs let you browse the UI.
They do not run this flow.
Starting a paid plan covers retries and pending checkouts. Plans and their Stripe products covers what happens when catalogue prices change.
Stripe API version and webhooks
Every Stripe request uses API version 2026-08-26.dahlia, set by
STRIPE_API_VERSION in src/stripe/client.ts. The constant is typed as the
installed stripe SDK's latest API version, so the build fails if the two
drift apart.
A webhook endpoint renders its events in its own API version, set in the
Stripe Dashboard (Developers › Webhooks › the endpoint › API version),
not in the version of the requests. Every endpoint that targets
/api/saas/webhooks/stripe must use 2026-08-26.dahlia. Until it does,
the SaaS module still accepts events rendered in older versions. The fields that the
2025-03-31.basil release moved are read from their basil location first and
from their older location second:
- the subscription billing period, now on each subscription item,
- the invoice's subscription, now under
parent.subscription_details, - invoice and credit note taxes, now in
total_taxes, - the credit note refund, now in
refunds.
The clover and dahlia releases moved none of the fields the module reads.
The SaaS module handles these webhook events and records every other event as
skipped:
| Event | What the module does |
|---|---|
checkout.session.completed | Applies the paid plan of the recorded checkout |
checkout.session.expired | Releases the recorded checkout and the trial it reserved |
invoice.created | Mirrors the invoice and admits it for usage lines |
invoice.finalized, invoice.voided | Mirrors the invoice |
invoice.paid | Mirrors the invoice, and reactivates a past-due or suspended workspace once no invoice is left open |
invoice.payment_failed | Mirrors the invoice, moves the workspace to past due and notifies the owners |
credit_note.created, credit_note.voided | Mirrors the credit note |
charge.refund.updated | Mirrors the refund, and notifies the owners when it succeeds |
customer.updated | Mirrors the billing address, email, company name and VAT number |
customer.subscription.updated | Stores the billing period, lands a scheduled plan change, and settles the end of a trial |
customer.subscription.pending_update_applied | Applies an upgrade whose payment Stripe confirmed |
customer.subscription.deleted | Cancels the workspace, or lands a scheduled Free plan |
customer.subscription.trial_will_end | Notifies the members that the trial ends soon |
New subscriptions, whether created at registration or by Checkout, open in the
classic billing mode (billing_mode[type]=classic). Since 2025-09-30.clover,
Stripe defaults new subscriptions to flexible. That mode no longer invoices a
free-to-paid upgrade at once, and it records Customer Portal cancellations as
cancel_at instead of cancel_at_period_end. The plan-change flows rely on
the classic behaviour that every existing subscription has.
Checkout Sessions opt out of Stripe Managed Payments
(managed_payments[enabled]=false). An account that enabled Managed Payments
applies it to every session by default. In this integration your business stays
the merchant of record, because the SaaS module collects tax IDs, runs Stripe Tax on
its invoices, injects invoice items and issues credit notes, and a Managed
Payments session allows none of these. If Stripe rejects the managed_payments
parameter, as an account never enrolled in Managed Payments may, the SaaS module logs
a warning and opens the session without it.
Starting a paid plan
Moving a workspace without a Stripe subscription to a paid plan opens a Stripe Checkout Session that stays payable for 30 minutes. The SaaS module records the request on the subscription as a pending checkout before it calls Stripe, and the session's completion or expiry webhook resolves it. A pending checkout never blocks the owner for good:
- If Stripe refuses to open the session (a 4xx answer), the SaaS module rolls back the
pending checkout, the trial it reserved and the Stripe customer it created.
The request answers
422 saas.errors.billing.checkout_rejected, and the owner can retry at once. - If Stripe cannot be reached or answers a 5xx, a session may exist, so the
pending checkout stays. The request answers
503 saas.errors.billing.checkout_unavailable. - The next attempt reconciles what it finds. It releases a pending checkout whose recorded session has expired, or that recorded no session within 15 minutes, and opens a new session. The redirect URL reaches the owner only once the session is recorded, so nobody can pay an unrecorded session.
- A recorded session that is still open answers
409 saas.errors.plan.checkout_in_progress. Any other pending plan change answers409 saas.errors.plan.change_in_progress. - The owner can give up a pending checkout from the billing page, or by leaving
Stripe through its cancel link (
DELETE /api/saas/tenant/plan/checkout). The SaaS module expires the recorded session first, so the link can no longer take a payment, and then releases the checkout. A cancel link from an earlier checkout releases nothing.
Plans and their Stripe products
The SaaS module keeps every plan billed through Stripe linked to a Stripe product and
price that match it, whoever writes the plan. The writer can be the
platform-owner plan editor, or a module that seeds its catalogue straight into
the plan table. A writer sets the plan's name, description, price, currency,
interval and billing mode, and leaves paymentProviderRefs alone, because
the SaaS module owns that field. The name, description and badge are localized
(@Localized, falling back to en). A writer stores one text per locale
({ en: "Pro", fr: "Pro" }, written through localize("*")), never an i18n
key. A Stripe product holds one text, so it gets the plan's name and
description in en.
A plan is billed through Stripe when its price is above 0, or when it is already linked to a Stripe price that its subscriptions may be billed on. A plan priced 0 that never had a Stripe price stays off Stripe, and its workspaces hold a local free subscription.
The sync runs at these points:
- When the plan editor creates or saves a plan, after writing it. The plan is
saved either way. When a billed plan is left without its Stripe price
(Stripe not configured, or a Stripe error, which is logged), the save answers
a warning notice (
saas.catalog.editor.notice.stripe_not_synced). The editor shows it next to Plan saved., and the next pass retries the sync. - Once DMS has initialised the database at startup, in the background, for every active plan.
- When a platform owner changes the Stripe tax code in the billing settings, for every active plan.
- Whenever a plan is about to be offered or billed (the tenant plan catalogue, a plan change, an operator action, a new workspace), so a plan written after startup is linked the first time it matters.
Outside the editor, a failed sync is logged and retried on the next pass.
Meanwhile the plan shows as not payable. Choosing it answers
400 saas.errors.plan.not_synced_with_stripe, and the checkout dialog shows
that message and offers no payment. With a placeholder Stripe key, every sync
is skipped. The plan cards badge a priced plan that has no Stripe price
Not on Stripe (isMissingStripePrice in the plans table). For a billed
plan, the editor's sync state reads Stripe not configured as a warning.
Each sync compares the plan with the terms recorded in
paymentProviderRefs.stripeSyncedTerms and calls Stripe only when they differ:
| What changed | Stripe effect |
|---|---|
| Nothing | None |
| Name, description, or the billing settings' tax code | The product is updated in place |
| Price, currency, interval or billing mode | A new price is created and the previous one archived. Existing subscriptions stay on their price |
Amounts are sent in the currency's minor unit, using the currency's decimal places (cents for EUR, whole yen for JPY), and the currency is sent in lower case. A plan linked before the terms were recorded is read back from Stripe once. A price that still matches is kept, so its subscriptions still resolve to the plan.
Every Stripe creation carries an idempotency key derived from the plan ID, its
creation time and the request. Replicas that sync the same plan at once, or a
retry within Stripe's 24-hour window, therefore land on the same product and
price. Only paymentProviderRefs is written back, so a sync never rolls back a
concurrent edit. A sync that raced an edit leaves the plan out of line, and the
next pass redoes it.
What moves a workspace's billing state
A workspace changes billing state on two clocks. Stripe's webhooks move it for
everything a payment decides, and the module's own crons move it for every
deadline the module keeps. Without webhooks reaching
/api/saas/webhooks/stripe, only the crons run.
| Transition | Moved by |
|---|---|
| Pending payment → active or trialing | checkout.session.completed |
| Trialing → active, or → past due when the trial's first charge fails | customer.subscription.updated |
| Active or trialing → past due (a charge fails) | invoice.payment_failed, except on a suspended or cancelled workspace |
| Past due or suspended → active (the last open invoice is paid) | invoice.paid |
| Paid → cancelled (the cycle a cancellation waited for ends) | customer.subscription.deleted |
| Paid → a scheduled Free plan (the cycle ends) | customer.subscription.deleted. A downgrade under a plan change policy is also landed by a cron every minute |
| Complimentary access whose end date passed → past due | Cron, daily at 02:00 |
| Past due for longer than the suspension delay (14 days by default) → suspended | Cron, daily at 01:00, when automatic suspension is on |
| Cancelled for longer than the data retention (30 days by default) → workspace and data deleted | Cron, daily at 04:30 |
The delays are the billing rules.
The crons run in the server's time zone. They compare the subscription's own
dates with the time they run: the dunning clock pastDueSince for a
suspension, freeUntil for complimentary access, and the subscription's last
write (updatedAt, which the database layer sets on every write) for a
cancellation. A cron also publishes the directory row of every workspace again
every 15 minutes.
A downgrade to a Free plan under a registered plan change policy closes the workspace's paid access at the end of the cycle, whether or not a webhook has arrived. On each run, the per-minute cron reads the subscription from Stripe. When Stripe reports it cancelled and the workspace passes validation against the Free plan, the cron lands the plan. Otherwise it records the change as blocked and notifies the owners.
Nothing else advances a subscription. A trial whose end date passed stays
trialing until Stripe reports how it ended. A renewal date that passed is not
renewed. A past-due workspace waits for the payment Stripe reports, or for the
suspension cron.
Demos, tests and seeded data
Stripe is the module's only billing path, demos and tests included. Give the
module the keys of a Stripe account in test mode (sk_test_…, pk_test_…)
and forward its webhooks. In development, use the Stripe CLI
(stripe listen --forward-to localhost:5010/api/saas/webhooks/stripe, which
pnpm stripe:webhooks runs in packages/dms-saas). Test mode moves no money,
and every screen then shows what production shows. Invoices, the next invoice
preview, the payment method, refunds and operator actions all read or call
Stripe.
The module mirrors only what its own flows create. A webhook names a Stripe
customer, and the module acts on it only when one of its workspaces holds that
customer. A subscription created straight in Stripe for a customer of its own
is not adopted. To show paying workspaces, go through the flows in test mode.
Register or create a workspace, pay with the test card 4242 4242 4242 4242,
change plans, and cancel. A card that attaches but declines every charge, such
as 4000 0000 0000 0341, reaches the past-due and suspension states.
A project that writes the module's tables directly, as the playground data set does to review every screen without Stripe, takes on what the module then derives from them:
- Dates must be relative to the time of writing. The crons above compare them with the time they run, so a workspace seeded past due for longer than the suspension delay is suspended at the next 01:00. A cancelled workspace is deleted with its data 30 days after it was written.
- An
activesubscription without astripeSubscriptionIdis a free workspace. It bills nothing and counts for 0 in MRR. - Fake Stripe IDs make it count, but every action that calls Stripe for it fails: plan changes, cancellation, refunds, credit notes and balance credits.
- Once its rows are written, call
RecomputeTenantBillingState(reference). The directory, MRR and churn read the row it publishes. A workspace first published already suspended or cancelled counts its plan's MRR as churn only when it has a paid invoice above 0.