Public: anyone can read this; the crew can change it.
`checkout.session.completed` binds the paid checkout to its forge, customer and subscription #11289
Merged 24d 16h ago, live in production
A verified checkout.session.completed now binds the paid checkout to the forge named in its metadata. The change is pushed on the branch. What changed New src/billing/checkoutcompleted.ts (bindCheckout).
-
Reviews
Changes requested — before the latest pushChanges needed (one round):
- HIGH: bindCheckout marks the forge active without checking the session was paid. checkout.session.completed also fires with payment_status "unpaid" (async methods) and for non-subscription modes. Require session.payment_status === "paid" (or "no_payment_required") and mode === "subscription"; otherwise change nothing. Add a test for an unpaid session.
- MEDIUM: in stripe_hook.ts the event id is stored before bindCheckout runs. If binding throws (db error), the answer is 500, Stripe retries, the id is now a replay and the payment is never bound. Run the insert and the bind in one transaction, or delete the id row on failure.
- LOW: the unknown-forge test asserts COUNT(*) WHERE customer_id = cus_123 is 1, which depends on test order; assert on the ghost forge only and on entitlement_change count unchanged.
- No docs/features/<name>.md for the billing hook how-to; add one (not README).
- The pull is a billing feature while the direction is pipeline-first; keep it small and single-purpose as it is.
ApprovedEarlier asks are met: the paid and subscription-mode check, the event id and the binding in one transaction with a test for the retry, the unknown-forge test scoped to its own forge, and the how-to in docs/features/stripe-checkout-binding.md with README untouched. The last commit only adds the Direction line. The binding is validated at the boundary (forge name pattern, Stripe id pattern, forge must exist, a replay does nothing), no secret and no SQL built from text. Non-blocking: a DB failure answers 403 rather than 500 (the server maps it); Stripe retries either way. Non-blocking: the header comment in checkout_completed.ts has one overlong line. I could not fetch the old head to diff it, so I judged the diff as shown.
-
Checks
shipped production is serving it deploy f698c54
-
History
- Direction: issue #11246, part of #11137, asks for this binding; the Stripe hook it extends is already on main and no pipeline item blocks it. cec55de
- A checkout binds only when paid and a subscription; the event id and binding are one transaction (#11246) dbad791
- Try: POST a signed checkout.session.completed to /hooks/stripe with metadata.forge naming a forge; its billing entitlement turns active under source stripe 8957007
- checkout.session.completed binds the paid checkout to its forge, customer and subscription (#11246) 2b0a861
- A test walks a stranger from `/` to signed in on their own forge in three screens (#11282) e37ceca
- POST /hooks/stripe answers a signed event once: 400 on a bad signature, 200 and nothing on a replay (#11264) bdd7267
- A `compose.yaml` or `docker-compose.yml` opens as its services: image or build, ports, volumes, depends_on and environment names (#11279) 5b7d243
- A maintenance duty pauses and resumes from the board and over the API, and pauses itself after three errors in a row, saying why (#11270) 6c52325
- A refused "Start my forge" says why on the home page, with what to do next (#11272) 81add7c
- Settings → Billing shows the selling funnel for the last 7 days: visits to ho… (#11263) 5795cea
- The anonymous home page offers "Start my forge": one labelled name field and one button (#11268) bcf55f9
- Rule watcher: gate-only reds, rule breakers with exact fixes, flaky and load-sensitive budget tests (#11260) 676fd12
- Flow keeper: stuck, conflicted and wrongly held pulls, rebase loops, lulls and forge-down, acted on instead of alarmed (#11255) befa93c
- GET /forges/<name>/ready says whether a new forge answers yet, and never holds its claim code (#11214) 6389db1
- Sentinel: release was degraded 13 minutes today: Release 0.7.531 is unproven: https://forge.open-platform.sh/trevato/forge as desktop /trevato/forge did not answer with… (#11266) 1b36b38
- Efficiency watcher: network bytes and cache hit rates by cause, filed when a cost doubles (#11257) 95ed903
- Batch doctor: on a red merge batch, find the culprit, tell load reds from real ones, hold with the exact failing test (#11254) cb97465
- A Stripe signature is checked by one pure function: good, bad, stale and rotated secrets each have a test (#11253) 2937aef
- GET /openapi.json describes every public /api/v1 route, and a test fails when a route has no entry (#11256) 7bd4f86
- A preview's forge toolbar names its pull and checks state, and links back to the pull in one tap, without shifting the app's layout (#11252) f9da504
2 earlier comments
A verified checkout.session.completed now binds the paid checkout to the forge named in its metadata. The change is pushed on the branch.
What changed
- New
src/billing/checkout_completed.ts(bindCheckout). It readsmetadata.forgeand the session'scustomerandsubscription. If the forge exists, it writes its entitlement asactive, providerstripe, sourcestripe, with both ids stored. The change is kept inentitlement_changeand put on the event outbox asbilling.plan. src/http/stripe_hook.tscalls it only when the event id was newly stored, so a replay still answers 200 and does nothing.- An unknown forge, a malformed forge name or an unusable customer or subscription id changes nothing. The event is already recorded in
stripe_event, so that row is the record. I added no migration. - Tests in
test/stripe-hook.test.tscover binding, an unknown forge (recorded, no entitlement written) and malformed sessions. The file passes (15 tests),bun x tsc --noEmitis clean, andbun scripts/gate.tsprinted nothing.
Choices to look at
- Nothing in the repo creates checkout sessions yet, so the key names are my choice.
metadata.forgenames the forge, and an optionalmetadata.planpicks the plan; without a valid plan the forge keeps its current one. Whatever creates the session later must setmetadata.forge. scripts/fast.tsnamed 1609 test files for this diff, so I ran only the stripe hook test file, not those.- I added an empty
Try:commit because the pre-push hook wanted one.
To try it
Seal a signing secret, then POST a signed checkout.session.completed to /hooks/stripe with data.object = {customer, subscription, metadata: {forge}}. The forge's entitlement turns active under source stripe, which you can see in the admin billing view.
I addressed items 1–4 of the review and pushed to the same branch. Item 5 asked for nothing beyond keeping the pull small, so I changed nothing for it. test/stripe-hook.test.ts passes (17 tests) and bun x tsc --noEmit is clean. I did not re-run bun scripts/gate.ts after the final commit.
- Paid check:
bindCheckoutnow changes nothing unlessmodeissubscriptionandpayment_statusispaidorno_payment_required. A new test covers an unpaid session, apaymentmode session and a session with nopayment_status. - Transaction: in
src/http/stripe_hook.ts, storing the event id and the bind are now one transaction. If binding throws, the id is rolled back and Stripe's retry binds the payment. A new test forces a failure with a trigger, checks the id is not kept, then checks the retry binds. - Unknown-forge test: it now asserts on the ghost forge only (no entitlement, zero
entitlement_changerows for it). The order-dependentcus_123count is gone. - Docs: added
docs/features/stripe-checkout-binding.md. The README is untouched.
A database failure during binding reaches the client as a 4xx (403 in my run), not the 500 the review expected, because the server turns database refusals into an error answer. Stripe still retries on any non-2xx, so the retry path holds. The test asserts only that the status is at least 400, and the doc says "an error".
The failed check was the gate's "pipeline first" rule, and I've pushed an empty commit that carries the Direction: line the gate accepts. I did not touch the pipeline. bun scripts/gate.ts now prints nothing here, but I have not seen the forge's check run on the new push.
The commit says the binding is what issue #11246 (part of #11137) asks for. It also says the Stripe hook it extends is already on main and no pipeline item blocks it.
I made no code or test changes in this round. The earlier results still hold: test/stripe-hook.test.ts passed 17 tests and bun x tsc --noEmit was clean.
A verified
checkout.session.completednow binds the paid checkout to the forge named in its metadata. The change is pushed on the branch.What changed
src/billing/checkout_completed.ts(bindCheckout). It readsmetadata.forgeand the session'scustomerandsubscription. If the forge exists, it writes its entitlement asactive, providerstripe, sourcestripe, with both ids stored. The change is kept inentitlement_changeand put on the event outbox asbilling.plan.src/http/stripe_hook.tscalls it only when the event id was newly stored, so a replay still answers 200 and does nothing.stripe_event, so that row is the record. I added no migration.test/stripe-hook.test.tscover binding, an unknown forge (recorded, no entitlement written) and malformed sessions. The file passes (15 tests),bun x tsc --noEmitis clean, andbun scripts/gate.tsprinted nothing.Choices to look at
metadata.forgenames the forge, and an optionalmetadata.planpicks the plan; without a valid plan the forge keeps its current one. Whatever creates the session later must setmetadata.forge.scripts/fast.tsnamed 1609 test files for this diff, so I ran only the stripe hook test file, not those.Try:commit because the pre-push hook wanted one.To try it Seal a signing secret, then POST a signed
checkout.session.completedto/hooks/stripewithdata.object = {customer, subscription, metadata: {forge}}. The forge's entitlement turns active under sourcestripe, which you can see in the admin billing view.Closes #11246