Design a Payment System
💳 What is a payment system? It is the layer between a shop's checkout and the outside companies that actually move money: card processors, wallet apps and banks. It takes a payment request, decides which provider should handle it, and keeps track of each payment as it is authorized, captured and maybe refunded. The shop talks to one system; the system talks to many providers.
Interviewers ask this one in a few shapes: "design a payment system", "design a payment orchestrator", or "route payments to providers by payment method". They are the same design. The orchestrator is the entry-point class, routing is how it picks a provider, and the state machine is what keeps every payment honest. We cover all three.
The problem looks like plumbing, but it tests two things interviewers care about a lot. First, a strict state machine: money must never be captured twice or refunded before it was taken. Second, behavior when the outside world misbehaves: a provider that is down, a client that sends the same request twice.
Requirements
"Design a payment system. Merchants accept cards, wallets and bank transfers through several outside providers. Track each payment from the first request to the last refund."
That is a big prompt with almost no rules in it. Every answer below turns into a rule we will code against.
Clarifying Questions
You: What steps does a payment go through?
Interviewer: Two-step, like most online shops. First we authorize: the provider puts a hold on the money. Later we capture: the money actually moves. Before capture, the merchant can cancel and the hold is released. After capture, the merchant can refund, fully or in parts.
You: How do we choose which provider handles a payment?
Interviewer: Each provider supports some payment methods. Providers are added in priority order. A payment goes to the first provider that supports its method. If that provider is down, try the next one. If nobody can take it, the payment fails.
You: Do we call real provider APIs?
Interviewer: No. Treat each provider as an object that is either up or down. When it is up, every call succeeds. When it is down, every call fails. We will flip them up and down in the tests.
You: Once provider A has authorized a payment, can provider B capture it if A goes down?
Interviewer: No. The hold lives at A, and only A can capture it, refund it or release it. If A is down, the call fails and the merchant can try again later.
You: What if the checkout sends the same request twice, say after a network timeout?
Interviewer: Good question. Every create request carries an idempotency key. The same key must give back the same payment, never a second one. If someone reuses a key with a different amount or method, that is a client bug: reject it.
You: Can a payment be refunded more than once?
Interviewer: Yes, in parts, as long as the total never goes above the captured amount. When everything has been refunded, the payment is fully refunded and done.
You: Currencies, fees, fraud checks?
Interviewer: One currency, amounts in whole cents. No fees and no fraud checks for now.
The fourth answer is the one people miss. It means a payment must remember which provider authorized it, and that capture, refund and cancel never go back through routing. Routing happens exactly once.
💡 Interview tip: Ask about retries early. "What happens if the client sends this twice?" is the question that separates a payment design from a to-do list app. Interviewers often wait to see if you raise it yourself.
Final Requirements
- Providers are registered in priority order, each with the payment methods it supports (
CARD,WALLET,BANK_TRANSFER). A provider can go down and come back up. - A client creates a payment with an idempotency key, an amount and a method. Payment ids are
1, 2, 3, .... Creating again with the same key and the same details returns the same id; the same key with different details, or a non-positive amount, is an error. - Authorizing sends the payment to the first provider, in priority order, that supports its method and is up. That provider is remembered. If no provider can take it, the payment fails for good.
- Capture, refund and cancel go to the payment's own provider only. If it is down, the call fails and nothing changes.
- A payment follows a fixed life cycle: created, then authorized, then captured, then partially or fully refunded. A created or authorized payment can be cancelled. Any call that does not fit the current status fails and changes nothing.
- Refunds can be partial. The total refunded can never exceed the amount. Refunding the last cent makes the payment fully refunded.
- Anyone can ask for a payment's status and its provider.
- New routing rules (cheapest provider, best success rate) are likely, so routing must be swappable.
Out of scope
- Real provider APIs, network calls and webhooks (we come back to webhooks in Extensibility).
- Currencies, exchange rates and provider fees.
- Fraud checks, 3-D Secure, card storage.
- Many threads at once (also in Extensibility).