Skip to content

feat(billing): say on every page when a cancelled plan ends, with Keep my plan - #1823

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/cancel-ends-banner
Oct 30, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/cancel-ends-banner

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

From testing: after Cancel subscription on Billing the product kept working. That's right: the subscription ends at the period's end, and the trial's end in a trial. But only Billing said so. Now every page says it until that date, with a way to keep the plan. The product owner approved this in a local preview.

Migration 00386: organizations.cancel_at_period_end (boolean not null default false)

  • Why a column: cancellation wasn't stored before; only Billing read it live from Stripe. Storing it means pages don't call Stripe.
  • Guard: a server-only guard trigger of its own, with the same rolbypassrls pattern. It doesn't rewrite 00405's guard_organization_server_columns. On a fresh install 00386 runs before 00405, which re-creates that function, so the column would drop out of its list. A separate trigger doesn't depend on the order.

Writes

  • Webhook:
    • checkout.session.completed and customer.subscription.updated write sub.cancel_at_period_end;
    • customer.subscription.deleted writes false.
  • Billing: Cancel (DELETE) writes true with subscription_ends_at. Resume (PATCH reactivate) writes false. Both are optimistic, with the service role. Billing refreshes after a cancel, so the band shows at once.

The band (PlanEndsBanner)

  • Where: between ReadOnlyBanner and DemoBanner on every dashboard page, in the same amber style, with dark:. The layout passes subscription_ends_at and cancel_at_period_end to PlanProvider.planEndsAt.
  • Text: "Your plan ends on {date}." In a trial: "Your free trial ends on {date}, and your plan won't start."
  • Admins get Keep my plan (PATCH reactivate, which is admin-only today). Others read "To keep it, ask an admin of your workspace."
  • Dismiss: × hides it for the browser session (sessionStorage, keyed to the end date, wrapped in try/catch). A new date shows it again.
  • Not shown: once the date has passed (read-only mode takes over), on a read-only workspace, or on the demo brand.
  • Rendering: client-only, in the person's time zone, so there's no hydration mismatch.

Help: new help/settings/cancel-or-keep-your-plan.md (approved: false).

Related issue

None. Product owner's feedback from testing billing.

Type of change

  • feat — New feature

How to test

  1. Apply 00386. On a cloud workspace with an active plan, Billing › Cancel subscription. The band shows on every page with the end date.
  2. Keep my plan (admin) removes it, and Stripe's cancel_at_period_end is false. As a non-admin you see the "ask an admin" line.
  3. × hides it until the next browser session.
  4. Cancel during a trial: the trial wording shows.
  5. Tests:
    • web: plan-ends-banner, api/stripe webhook and subscription, plan-switcher, providers and Help validation (135 tests);
    • server: demo/coverage (7/7).

00386 was applied on the dev DB with the product owner's OK. The light and dark previews, and the trial wording, were shown and approved.

Deploy: apply 00386 before the web deploy, since the layout reads the column.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • bash supabase/build-schema.sh: schema.sql is current (00386)
  • Web: eslint and the related tests pass (typecheck left to CI)
  • Tests added
  • Help article updated

🤖 Generated with Claude Code

… my plan

A canceled subscription runs to the end of its paid period, but only
Billing said so. Every page now shows a thin band while it does: "Your
plan ends on {date}." For a canceled free trial it reads "Your free trial
ends on {date}, and your plan won't start." Decided 2026-10-29.

- 00386: organizations.cancel_at_period_end. Stripe's webhook writes it
  (checkout, subscription.updated; deleted clears it), and the cancel and
  reactivate routes mirror it at once. It is server only, held by its own
  trigger: 00405 rewrote guard_organization_server_columns after this
  number, so a fresh install would end on 00405's version, without the
  column. schema.sql and the Supabase types are updated.
- The layout passes the end date (planEndsAt) to PlanProvider. The band
  sits with the read-only and demo bands, in their amber style with dark
  variants.
- Keep my plan, for admins as the route allows, resumes the plan as
  Billing's Reactivate does. Other members read "To keep it, ask an admin
  of your workspace."
- The band closes for the browser tab's session (sessionStorage, by end
  date), and shows nothing past the end date (the read-only band takes
  over), while read-only, or on the demo brand.
- Billing's cancel refreshes the layout, so the band shows at once.
- Help: settings/cancel-or-keep-your-plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Buying a trial now sends cancel_at_period_end false to Stripe; the
optimistic organization update mirrors it, so a trial canceled before it
was bought loses the plan-ends band at once instead of when the webhook
lands.
gkhngyk
gkhngyk previously approved these changes Oct 30, 2026

@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 with 2cc6612 (one commit on this branch), which adds one small fix.

The fix: Buy now clears the flag too. Buying a trial now already sends cancel_at_period_end: false to Stripe, but the optimistic organization update after it wrote only plan and subscription_status. A trial canceled and then bought kept the "Your free trial ends on …, and your plan won't start" band until the webhook landed. The buy-now update now writes cancel_at_period_end: false as well. A plan or period change leaves the column alone, since Stripe keeps a cancellation as it is there. Tests: the buy-now expectation is updated, there's a new "canceled trial, then bought" case, and the period-change test checks the column isn't written. Both buy-now tests fail without the fix.

Checked

  • 00386: the new column has its own guard trigger, using the same rolbypassrls check as 00276/00405. It doesn't depend on the order of 00405's function, which is the right call. not null default false on organizations is a metadata-only change, with no rewrite. It's idempotent (if not exists / drop trigger if exists). The number is unique against main and every open PR.
  • Writes come only from the service role: the webhook (checkout, subscription.updated, subscription.deleted) and the subscription route (Cancel, Reactivate, and now Buy now). Keep my plan uses the existing PATCH reactivate, which is admin-only on the server before any Stripe call. Repeating it is harmless.
  • The layout adds two plain columns to its select, with no embeds. schema.sql matches build-schema.sh. plan-row.test.ts passes (no client → server import).
  • The band: client-only date, sessionStorage wrapped in try/catch, hidden on the demo brand, on a read-only workspace and past the end date. Billing's Cancel and Reactivate both refresh the layout.

Follow-ups (not blocking)

  • Existing canceled subscriptions: the column starts false for everyone. A workspace that canceled before this ships won't see the band until Stripe sends another subscription.updated. A one-off pass over the Stripe subscriptions with cancel_at_period_end = true would set them.
  • Webhook ordering: like subscription_status, the column takes whatever the last delivered event says. If a late "canceled" event lands after a quick Keep my plan, the band can come back until the next event. Reading the subscription fresh in the handler, or comparing event.created, would settle it for all the mirrored fields.
  • A cancellation made in the Stripe customer portal or the dashboard with a set date (cancel_at) rather than at period end wouldn't show the band. Worth a look if the portal is set up that way.

Merge: clean on current main. With the fix it conflicts with #1804 and #1818 in subscription/route.ts and its test, where both edit the same optimistic update. The resolution keeps both keys (subscription_ends_at from those PRs, cancel_at_period_end from here). It's clean with #1806, #1845 and #1846.

Deploy: apply 00386 before the web deploy (the layout reads the column).

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

Merging in order with the other approved PRs. This head merges current main and keeps both sides of the conflict with #1804 in the subscription route: Buy now's update writes both subscription_ends_at and cancel_at_period_end: false, and the route and webhook tests expect both. Migration 00386 is applied on production just before the merge.

@gkhngyk
gkhngyk merged commit b9c7b23 into ansvisor:main Oct 30, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants