Skip to content

fix(payments): deploy stripe-webhook, advance paid-state, and sweep stalled rows (#948) - #960

Merged
thinkclay merged 1 commit into
mainfrom
feat/payments-reconciliation
Oct 2, 2026
Merged

thinkclay merged 1 commit into
mainfrom
feat/payments-reconciliation

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

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_transactions has had a set_updated_at trigger and no updated_at column since migration 095 (Feb 2026). 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 in the ledger could ever leave its 'pending' default, in any environment. Not via stripe-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 /confirm handlers 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.sql adds the column (the sibling tables in 095 all have it; the column is the omission, not the trigger).
  • scripts/check-migrations.mjs gets a fourth invariant that fails on any table with the trigger and no column. Verified by deleting 361 and watching it name core.payment_transactions; 85 triggers pass with 361 in place.

Item 3 — deploy stripe-webhook, and make it advance paid-state

stripe-webhook was not in scripts/deploy-functions.sh, so no environment had it. It is now, along with payments-reconcile.

It also only touched payment_transactions. A successful upfront charge left core.success_fees at 'pending' with upfront_paid_at NULL 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 advances success_fees (upfront and final), background_checks.paid_at and background_check_access.paid_at.

Decisions worth disagreeing with if you do:

  • Only a success moves paid-state. A decline is retryable — Stripe emits payment_intent.payment_failed per 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.
  • The stage comes from the fee's own intent ids, with transaction_type as fallback. The fee knows which intent paid which half; the transaction's type is set by whoever wrote the row.
  • A missing ledger row is reported, never inserted. A row is written before the client_secret is 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.
  • Every UPDATE is guarded on the state it moves away from (.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.ts holds 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 in cron.job text, 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_capture counts 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 to GATED.txt so it fails CI:

ok | 9 passed | 0 failed (201ms)

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 than tests/unit/ because nothing runs tests/unit/, in CI or prepush. That is a separate finding and I will file it: 10 test files under packages/supabase/functions/_shared/__tests__ and packages/supabase/tests/unit are excluded from vitest and referenced by no workflow.

Also: edge-fn type ratchet holds both functions at 0; types.ts regenerated for the new column and verified against types-owned-schemas.mjs; migrations 361 and 362 applied to a local stack (362 schedules payments-reconcile|*/10 * * * * in cron.job); pnpm prepush green.

pnpm radium:check reports 3 violations — DrawerContent.tsx theme dimension and @hookform/resolvers — all pre-existing and untouched here.

Still open on #948

Item 2 — createPersonaInquiry still sits between a successful charge and the id_verifications insert. 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

…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]>
@thinkclay

Copy link
Copy Markdown
Contributor Author

Related: #961 — the ten packages/supabase test files that no runner executes, which is why payments-settlement.test.ts went into tests/routers/ rather than tests/unit/.

@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


@thinkclay
thinkclay merged commit bfe116f into main Oct 2, 2026
18 checks passed
@thinkclay
thinkclay deleted the feat/payments-reconciliation branch October 2, 2026 20:37
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.

1 participant