← Technical notes
Confidential / NDA3 diagrams

Cancel vs a late or duplicate payment confirmation

Provider confirmation arrives asynchronously. It can race a cancel, arrive twice, or never arrive.

TL;DR

NDA note: one locked mark-confirmed path so cancel, a late confirmation, and a background check cannot leave a checkout in two states.

live trace · Cancel races a late confirmationfrom the original diagram
Buyer API Provider Checkout session 01cancel unconfirmed checkout 02was this confirmed? 03not confirmed 04lock, re-check, cancel, release holds 05late confirmation, confirmed 06lock session 07was cancelled, so revive: mark confirmed, restoreorders, commit stock 08duplicate confirmation 09already confirmed, so no-op
  1. 01
    Buyer → APIcancel unconfirmed checkout
  2. 02
    API → Providerwas this confirmed?
  3. 03
    Provider → APInot confirmed
  4. 04
    API → Checkout sessionlock, re-check, cancel, release holds
  5. 05
    Provider → APIlate confirmation, confirmed
  6. 06
    API → Checkout sessionlock session
  7. 07
    API → Checkout sessionwas cancelled, so revive: mark confirmed, restore orders, commit stock
  8. 08
    Provider → APIduplicate confirmation
  9. 09
    API → Checkout sessionalready confirmed, so no-op
01

Background

Provider confirmation arrives asynchronously. A buyer can cancel an unconfirmed checkout at the same moment the provider confirmation is on its way, the confirmation can arrive twice, or it can never arrive.

02

How it works

  • Buyer cancels: before cancelling, the system asks the checkout provider whether the checkout was actually confirmed. If confirmed, it marks the checkout confirmed and rejects the cancel. If the provider cannot be reached, it does not cancel. Otherwise it locks the session, re-checks, cancels the unconfirmed orders and releases the holds.
  • Confirmation arrives: verify the signature, confirm with the provider, lock the session. Already confirmed means do nothing (duplicate). Cancelled means revive: mark confirmed, restore the orders, and commit the stock.
  • Confirmation never arrives: a background job checks pending checkouts with the provider and runs the same mark-confirmed path.
03

Design decisions

  • One locked mark-confirmed path shared by the callback and the background check, so all three triggers serialise on the same row lock.
  • Revive instead of auto-refund when money arrives after a cancel: the checkout was confirmed, so the order should continue.
  • Fail safe on uncertainty: an unreachable provider means don't cancel, because a wrong cancel on a confirmed order is worse than a slightly longer pending state.
  • Re-check after acquiring the lock, because the state may have changed while waiting.
All diagrams

Other views

Tap a table to see what it links to.

CHECKOUT_SESSIONstring statestring pay_statetimestamp cancelled_at
PROVIDER_RECORDstring provider_refstring state
CHILD_ORDER
STOCK_HOLD
  • CHECKOUT_SESSION confirmed by PROVIDER_RECORD 1 → many
  • CHECKOUT_SESSION groups CHILD_ORDER 1 → many
  • CHECKOUT_SESSION owns STOCK_HOLD 1 → many

← Technical notes on the home page