Repository navigation
fix(billing): write the subscription's period end at checkout - #1358
Conversation
Only customer.subscription.updated wrote organizations.subscription_ends_at, so a new trial had none until Stripe's next update: the plan guard counted quotas by calendar month and Cancel's "give us 3 days" had no date. The checkout webhook and the success route now write it with the plan, through one periodEndIso helper that the subscription route and the update webhook share. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk the follow-up from your #1312 review: Existing |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good.
-
No older value over a newer one. Both new writes read the subscription from Stripe when they run:
- the webhook through
stripe.subscriptions.retrieve(web/src/app/api/stripe/webhook/route.ts:54); - the success route through
checkout.sessions.retrievewithexpand: ['subscription'](web/src/app/api/stripe/success/route.ts:22-32).
So they write Stripe's current period end, in the same update as the subscription id it belongs to. A late or retried
checkout.session.completedcan't put back an older date. When Stripe gives no period end, the field is left out, not cleared. - the webhook through
-
customer.subscription.updatedbehaves as before. It still writes the event's own copy, through the sharedperiodEndIso. -
Who can reach these paths is unchanged. The webhook checks Stripe's signature. The success route only trusts the session Stripe returns.
-
The plan guard's quota window. A trialing org's window now runs to the trial's end, not to the end of the calendar month. When the trial converts, the window moves to the paid period. That is what
quotaPeriodStartintends. -
Tests. Both routes are tested with and without a period end.
periodEndIsohas its own tests. -
Merging. It merges cleanly on current main and with #1312 and #1328. There is no migration, and the server doesn't change.
Two older problems, not for this PR (could be follow-ups):
- An old success link can undo a newer subscription. A browser history entry for
/api/stripe/success?session_id=…replays the write. If that session's subscription was cancelled and replaced since, the org goes back to the old subscription's id, plan and status. With this PR, the old period end comes back too. The replay also starts tracking and volume analysis again. Fix: skip the write when the retrieved subscription iscanceledorincomplete_expired, here and incheckout.session.completed. - Checkout isn't admin-only.
POST /api/stripe/checkoutonly checks that the caller belongs to the org. Changing or cancelling a plan (PATCH/DELETE) also needs the admin role. Checkout also doesn't refuse an org that already has a live subscription. Onboarding is its only caller, so this only matters for a direct request.
Tested on top of main together with #1328: web and server tests, tsc, lint and Prettier pass.
Summary
A new subscription now gets its period end (
organizations.subscription_ends_at) at checkout, not only at its firstcustomer.subscription.updated.This follows @gkhngyk's note on #1312. Only
customer.subscription.updatedwrote the column.checkout.session.completedand/api/stripe/successnever set it, so a new trial hadnullthere until Stripe's next update: its renewal, the trial's end or a plan change. On prod, 4 of 9 trialing orgs hadnull. Two places read it:quotaPeriodStart): withnullit counts quotas by calendar month instead of from the billing day;What changed (web only, no migration)
lib/billing/period-end.ts:periodEndIso(sub)reads the first item'scurrent_period_end(Stripe API 2025-03-31+), falls back to the subscription's own, and returnsnullif neither is there. It is the same rule as before, now in one place:getCurrentPeriodEndIsoinapi/stripe/subscription/route.ts;customer.subscription.updatedhad its own inline copy.checkout.session.completedandGET /api/stripe/successwritesubscription_ends_atwith the plan, status and Stripe ids. While the subscription trials, that is the trial's end.customer.subscription.updatedalready did.subscription/route.tsandcustomer.subscription.updateduse the helper. Their behaviour is unchanged.Existing rows fill on their next
customer.subscription.updated. To fill them sooner, the subscription can be read from Stripe for each org with astripe_subscription_idwhose period end isnullor past. That needs the live Stripe key, so it would be yours to run. I can add a one-off script for it if you want.Related issue
Follow-up to #1312 (review note, 2026-10-22). No issue.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
The checkout needs Stripe, so the routes are covered by tests with Stripe and Supabase mocked:
lib/billing/period-end.test.ts(3): item-level first, the subscription's own for older API versions,nullwhen there is none.api/stripe/webhook/route.test.ts(3, new):checkout.session.completedwritessubscription_ends_atwith the plan, status and ids, then aligns prompts;customer.subscription.updatedstill writes it, and leaves it alone without one.api/stripe/success/route.test.ts(2, new):api/stripe/subscription/route.test.ts(12) passes unchanged.With a Stripe test key: start a trial from Billing, then check that
organizations.subscription_ends_atholds the trial's end right after the redirect, before any update event.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/) (0 errors; the 6 warnings are in untouched files, as on main)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run: web
yarn test(2,729 tests) andyarn build.🤖 Generated with Claude Code