Skip to content

Billing: checkout idempotency, packs paid later, refunds and disputes, narrower problem clearing, plan change email (sc-9146) - #215

Merged
ndbroadbent merged 3 commits into
mainfrom
billing-should-fix
Dec 4, 2026
Merged

ndbroadbent merged 3 commits into
mainfrom
billing-should-fix

Conversation

@ndbroadbent

@ndbroadbent ndbroadbent commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Part of the sc-9146 should-fix list.

  • Checkout idempotency: Team Checkout sends Idempotency-Key: visualci-team-checkout-<org>-<half hour> and an expires_at at the end of the next half hour, so every call in a window is the same request. Two tabs or a double click get one session, not two subscriptions. The webhook's two-subscriptions notice stays as the backstop. One edge case: someone who cancels Team and resubscribes within the same half hour gets the finished session back, and can retry 30 minutes later.
  • async_payment_succeeded: a pack paid by a method that settles later completes Checkout unpaid (ignored, as before) and is now credited when this event arrives, once.
  • invoice.paid clears only the problem it settles: the invoice's own failed payment, plus a failed or unpaid renewal when it's the subscription's invoice. An automatic pack's invoice no longer hides a failed renewal, and a project that left the open source pool keeps its notice. Turning automatic packs on clears only a declined pack or a failed invoice.
  • Plan change email: recorded in the transaction that changes the plan (notify::queue_in). Before, it was queued after the commit, and if queueing failed the email was lost for good, because Stripe's retry found the plan already changed.

Stripe setup: the webhook endpoint must also subscribe to checkout.session.async_payment_succeeded (docs/deploy.md updated). Until it does, nothing changes for card payments.

Tests (app/tests/billing.rs): three of the new tests fail on main (pack paid later, the invoice settling only its own problem, auto packs clearing only their own). The email test guards the dedupe on a replayed event; a failed queue can't be simulated. The checkout tests check that the key and the body are the same across two calls.

  • Refunds and disputes (second commit): terms.md promises that "if a payment is refunded or disputed, the credits it bought are removed", and nothing did it. charge.refunded takes back the refunded share of the pack's credits, charge.dispute.created all of them, and a won dispute gives back whatever it took beyond a refund's share. A charge is traced to its pack through Stripe (the Checkout session for its payment intent, or the automatic pack's invoice). A new table, credit_reversals, records each charge's refunded share and whether a dispute is open, so retries take nothing and a partial refund takes only its part. The balance never goes below zero: credits already spent are forgiven, once, rather than taken from a pack bought later. The Stripe key needs read access to Charges, Checkout Sessions and Invoices; a lookup that fails answers 500, and Stripe retries.

Tests for refunds: a refund taken once, never more than is left, traced to automatic packs, subscriptions skipped, a dispute won and lost, a refund during a dispute.

The Stripe events are on Nathan's Tasks (sc-9384).

  • No Stripe calls in a request (third commit): the API's and MCP's finalize ran the out-of-credits check, whose listener buys an automatic pack, which meant up to 15 Stripe calls inside the request (the test lists them on main). Finalizing now fails the build at once only when no automatic pack could help. When one could, the build stays processing and its job, which checks again before comparing anything, buys the pack.

Still to do in sc-9146: the allowance per billing period (a product question, with the coordinator).


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@ndbroadbent ndbroadbent changed the title Billing: checkout idempotency, packs paid later, narrower problem clearing, plan change email with the change (sc-9146) Billing: checkout idempotency, packs paid later, refunds and disputes, narrower problem clearing, plan change email (sc-9146) Dec 4, 2026
…y by what caused them, plan change email with the change (sc-9146)

- Team Checkout sends an idempotency key per organization and half hour, with the session's expiry fixed to the window,
  so two tabs get one session instead of two subscriptions.
- checkout.session.async_payment_succeeded credits a pack paid by a method that settles later; before, it was never
  credited.
- invoice.paid clears only the problem that invoice caused (its own failed payment; a subscription invoice also the
  failed or unpaid renewal), and turning automatic packs on clears only a declined pack or failed invoice, not a second
  subscription or a project that left the open source pool.
- The plan change email is recorded in the transaction that changes the plan (notify::queue_in): queued after the
  commit, a failure lost it for good, since Stripe's retry found the plan already changed.
…t (sc-9146)

terms.md promises it; nothing did it. charge.refunded takes back the refunded share of the pack's credits,
charge.dispute.created all of them, and a dispute won gives back what it took beyond any refund's share. A charge is
traced to its pack through Stripe (its Checkout session, or its automatic pack's invoice). credit_reversals records
each charge's refunded share and open dispute, so every retry and later event takes only the difference, and credits
already spent are forgiven once instead of taken from a pack bought later.
…matic pack (sc-9146)

The API's and MCP's finalize ran stop_if_out_of_credits, whose CreditsRanOut listener buys an automatic pack: up to
15 Stripe calls inside the request. Finalizing now fails the build at once only when no automatic pack could help;
when one could, the build stays processing and its job, which checks again before comparing anything, buys it.
@ndbroadbent
ndbroadbent merged commit e806aa1 into main Dec 4, 2026
16 checks passed
@ndbroadbent
ndbroadbent deleted the billing-should-fix branch December 4, 2026 17:40
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