Skip to content

Design a Payment System, stage 4 of 13: model

Which transitions can happen?

An attempt's states are created, processing, succeeded, failed and refunded. Information about it arrives from three directions: the provider's synchronous response, webhooks, and the reconciler. Decide which of these statements about transitions are true.

System so far· 4 parts
123CLIENTBuyer's browserSERVICECheckout APIDATABASEPostgresEXTERNALPayment provider

Select a component to see what it is responsible for and which state it owns.

  1. 1Buyer's browser → Payment provider: Card entry and 3-D Secure in hosted fields
  2. 2Buyer's browser → Checkout API: Pay (Idempotency-Key header); poll status
  3. 3Checkout API → Postgres: Claim attempt; conditional transitions

What you need to know

0 of 2 checks done
  1. A state machine lists which moves are allowed. Two rules make one robust when information arrives from several directions:

    1. Move only on facts. A decline is a fact; a timeout isn't.
    2. Move forward only. Each transition names the state it starts from, so a late or repeated message can't drag the state backwards.

    In SQL, each move is UPDATE … SET status = 'succeeded' WHERE id = $1 AND status = 'processing'. See State machines for business state.

  2. Check

    An attempt is 'succeeded'. A delayed 'processing' webhook for it arrives. With forward-only transitions, what happens?