Design a Card Payment Processor
Take card payments for online stores: ask the card network for approval, capture the money when an order ships, and settle every payment into one clear final state even when the network does not answer.
Problem statement
For this exercise, assume you are building a payment processor that online stores use to take card payments.
When a shopper checks out, the store's checkout sends your processor an order ID, an amount and a card token (a separate card vault has already turned the card number into a token, so you never see card numbers).
Your processor asks the card network to authorize the payment, which holds the amount on the shopper's card, and tells the store whether it was approved or declined. When the store ships the order, it captures the payment for up to the approved amount.
A store can also cancel an approved payment, and an approval that is not captured within 7 days is canceled automatically. The store can look up any payment's status and the history of its changes.
For this exercise, assume the card network offers four calls that each carry a reference you choose for the payment: authorize, capture, void (release an approval) and status.
Sending capture or void twice with the same reference has the same effect as sending it once, but sending authorize twice can place two holds on the shopper's card.
Status tells you whether the network approved or declined an authorization with that reference; if it has no record of the reference 30 seconds after you sent it, that authorization will never happen.
A payment must be authorized at most once for each checkout request, captured at most once and never for more than was approved, and must always end in one clear final state that the store can see. Refunds, disputes, fraud checks, payouts to stores and the card vault are out of scope.
Design the processor, from the store's checkout request to the card network and back.
What to build
- A store can submit a card payment for an order and get back approved, declined or pending; sending the same checkout request again returns the first answer instead of a second payment.
- A store can capture an approved payment once for up to the approved amount, or cancel it; approvals not captured within 7 days are canceled automatically.
- A store can read a payment's current status and the history of its state changes, and a pending payment always reaches approved or declined.
At scale
- For this exercise, assume about 5 million card payments on a normal day and a peak of about 1,000 authorizations per second on the busiest shopping day of the year.
- For this exercise, assume the card network usually answers in about 300 milliseconds, your processor stops waiting for it after 2 seconds, and a store's checkout waits at most 3 seconds for your answer.
- For this exercise, assume about 1 in 1,000 calls to the card network times out.
- For this exercise, assume an acknowledged payment must never be lost, losing one availability zone must not stop the processor, and every payment and its history are kept for 7 years.