Skip to content

fix(billing): write the subscription's period end at checkout - #1358

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/subscription-ends-at-checkout
Oct 22, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/subscription-ends-at-checkout

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

A new subscription now gets its period end (organizations.subscription_ends_at) at checkout, not only at its first customer.subscription.updated.

This follows @gkhngyk's note on #1312. Only customer.subscription.updated wrote the column. checkout.session.completed and /api/stripe/success never set it, so a new trial had null there until Stripe's next update: its renewal, the trial's end or a plan change. On prod, 4 of 9 trialing orgs had null. Two places read it:

What changed (web only, no migration)

  • lib/billing/period-end.ts: periodEndIso(sub) reads the first item's current_period_end (Stripe API 2025-03-31+), falls back to the subscription's own, and returns null if neither is there. It is the same rule as before, now in one place:
    • it was getCurrentPeriodEndIso in api/stripe/subscription/route.ts;
    • the webhook's customer.subscription.updated had its own inline copy.
  • Webhook checkout.session.completed and GET /api/stripe/success write subscription_ends_at with the plan, status and Stripe ids. While the subscription trials, that is the trial's end.
  • When Stripe gives no period end, the field is left out of the update, not cleared, as customer.subscription.updated already did.
  • subscription/route.ts and customer.subscription.updated use 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 a stripe_subscription_id whose period end is null or 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 feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How 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, null when there is none.
  • api/stripe/webhook/route.test.ts (3, new):
    • checkout.session.completed writes subscription_ends_at with the plan, status and ids, then aligns prompts;
    • it leaves the field out when Stripe gives none;
    • customer.subscription.updated still writes it, and leaves it alone without one.
  • api/stripe/success/route.test.ts (2, new):
    • with an expanded subscription, it writes the period end and redirects to the dashboard;
    • with an unexpanded one, it retrieves the subscription and leaves the field out without a period end.
  • 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_at holds the trial's end right after the redirect, before any update event.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run: web yarn test (2,729 tests) and yarn build.

🤖 Generated with Claude Code

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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk the follow-up from your #1312 review: checkout.session.completed and /api/stripe/success now write subscription_ends_at (the trial's end while it trials), through one periodEndIso helper that the subscription route and customer.subscription.updated now share. Web only, no migration.

Existing null or past rows fill on their next customer.subscription.updated. If you'd rather fill them now, I can add a one-off script that reads each org's subscription from Stripe. It needs the live key, so it would be yours to run.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.retrieve with expand: ['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.completed can't put back an older date. When Stripe gives no period end, the field is left out, not cleared.

  • customer.subscription.updated behaves as before. It still writes the event's own copy, through the shared periodEndIso.

  • 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 quotaPeriodStart intends.

  • Tests. Both routes are tested with and without a period end. periodEndIso has 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):

  1. 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 is canceled or incomplete_expired, here and in checkout.session.completed.
  2. Checkout isn't admin-only. POST /api/stripe/checkout only 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.

@gkhngyk
gkhngyk merged commit c1d2299 into ansvisor:main Oct 22, 2026
6 checks passed
gkhngyk added a commit that referenced this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants