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
01
Buyer → APIcancel unconfirmed checkout
02
API → Providerwas this confirmed?
03
Provider → APInot confirmed
04
API → Checkout sessionlock, re-check, cancel, release holds
05
Provider → APIlate confirmation, confirmed
06
API → Checkout sessionlock session
07
API → Checkout sessionwas cancelled, so revive: mark confirmed, restore orders, commit stock
08
Provider → APIduplicate confirmation
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.