Repository navigation
fix(success-fees): stop discarding errors on the two payment_transactions writes - #958
Conversation
…ions writes (#948) Both writes were bare `await`s. #948 item 6. ## The intent-creation insert (was line 429) Two holes, not one: * the error was discarded, so a failed insert still returned a `client_secret` and the customer could pay with nothing recorded * it was gated on `createdNewSuccessFee || createdNewIntent`, so the RETRIEVE branch — a second call for an intent that already exists — skipped it entirely. If the first call's insert had failed, the retry handed out a secret for an intent that still had no row. Now unconditional and idempotent. `payment_transactions` has a unique index on `stripe_payment_intent_id` (`payment_transactions_stripe_payment_intent_id_key`, confirmed against prod), so an upsert with `ignoreDuplicates` is a no-op when the row is already there and repairs the gap when it is not. And it fails the request rather than returning the secret. With no client_secret the intent is never confirmed, so no money moves — the same invariant #949 established for id-verification: there is a row for every charge, not usually a row. ## The confirmation update (was line 541) This is the one that records a CONFIRMED payment, and a failure returned 200 with the ledger quietly disagreeing with `success_fees` about whether the money arrived. Left non-fatal — the fee is already marked `upfront_paid` above and the customer has paid, so failing here would be worse than a row that lags, which the webhook or a reconciliation sweep can still settle — but it now logs with the intent id instead of vanishing. Verified: `deno fmt --check` and `deno lint` clean, edge-function ratchet unchanged at api 334, prepush green. No behaviour change on the happy path — the upsert writes exactly what the insert did. Remaining in #948: deploy and extend stripe-webhook for success_fees (item 3), and a reconciliation job for rows left pending (item 5). Those two are really one piece of work, and this change is what makes a reconciliation sweep possible — it guarantees the rows exist to sweep. Co-Authored-By: Claude Opus 5 <[email protected]>
629cd24 to
432953a
Compare
|
Pushed a fix — the first run failed `Lint, typecheck & build`, and it was mine. Replacing the conditional insert with an unconditional upsert removed the only code that read `createdNewSuccessFee` and `createdNewIntent`, leaving both assigned-but-never-read. Removed them, and reworded the comment that named them. Worth noting why I missed it locally. I verified with `deno fmt`, `deno lint` and `deno check` — all clean. But `functions/api/deno.json` excludes `no-unused-vars` from its lint rules: ```json So the Deno toolchain could not have caught this, and I treated "deno is clean" as "this file is clean". The repo's own `pnpm check` runs biome and tsc over the same files and does flag it — that is the check that matters for this directory, and the one I should have run before pushing. Now verified with both: `nx run supabase:typecheck` and `nx run-many -t lint -p supabase` pass, alongside deno fmt/lint/check and the edge-function ratchet still at `api 334`. |
📊 Performance Benchmark Results |
…talled rows (#948) (#960) Items 3 and 5 of #948, plus the reason neither could have worked. core.payment_transactions has carried a set_updated_at trigger and no updated_at column since migration 095. core.set_updated_at() assigns NEW.updated_at, so every UPDATE on the payment ledger raised `record "new" has no field "updated_at"` and rolled back: no row could ever leave its 'pending' default, in any environment. Invisible because the table is empty (Stripe unconfigured, #928) and because the /confirm handlers discarded that error until #949 and #958. Migration 361 adds the column; check-migrations.mjs gains a fourth invariant so the shape cannot return. stripe-webhook was in no deploy list, so no environment had it, and it only touched payment_transactions — a paid upfront fee left success_fees at 'pending' while the ledger said 'succeeded'. The ledger write and the paid-state advance now share _shared/payments/settlement.ts, which also backs the new payments-reconcile sweep, so a missed webhook settles identically to a delivered one. payments-reconcile asks Stripe about anything pending past a threshold and applies the answer. pg_cron every 10 minutes via migration 362, following 337's Vault pattern. It mirrors Stripe and never creates, confirms or cancels anything: non-terminal intents stay pending, and an intent absent at Stripe is logged rather than given a terminal status. Only a success moves paid-state. A decline is retryable against the same intent, so flipping the fee to 'failed' would end a live sale; the attempt is recorded on the ledger row instead. tests/routers/payments-settlement.test.ts covers all of it against the real database and is in GATED.txt — 9 passed. It is the test that found the missing column. Co-authored-by: Claude Opus 5 <[email protected]>
#948 item 6. Both writes were bare
awaits — and investigating the first one turned up a second hole I hadn't seen when filing the audit.The intent-creation insert
Two problems, not one:
client_secretand the customer could pay with nothing recorded.createdNewSuccessFee || createdNewIntent, so the retrieve branch — a second call for an intent that already exists — skipped it entirely. If the first call's insert had failed, the retry handed out a secret for an intent that still had no row.Just checking the error would have fixed (1) and left (2) wide open.
It is now unconditional and idempotent.
payment_transactionshas a unique index onstripe_payment_intent_id(payment_transactions_stripe_payment_intent_id_key— confirmed against prod), so an upsert withignoreDuplicatesis a no-op when the row is already there and repairs the gap when it is not.And it fails the request rather than returning the secret. With no
client_secretthe intent is never confirmed, so no money moves — the same invariant #949 established for id-verification: there is a row for every charge, not usually a row.The confirmation update
This is the one that records a confirmed payment, and a failure returned 200 with the ledger quietly disagreeing with
success_feesabout whether the money arrived.Left non-fatal — the fee is already marked
upfront_paidabove and the customer has paid, so failing here would be worse than a row that lags, which the webhook or a reconciliation sweep can settle — but it now logs with the intent id instead of vanishing.Verification
deno fmt --checkanddeno lintcleanapi 334pnpm prepushgreenWhat this unblocks
Remaining in #948: deploy and extend
stripe-webhookforsuccess_fees(item 3), and a reconciliation job for rows leftpending(item 5). Those are really one piece of work — and this change is what makes a sweep possible, because it guarantees the rows exist to sweep.🤖 Generated with Claude Code