Repository navigation
feat(billing): webhook-side subscription dedup (#3184) - #3242
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 vianotifySystemError, and skip the org-row UPDATE so the surviving sub's state stays intact.server/src/billing/dedup-on-subscription-created.tsserver/src/http.tswebhook handler, gated by asuppressOrgUpdateflagFailure semantics (intentional):
subscriptions.listfailure → fall through (don't know). Refusing to process the webhook would block legitimate first-time subs during a Stripe blip.subscriptions.cancelfailure → still alert ops; manual intervention.one-active-stripe-sub-per-org(feat(billing): integrity invariants framework — Phase 1 (#3181) #3193).Closes #3184.
Test plan
npm run typecheckcleannpx vitest run tests/unit/dedup-on-subscription-created.test.ts— 7 tests passsubscriptions.listthrows → fall through, no cancelsubscriptions.cancelthrows → still alert opsorgIdrenders as "org unknown" in alerttest:unit+test:test-dynamic-imports+typecheck) green🤖 Generated with Claude Code