web: billing recovery — email-claim redemption, portal for lapsed subscribers, dunning

Merged
LA
3mo ago
+644-41

Three billing-recovery fixes so paid users cannot get stranded.

Email-claim redemption. billing_email_claims rows (anonymous buyer whose Stripe email belongs to another Stack account) were write-only; the buyer paid and got nothing. Claims are now matched on verified-email sign-in and from a dashboard billing banner, transferring the Stripe customer and subscription to the signed-in Stack user under the existing per-principal advisory lock, marking the claim redeemed, and refreshing entitlements through the pro.ts sync. Covered by tests for redeem, double-redeem, and mismatched email.

Portal for lapsed subscribers. billingManagement was "stripe" only while isPro, so a canceled or unpaid subscriber saw Upgrade and a fresh checkout (duplicating Stripe customers). Any user with an existing Stripe customer record now gets the portal (pricing pages, app-pricing, dashboard); Upgrade remains for users with none.

Dunning email (descoped). After review, the dunning email was descoped because link-identity, retry-terminality, and ledger-retention design questions need a separate design. It will return as a dedicated PR with an outbox pattern and a retention policy. For now, the dashboard past_due banner remains the dunning surface.

Verification: bun run typecheck clean; bun test tests/billing-ownership.test.ts tests/billing-checkout-route.test.ts, 29 pass. Claim redemption requires a verified email match, exact-match only, no fuzzy matching by design; the redemption banner polls no external state (server-rendered on page load).

Work was produced across interrupted codex runs and completed and reviewed by the session agent.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes two billing-recovery paths so paid users cannot get stranded: email claims now redeem on verified sign-in, and lapsed subscribers keep portal access instead of creating duplicate checkout customers. A dunning email was descoped to a follow-up; past-due users instead see a dashboard banner.

Email-claim redemption

  • billing_email_claims rows (anonymous buyers whose Stripe email belongs to another Stack account) were write-only, so the buyer paid and received nothing.
  • Claims redeem on verified-email sign-in and from a dashboard billing banner, transferring the Stripe customer and subscription under the existing per-principal advisory lock, marking the claim redeemed, and refreshing entitlements via the pro.ts sync.
  • Redemption requires an exact email match; retried sign-in callbacks cannot double-redeem.

Lapsed subscribers

  • billingManagement is "stripe" only for users or teams with a portal-recoverable subscription (active, trialing, past-due, unpaid, or cancel-at-period-end); customer-only rows and terminally canceled subscriptions keep the checkout path, which reuses the existing customer instead of creating a duplicate.
  • Portal metadata comes from the best recoverable subscription row (latest period end) rather than the newest-updated row, so a late-touched canceled row cannot route an unpaid owner to duplicate checkout; active entitlement comes from any active row.
  • Checkout redirects portal-recoverable subscriptions to the portal instead of opening a second session.
  • The dashboard billing page shows a past-due banner linking the portal; past-due still counts as active entitlement, but the user is notified immediately.

Summary by CodeRabbit

  • New Features

    • Added billing portal access for past-due and lapsed subscriptions.
    • Pricing pages now show “Manage billing” when portal management is available.
    • Added failed-payment banners and localized billing notification emails with portal links.
    • Added safeguards to prevent duplicate billing notifications and support reliable delivery retries.
    • Customer-facing billing links now use a trusted application origin.
  • Bug Fixes

    • Improved separation of personal and team billing records.
    • Prevented duplicate or misdirected billing claim transfers.
    • Checkout now redirects eligible customers to billing management instead of creating a new session.