Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

System Design (High-Level Design)  Module 27 – Case studies: payments, money and markets

Design an online payment gateway

Design a payment gateway: merchant APIs for orders, payments, captures and refunds, routing across acquiring banks, signed webhooks and card tokens.

  • Advanced
  • 25 minutes
  • Examples run with Python 3.14.8, Pyodide 314.0.7, Node.js 24.21.0 and quickjs 0.32.0
  • By MySmartCoPilot
Free intro, Pro: 6 of 25 minutes free
Free: One API in front of networks that each work differently. With a pass: the other 13 sections and 3 interview answers, about 19 minutes.

What you will learn

  • Design merchant APIs for orders, payments, captures, refunds and webhooks
  • Route payments across methods and acquiring banks by their recent approval rates
  • Isolate card numbers from the merchant's systems with hosted fields and tokens
  • Estimate attempt rates, webhook deliveries and storage for a gateway

Before you start

What you’ll learn · Before you start

What you will learn

  • Design merchant APIs for orders, payments, captures, refunds and webhooks
  • Route payments across methods and acquiring banks by their recent approval rates
  • Isolate card numbers from the merchant's systems with hosted fields and tokens
  • Estimate attempt rates, webhook deliveries and storage for a gateway

Before you start

On this page

A payment gateway lets a merchant accept cards, bank-app payments, net banking and wallets, four kinds of network that each work differently, through one integration. The merchant’s server creates an order, and the shopper pays on a checkout page or card fields that the gateway hosts, so card numbers never touch the merchant. The gateway’s payment service turns each attempt into the request its network expects: a card authorisation through an The bank that a merchant's card payments go through: it connects the merchant, or its gateway, to the card networks, and pays out the captured payments., a request to a bank payment network, or a redirect to net banking. Card payments are authorised first and captured later, and a refund is a new transaction against a captured payment. Every write the merchant sends carries an idempotency key: A unique id that a client chooses once for an operation and sends with every attempt of it. The server stores the result under the key, so a repeat gets the stored result instead of a second charge or write. More in “How distributed systems fail”, every outcome reaches the merchant as a signed webhook that is retried until it is received, and a router spreads payments across acquiring banks by their recent approval rates, moving traffic away from one that starts to fail.

One API in front of networks that each work differently

What a gateway sits between

Merchants are on one side, and on the other are networks that each work differently: card networks reached through acquiring banks, real-time bank payments, net banking and wallets. Each has its own messages, timeouts and ways to fail:

Method What the gateway sends
Cards An authorisation through an acquiring bank, captured later
Bank-app payments A request to the bank payment network
Net banking A redirect to the shopper’s bank
Wallets A request to the wallet provider

The gateway hides them behind one API and one state machine per payment, so a merchant integrates once, whichever methods its shoppers use. It also handles card numbers that nobody else should ever see.

Checkpoint

Check: What the gateway sits between

  1. Question 1 of 2 What does the gateway hide behind its one API?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: Networks that each have their own messages, timeouts and ways to fail

    The gateway turns each attempt into the request its network expects, a card authorisation through an acquiring bank, a request to a bank payment network or a redirect to net banking, behind one API and one state machine per payment.

  2. Question 2 of 2 A shopper types a card number into the gateway's hosted card fields. Which systems receive the number?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Only the gateway's token vault

    The checkout page and card fields are hosted by the gateway, and the fields send the card number only to the token vault. Everything after that carries a token.

Requirements

Merchants create orders, take payments by several methods, capture authorised card payments, refund in full or in part, and receive every outcome as a webhook they can verify. No payment or refund happens twice, whatever is retried. Merchants never store card numbers, and the gateway keeps them in one vault. Payments keep flowing when an acquiring bank degrades, and payouts and the gateway’s books are reconciled every day against the acquirers’ reports. Ask first which payment methods and countries are in scope, whether the gateway holds merchants’ money or only routes it, and how soon merchants must be paid.

Estimates

Assume 20 million payment attempts a day with 12 % of them in the busiest hour, 80 % of attempts succeeding with 3 webhooks each, a 4 KB record per attempt kept in 3 copies, card payments as half of all attempts, and one acquiring bank, carrying 40 % of the card payments, whose approvals fall from 92 % to 60 %. Work out each figure first:

Checkpoint

Check the estimates

  1. Question 1 of 3 A gateway handles 20 million payment attempts a day, and the busiest hour carries 12 % of them. How many attempts a second is that hour?

    Type a number.

    Show the answer: Question 1 of 3

    Answer: 667 attempts a second (anything from 600 to 734 counts)

    20,000,000 × 0.12 = 2.4 million attempts in 3,600 seconds, about 667 a second. Each attempt is several calls: the merchant's request, the network's authorisation, the state change and its events.

  2. Question 2 of 3 At about 667 attempts a second, 80 % succeed, and each success sends 3 webhooks to the merchant. How many webhook deliveries a second is that, before any retry?

    Type a number.

    Show the answer: Question 2 of 3

    Answer: 1600 deliveries a second (anything from 1440 to 1760 counts)

    667 × 0.8 ≈ 533 successes a second; × 3 ≈ 1,600 deliveries a second. Retries to slow or failing endpoints come on top, so the sender is sized for several times this.

  3. Question 3 of 3 Half of the 667 attempts a second are card payments, one acquiring bank carries 40 % of them, and its approval rate falls from 92 % to 60 %. How many extra card payments a minute are declined until routing moves traffic away?

    Type a number.

    A hint

    Find the bank's card payments a second first, then the extra share that is declined.

    Show the answer: Question 3 of 3

    Answer: 2561 payments a minute (anything from 2305 to 2817 counts)

    667 × 0.5 × 0.4 ≈ 133 card payments a second at that bank; 32 % more of them are declined, about 42.7 a second, or about 2,561 a minute. Every minute the router takes to notice costs that many checkouts.

Quantity Result
Attempts a second, busiest hour about 667
Webhook deliveries a second about 1,600
Records a year, in 3 copies about 88 TB
Extra declines a minute, one bank degraded about 2,560

The volume is moderate; correctness and routing are the hard parts.

The first design

The merchant’s server talks to the gateway API, and the shopper talks to the hosted checkout, whose card fields send the card number only to the token vault. The diagram shows every part, top to bottom; only the vault’s box ever holds a card number:

A payment gateway: merchant API and hosted checkout feed a payment service that routes to acquirers and sends signed webhooks back.Merchant's serverShopper's browseror appGateway APIorders, captures, refunds,idempotency keysHosted checkoutcard fields andpayment methodsToken vaultcard numbersnever leave itPayment serviceone state machine perpayment, and an outboxRouterapprovals per acquirer,last 60 secondsAcquiring banks, cardnetworks, bank paymentsWebhook sendersigned and retriedMerchant'swebhook endpointcreate order,capture, refundpaycard numbertokenauthoriseeventsoutcomes

The first design of an online payment gateway

Text description of the diagram

The diagram shows the gateway's parts from top to bottom.

  1. The merchant's server calls the gateway API to create orders, capture payments and refund them, with an idempotency key on every write.
  2. The shopper's browser or app pays on the gateway's hosted checkout, whose card fields send the card number only to the token vault, which never lets it out. The checkout passes a token to the payment service.
  3. The payment service keeps one state machine per payment and an outbox of events. It asks the router where to send each attempt; the router chooses by each acquirer's approvals over the last 60 seconds and sends the authorisation to the acquiring banks, card networks and bank payment networks.
  4. The payment service's events go to the webhook sender, which signs them and retries them; a dashed arrow shows the outcomes reaching the merchant's webhook endpoint.

The payment service receives a token, keeps one state machine per payment and asks the router where to send the authorisation. Every change writes an event to an outbox, and the webhook sender signs each event and delivers it to the merchant’s endpoint until it is acknowledged. Step through one card payment, from the order to the webhook:

Interactive · step by step One card payment through the first design Step through one card payment from the merchant's order to the webhook, and see where the card number goes and where it never goes.
One card payment through the first design, step 8 of 8. Showing Merchant's server, Shopper, Gateway API, Hosted checkout, Payment service (authorised), Token vault, Router, Webhook sender, Acquiring bank, Merchant's endpoint.Merchant's serverShopperGateway APIidempotency keysHosted checkoutcard fieldsPayment serviceauthorisedToken vaultcard numbers stay hereRouterrecent approvalsWebhook sendersigned and retriedAcquiring bankMerchant's endpointsigned webhook

