Repository navigation
fix(payments): deploy stripe-webhook, advance paid-state, and sweep stalled rows (#948) - #960
Merged
Merged
Conversation
…talled rows (#948) 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]>
4 tasks
Contributor
Author
|
Related: #961 — the ten |
6 tasks
Contributor
📊 Performance Benchmark Results |
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.
Closes two of the six boxes on #948 — the webhook that advances nothing, and the reconciliation job that does not exist — and fixes a third thing found on the way that is larger than either.
The thing found on the way
core.payment_transactionshas had aset_updated_attrigger and noupdated_atcolumn since migration 095 (Feb 2026).core.set_updated_at()assignsNEW.updated_at, so every UPDATE on the payment ledger raisedand rolled back. No row in the ledger could ever leave its
'pending'default, in any environment. Not viastripe-webhook(which would then 500 and make Stripe retry the event forever), not via/confirm-upfront, not via a refund.It survived eighteen months because the table is empty — Stripe has never been configured anywhere (#928) — and because the two
/confirmhandlers discarded that update's error until #949 and #958 made them report it. It is the only table in the database with the trigger and no column.361_payment_transactions_updated_at.sqladds the column (the sibling tables in 095 all have it; the column is the omission, not the trigger).scripts/check-migrations.mjsgets a fourth invariant that fails on any table with the trigger and no column. Verified by deleting 361 and watching it namecore.payment_transactions; 85 triggers pass with 361 in place.Item 3 — deploy
stripe-webhook, and make it advance paid-statestripe-webhookwas not inscripts/deploy-functions.sh, so no environment had it. It is now, along withpayments-reconcile.It also only touched
payment_transactions. A successful upfront charge leftcore.success_feesat'pending'withupfront_paid_atNULL while the ledger said'succeeded'— the two tables disagreeing about whether money arrived, which #948 called "arguably worse than both rows being wrong together".The ledger write and the paid-state advance now both live in
_shared/payments/settlement.ts, shared with the sweep so a reconciled payment and a webhook-settled payment cannot end up in different states. It advancessuccess_fees(upfront and final),background_checks.paid_atandbackground_check_access.paid_at.Decisions worth disagreeing with if you do:
payment_intent.payment_failedper attempt and the customer can pay the same intent with another card — so marking the hire's fee'failed'on the first decline would end a live sale. Failures and cancellations are recorded on the ledger row, which is where attempt history belongs.transaction_typeas fallback. The fee knows which intent paid which half; the transaction's type is set by whoever wrote the row.client_secretis handed out (fix(id-verification): record the charge before the client can make it #949), so a missing one means an intent from some other path — we would not know its type, org, or what it paid for, and a guessed row in a financial ledger is worse than an absent one..is(paid_at, null),.in(status, …)), so replays are no-ops and a late upfront event cannot walk a completed fee backwards._shared/payments/stripe-client.tsholds the client construction both functions need; the webhook moved onto it rather than keeping its own copy.Item 5 — the reconciliation sweep
payments-reconcile, driven by pg_cron every 10 minutes against a 15-minute threshold (migration 362, following 337's Vault pattern exactly — no secret incron.jobtext, identical in every environment). It asks Stripe about anything pending past the threshold and applies the answer through the shared module.It is a mirror, not a decision-maker: it never creates, confirms or cancels anything at Stripe. Non-terminal intents are left pending and counted.
requires_capturecounts as open — an authorisation is not a payment. An intent that does not exist at Stripe is left pending and logged, because writing a terminal status from an absent intent would be inventing a financial fact. Oldest-first with a 45s budget inside the 55s cron slot, so a backlog drains across runs instead of timing out mid-sweep. With nothing pending it is one indexed SELECT and no Stripe call.Verification
tests/routers/payments-settlement.test.ts— 9 tests, real rows in the real local database, added toGATED.txtso it fails CI:Covering: ledger + fee both advance; replaying the same event id changes nothing; a sweep over an already-settled row is a no-op (no event id, so idempotency comes from the UPDATE guards — the case that runs in production every 10 minutes); a decline leaves the fee payable; a success after a decline still advances it and clears the failure; an orphan intent is reported not invented; the final stage completes the fee chosen by the fee's intent ids; a late upfront event cannot walk a completed fee backwards.
It is the one file in
tests/routers/that does not speak HTTP — what it tests is the UPDATE guards themselves. It went in the gate rather thantests/unit/because nothing runstests/unit/, in CI or prepush. That is a separate finding and I will file it: 10 test files underpackages/supabase/functions/_shared/__tests__andpackages/supabase/tests/unitare excluded from vitest and referenced by no workflow.Also: edge-fn type ratchet holds both functions at 0;
types.tsregenerated for the new column and verified againsttypes-owned-schemas.mjs; migrations 361 and 362 applied to a local stack (362 schedulespayments-reconcile|*/10 * * * *incron.job);pnpm prepushgreen.pnpm radium:checkreports 3 violations —DrawerContent.tsxtheme dimension and@hookform/resolvers— all pre-existing and untouched here.Still open on #948
Item 2 —
createPersonaInquirystill sits between a successful charge and theid_verificationsinsert. The charge is recorded before the money moves now (#949), so it is no longer unrecoverable, but the inquiry can still fail after a successful payment.Not verified here
No environment has Stripe keys, so nothing in this PR has settled a real payment. What is proven is the database behaviour; what is not is the Stripe round trip. The webhook secret is still NULL in all three envs — these functions deploy inert until the keys go in, which is deliberate.
🤖 Generated with Claude Code