Repository navigation
fix(billing): re-throw on transient revenue_events INSERT failure (closes #3693) - #3697
Merged
Merged
Conversation
) Two webhook handlers were swallowing all errors when inserting into revenue_events and returning 200 — Stripe never retried, so transient DB errors (statement timeout, connection blip, brief contention) silently lost paid revenue. The UNIQUE constraint on stripe_invoice_id makes the legitimate-retry case safe (PG code 23505 = duplicate Stripe re-fire), but the swallow was also masking real transient failures. Now distinguishes by error code: - 23505 (unique_violation): swallow — Stripe re-fired the same invoice.paid event, the row already exists - Anything else: re-throw — let Stripe retry with backoff Applied at: - http.ts:4180-4239 — invoice.paid revenue_events INSERT - http.ts:4400-4458 — invoice.payment_failed revenue_events INSERT The third site (charge.refunded at http.ts:4480) uses stripe_charge_id which is NOT a UNIQUE column on revenue_events. The 23505 pattern is unsafe there until a schema migration adds the constraint. Kept the swallow with a comment explaining the gap and pointing at the follow-up scope (add UNIQUE (stripe_charge_id) WHERE revenue_type = 'refund' partial index, then apply the same pattern). Per security-reviewer's framing on #3691: "audit-sync-revenue-backfill is a 90-day workaround that requires running it; refunds older than that window never reconcile." This closes the gap for invoice-based revenue events; refund-side gap remains in the follow-up. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
3 of 4 tasks
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 #3693 (the first of three triage follow-ups from #3691).
Two webhook handlers were swallowing all errors when inserting into
revenue_eventsand returning 200 — Stripe never retried, so transient DB errors silently lost paid revenue. Now distinguishes legitimate Stripe-retry duplicates (PG23505unique_violation) from real transient failures (re-throws so Stripe retries with backoff).Sites fixed
http.ts:4180-4239—invoice.paidrevenue_events INSERThttp.ts:4400-4458—invoice.payment_failedrevenue_events INSERT} catch (revenueError) { - logger.error({...}, 'Failed to insert revenue event'); - // Continue processing - don't fail the webhook + if ((revenueError as { code?: string })?.code === '23505') { + logger.info(...,'duplicate event ignored'); + } else { + logger.error({...}, 'Failed to insert revenue event — re-throwing so Stripe retries'); + throw revenueError; + } }Out of scope (follow-up): refund site
The third site at
http.ts:4480(charge.refunded) usesstripe_charge_idwhich is NOT a UNIQUE column onrevenue_events. The 23505 pattern is unsafe there until a schema migration adds the constraint (recommend partial indexUNIQUE (stripe_charge_id) WHERE revenue_type = 'refund').Kept the swallow with a comment explaining the gap. Per the security-reviewer's M3 finding, refunds older than Stripe's listable window never reconcile via the existing backfill admin tool — the refund-side schema follow-up is needed eventually.
Test plan
npx tsc --noEmitcleanadmin-sync-revenue-backfillintegration tests still pass (4/4)invoice.paidwebhook against a customer; assert 200; trigger again with sameevent.id; assert 200 + only one revenue_events row (UNIQUE caught)Refs
🤖 Generated with Claude Code