Skip to content

feat(billing): webhook-side subscription dedup (#3184) - #3242

Merged
bokelley merged 2 commits into
mainfrom
bokelley/webhook-dedup-3184
Apr 26, 2026
Merged

bokelley merged 2 commits into
mainfrom
bokelley/webhook-dedup-3184

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

Closes the cross-path race that the intake-side guards (#3171 blockIfActiveSubscription, #3179/#3183 advisory lock) leave open. Stripe Checkout creates a session URL — the subscription gets minted only when the user completes the hosted page. Two concurrent intake paths (e.g., admin invite + member checkout) can both pass their guards before either mints a sub. Triton hit this in April 2026.

On customer.subscription.created, re-check the customer's full live-sub inventory. If another live sub (active/trialing/past_due) exists, cancel the new one with proration refund, alert ops via notifySystemError, and skip the org-row UPDATE so the surviving sub's state stays intact.

  • New helper: server/src/billing/dedup-on-subscription-created.ts
  • Wiring: server/src/http.ts webhook handler, gated by a suppressOrgUpdate flag

Failure semantics (intentional):

  • subscriptions.list failure → fall through (don't know). Refusing to process the webhook would block legitimate first-time subs during a Stripe blip.
  • subscriptions.cancel failure → still alert ops; manual intervention.
  • A missed duplicate is caught by the periodic integrity invariant one-active-stripe-sub-per-org (feat(billing): integrity invariants framework — Phase 1 (#3181) #3193).

Closes #3184.

Test plan

  • npm run typecheck clean
  • npx vitest run tests/unit/dedup-on-subscription-created.test.ts — 7 tests pass
    • no other live subs → no-op
    • other subs all canceled/incomplete → no-op
    • one other active sub → cancel + alert
    • trialing/past_due treated as live
    • subscriptions.list throws → fall through, no cancel
    • subscriptions.cancel throws → still alert ops
    • missing orgId renders as "org unknown" in alert
  • Pre-commit hook (test:unit + test:test-dynamic-imports + typecheck) green

🤖 Generated with Claude Code

bokelley and others added 2 commits April 25, 2026 20:24
…3184)

Closes the cross-path race that the intake-side guards (#3171
blockIfActiveSubscription, #3179/#3183 advisory lock) leave open.
Stripe Checkout creates a session URL, not a subscription — the sub
gets minted only when the user completes the hosted page. So two
concurrent intake paths (e.g., admin invite + member checkout) can
both pass their guards before either mints a sub, and the user can
complete both within seconds. Triton hit this in April 2026.

On customer.subscription.created we re-check the customer's full
live-sub inventory. If another live sub (active/trialing/past_due)
exists, we cancel the new one with proration refund, alert ops via
notifySystemError, and skip the org-row UPDATE so the surviving sub's
state stays intact.

Lookup or cancel failures fall through to "don't know" — a missed
duplicate is caught by the periodic integrity audit
(one-active-stripe-sub-per-org). Failing closed during a Stripe blip
would block legitimate first-time subscriptions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Three concrete reviewer-flagged fixes:

1. (BLOCKER) The follow-up customer.subscription.{updated,deleted}
   events for the canceled duplicate sub were clobbering the surviving
   sub's row state — exactly the bug this PR was meant to prevent. The
   suppressOrgUpdate flag only gates the .created event. Added a
   structural guard at the top of the .updated/.deleted branches: if
   the event's sub id isn't the one we currently track AND the event's
   status is non-live, ignore the event. .created is exempt because
   that's how we *learn* about a new sub.

2. Stripe webhook retries of customer.subscription.created would
   re-invoke the helper after we already canceled. Stripe's cancel on
   an already-canceled sub returns 400, not a silent no-op, which
   threw and re-fired the ops alert. Added an early-return when the
   incoming subscription.status is already non-live.

3. subscriptions.list({ status: 'all', limit: 20 }) could miss the
   live sub on customers with many canceled experiments. Bumped to
   limit: 100 (Stripe max) and warn when has_more.

Also addressed:
- Alert message correctly reflects cancel failure ("COULD NOT be
  canceled — cancel manually") instead of unconditional
  "was canceled with proration".
- Enriched the alert with new sub amount, lookup_key, and
  collection_method so ops can triage without clicking into Stripe.

Test coverage extended to: retry-of-canceled, page overflow, alert
content on cancel-success and cancel-failure, alert payload fields.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit fa5b6b3 into main Apr 26, 2026
11 of 12 checks passed
@bokelley
bokelley deleted the bokelley/webhook-dedup-3184 branch April 26, 2026 00:38
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.

Webhook-side dedup: refuse customer.subscription.created when org already has one

1 participant