The drawing shows the whole scene; its 8 steps are listed below.

Every step as text
  1. The order. The merchant's server creates an order for 1,000 through the gateway API, with an idempotency key on the request.
  2. The shopper pays. The shopper pays on the checkout page that the gateway hosts, with card fields that the gateway serves.
  3. The card number. The card fields send the card number only to the token vault. The merchant's pages and servers never see it.
  4. A token instead. The payment service receives a token, not the number, and starts the payment's state machine.
  5. Where to send it. The payment service asks the router which acquiring bank to use. The router chooses by each bank's recent approval rate.
  6. Authorised. The authorisation goes to the chosen acquiring bank and on to the card's network. The payment is authorised now and captured later.
  7. An event for every change. The change of state writes an event to an outbox, and the webhook sender takes it from there.
  8. The merchant hears. The webhook sender signs the event and delivers it to the merchant's endpoint, retrying until the endpoint acknowledges it.

Checkpoint

Check: The first design: one card payment

  1. Question 1 of 2 In the first design, what does the payment service receive from the hosted checkout instead of the card number?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: A token

    The card fields send the number only to the token vault, and the payment service receives a token, keeps one state machine per payment and asks the router where to send the authorisation.

  2. Question 2 of 2 What does the webhook sender do with each event that a change writes to the outbox?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: Signs it and delivers it to the merchant's endpoint until the endpoint acknowledges it

    Every change writes an event to an outbox, and the webhook sender signs each event and delivers it to the merchant's endpoint until it is acknowledged.

The deep-dive questions

  1. Which resources and states does the merchant API expose, and how is every write safe to retry?
  2. How do authorisation and capture work for cards, and what happens to an authorisation that is never captured?
  3. How are payments routed across acquiring banks, and how does the router notice one that degrades?
  4. How are card numbers kept out of the merchant’s systems and most of the gateway’s?
  5. How do outcomes reach merchants so they can trust them, and how are refunds kept from paying twice?
  6. What breaks at 10× and 100×, and which designs are rejected?

The merchant API moves each payment through a few states, and each write happens once Pro: opens with a Pro, Premium or Ultimate pass

In this part:

  • Resources, states and the moves the API allows
    • Every write safe to repeat, tested with duplicates
  • Authorise, then capture, before the hold lapses
  • Route by the last minute of approvals, and retry only the acquirer’s own failures
  • Card numbers go to one vault, and everything else carries a token
  • Webhooks merchants can trust are signed, deduplicated and never move a payment back
  • Settlement pays each merchant the net, and a chargeback is a workflow of its own
  • At 10× the key store gives way first, and at 100× one payment database
  • Failure drills: five faults the gateway must survive
  • Options considered and rejected
  • What each level covers, and a 45-minute self-mock
    • What each level is expected to cover
    • A 45-minute self-mock
  • Practise: route by the last minute, then the whole design
    • Route card payments by each acquirer's last minute
  • Interview questions
    • One of your three acquiring banks carries 40 % of card payments. Within five minutes its approval rate falls from 92 % to 60 %, then recovers an hour later. Design routing that moves traffic away quickly, recovers the bank's share afterwards, does not swing traffic back and forth, and never charges anyone twice.
    • A merchant wants one-click repeat payments, and its developers plan to keep customers' card numbers, encrypted, in their own database. What do you tell them, what should the merchant and the gateway store instead, and how does the choice change what each of them must protect?
    • A support agent and an automatic returns job refunded the same order within a second of each other, and the customer received twice the amount paid. Both requests had idempotency keys. Explain why the keys did not help, and design refunds so this cannot happen.
  • Key takeaways

5 diagrams · 3 runnable examples (JavaScript, Python) · 1 exercise with sample tests · 3 interview answers · about 50 minutes

Opening this part with your pass…

This part opens with any MySmartCoPilot pass.

  • Pro pass · 1 week₹79$2.99
  • 30 days₹179$5.99
  • 1 year₹1,699$45

One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

Read a complete sample first: Design movie seat booking like BookMyShow

References

Related tools

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.