Skip to content

fix(id-verification): create the Persona inquiry before the charge, not after (#948) - #965

Merged
thinkclay merged 1 commit into
mainfrom
fix/id-verification-inquiry-order
Oct 5, 2026
Merged

thinkclay merged 1 commit into
mainfrom
fix/id-verification-inquiry-order

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes the last open box on #948.

The gap

/confirm called createPersonaInquiry between a successful charge and the id_verifications insert that records it. core.id_verifications.persona_inquiry_id is NOT NULL, so Persona being down did not degrade the flow — it made the row unwritable:

  • the customer has paid
  • no verification row exists, and none can be created without an inquiry
  • a retried /confirm calls Persona again and fails again
  • once the client loses the payment intent id, nothing can finish the job

#949 made the charge recoverable (the payment_transactions row is written before the client_secret is handed out), so this was no longer invisible — a succeeded ledger row with id_verification_id NULL. But the customer still had no verification and no path to one.

The fix

/request now creates the Persona inquiry before the Stripe PaymentIntent exists, and carries its id on the intent's metadata and on the ledger row. /confirm reads it from there, so on the normal path there is no network call at all between a successful charge and the record of it.

This is the issue's first option ("call it before confirming") rather than its second ("record the payment first and let the inquiry be retried"), because persona_inquiry_id is NOT NULL: recording first would have needed a migration to make it nullable, a new status value, a retry sweep and another cron. Moving the call needs none of that, and it inverts the failure in the direction you want — Persona down now means /request returns 503 with nothing charged, instead of a paid customer with nothing to show for it.

resolvePersonaInquiry keeps a fallback chain — intent metadata → ledger metadata → mint a new one — so intents already in flight when this deploys still complete rather than stranding someone who has paid. Only that last branch makes a network call, and only for a pre-change intent.

The cost is an orphan inquiry when a customer abandons checkout. Persona inquiries that are created and never started expire on their side, and reference_id is the worker's id so they stay traceable. That is a much better failure than charging for a verification we then cannot create.

Verification

tests/routers/id-verification-rest.test.ts — two tests against the real api function and the real database, Stripe in mock mode:

ok | 2 passed | 0 failed (418ms)

They assert the invariant, not the happy path: that the inquiry id stored on id_verifications is the one that existed before the charge was possible.

Red path measured. With the route reverted to origin/main and the tests unchanged:

error: ledger metadata carries no persona_inquiry_id: {"stage":"intent","source":"id_verification_rest"}
FAILED | 0 passed | 2 failed

Then green again with the fix restored. Green → red → green, not just green.

Also: edge-fn type ratchet — api measures 334, exactly its baseline; deno fmt clean on the route; biome lint clean; pnpm prepush green.

What I did not do, and why

The test is not in GATED.txt. It needs STRIPE_MOCK_MODE=1 inside the function's runtime, and nothing provides that: supabase start has no --env-file, so the shared edge runtime CI brings up cannot see it, and without it loadStripeClient throws Stripe API key is not configured. before either route does anything interesting. Listing it in the gate would list a file that cannot pass, which is the one thing that file's header forbids.

So the suite carries the exact command it was verified with, and the recipe generalises to every REST payment route — which is #937. I have commented there rather than opening a new issue. I am aware this is the shape I complained about in #961; the difference is that the run path is written down in the file and the follow-up is scoped, not that it is fine.

No Stripe round trip is proven. No environment has Stripe keys, so the mock is the only Stripe these tests have seen. What is proven is the ordering and the metadata handoff.

One thing I could not explain. Locally the ratchet reports trpc/routers/teams.router.ts at 2 errors against a baseline of 1 — a z.date({ message }) (zod 4 syntax) on a line from #190. It reproduces on a clean main worktree with dependencies installed, and CI's "Edge function types" job passes on main, so local and CI disagree about a gate. Not caused by this PR and not fixed here, but a gate that disagrees with itself is worth someone's attention.

🤖 Generated with Claude Code

…ot after (#948)

Item 2 of #948, the last one open.

/confirm called createPersonaInquiry between a successful charge and the
id_verifications insert that records it. persona_inquiry_id is NOT NULL,
so Persona being down did not degrade the flow — it made the row
unwritable: the customer had paid, no verification could be created
without an inquiry, a retried /confirm called Persona again and failed
again, and once the client lost the payment intent id nothing could
finish the job. #949 made the charge recoverable; the verification was
not.

/request now mints the inquiry before the Stripe intent exists and
carries its id on the intent metadata and the ledger row, so /confirm
makes no network call on the normal path. Persona down means /request
returns 503 with nothing charged.

Chosen over "record the payment first and retry the inquiry" because
persona_inquiry_id is NOT NULL: that route needed a nullable-column
migration, a new status, a retry sweep and another cron. This needs
none. resolvePersonaInquiry keeps a fallback chain — intent metadata,
then ledger metadata, then a fresh inquiry — so intents in flight when
this deploys still complete.

tests/routers/id-verification-rest.test.ts asserts the inquiry stored on
id_verifications is the one that existed before the charge was possible.
Measured green, then red on the pre-fix route with the tests unchanged
(`ledger metadata carries no persona_inquiry_id`), then green again. It
is not in GATED.txt: it needs STRIPE_MOCK_MODE inside the function
runtime and `supabase start` has no --env-file, so CI cannot give it
that. The file carries the command it was verified with; wiring the gate
is #937.

Co-Authored-By: Claude Opus 5 <[email protected]>
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


@thinkclay

Copy link
Copy Markdown
Contributor Author

CI green: 11 pass, 4 skipping, none red — including Edge function types, which is the job that disagrees with my local run. Filed that as #969 with the evidence; it is not caused by this PR, and api measures 334 (its exact baseline) in both environments.

Ready to merge whenever you want it. Nothing here moves money: no environment has Stripe keys, and stripe-webhook/payments-reconcile are still undeployed.

@thinkclay
thinkclay merged commit 1a735c9 into main Oct 5, 2026
15 checks passed
@thinkclay
thinkclay deleted the fix/id-verification-inquiry-order branch October 5, 2026 12:11
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