Sub topic 1
Clarifying Requirements and Scope
A payment system is easy to describe and hard to bound. "Let merchants take card payments" can mean a two-week integration with a hosted provider or a multi-year build with its own bank connections. This chapter shows how to turn that sentence into a scope you can design against.
Prerequisites
This chapter assumes you know:
- Basic HTTP and REST API concepts.
- Relational database basics, including transactions and unique constraints.
- Distributed systems ideas such as timeouts and retries.
- How to read short Python and SQL snippets.
You do not need payments domain knowledge. Later chapters define the actors and the card payment steps in detail.
Why scope comes first
In most systems a design mistake costs rework. In a payment system it can cost money: a duplicate charge, a lost payout, or a balance that does not add up. Several early decisions are hard to reverse once real money flows:
- Whether you store card data or only hold tokens, which determines your compliance burden.
- Whether you call one payment service provider (PSP) or several.
- Whether you keep your own ledger or rely on the PSP's records.
- Which currencies and payment methods you support.
Settle these before drawing any boxes. A design review that skips them tends to argue about databases while the real disagreement is about what the system is for.
Decide what you are building
The first question is which role your system plays. Two common shapes give very different designs.
| Shape | What it does | Typical owner |
|---|---|---|
| Payment integration | Calls a PSP to charge customers for one business | An online shop or app |
| Payment platform | Accepts payments on behalf of many merchants and moves their money | A PSP, marketplace, or payments team |
This tutorial designs a payment platform that accepts card and similar payments on behalf of merchants. It sits between merchants and external card infrastructure. It must still work when those outside parties are slow, wrong, or unreachable.
Functional requirements
Functional requirements say what the system does. For this design, write them as short, testable statements.
- A merchant can create a payment for an amount in a given currency.
- A payment can be authorized, then captured, or voided before capture.
- A captured payment can be refunded, fully or partly.
- The merchant is told about status changes, by API response and by asynchronous notification.
- Money movement is recorded so that balances and payouts can be explained.
- Finance staff can compare internal records with PSP and bank reports.
Each statement hides questions. For item 3, ask whether partial refunds are allowed more than once, whether there is a refund window, and who pays when a refund is made after a payout. Write the answers down. Unanswered questions become production incidents.
Non-functional requirements
Non-functional requirements describe how well the system behaves. In payments, correctness outranks speed. A useful way to state this is a ranked list.
- Correctness. No lost payments and no double charges, even with retries, crashes, and duplicate messages.
- Durability. Once the system acknowledges a payment, a restart or failover must not erase it.
- Auditability. Every change to money or payment state can be traced to a cause and a time.
- Availability. Merchants can usually create payments, and degraded behavior is defined.
- Latency. Authorization responses are fast enough for a checkout page.
- Security and compliance. Card data is protected and the system's PCI DSS scope is small.
When two goals conflict, the ranking decides. For example, if the database is unreachable, rejecting a request is better than accepting it without recording it. A rejected payment is a nuisance. An unrecorded charge is a financial and legal problem.
Put numbers on them
Vague goals cannot be tested. Ask for figures, even rough ones, and record the assumptions.
| Question | Example assumption | Why it matters |
|---|---|---|
| Average and peak payments per second | 100 average, 1,000 at peak | Sizes the database and queues |
| Acceptable authorization latency | 95% under 2 seconds | Sets timeout budgets for PSP calls |
| Availability target | 99.95% for create-payment | Drives redundancy and failover design |
| Data retention | Seven years for financial records | Affects storage and deletion design |
| Currencies | 20, with 0, 2, and 3 decimal places | Affects money representation |
Peak load deserves special attention. Payment traffic is bursty around sales events and paydays, so a design sized for the average will fail on the day it matters most.
Define the boundaries
A scope statement lists what is in, and also what is out. Writing the exclusions prevents silent growth.
In scope for this design
- Card payments with authorization, capture, void, and refund.
- A public payment API with idempotency.
- An internal payment state machine and double-entry ledger.
- Retries, consistency patterns, and webhooks.
- Tokenization and the architectural effect of PCI DSS.
- Reconciliation, scaling, and monitoring.
Out of scope or treated lightly
- Building a card network or acting as an issuing bank.
- Detailed fraud model design. Fraud checks appear as a step in the flow only.
- Tax, invoicing, and subscription billing logic.
- Legal advice on licensing. Money transmission rules vary by country and need legal review.
Mark the out-of-scope items as "not designed here" rather than "not needed". Someone will ask about subscriptions on day one, and the answer should be that the design leaves room for them.
Build or buy
Few teams should connect to card networks directly. A common decision is to integrate with one or more PSPs, who handle the connection to acquirers and card networks. Compare the options on the dimensions that matter.
| Option | Strength | Cost |
|---|---|---|
| Single PSP, hosted checkout | Fastest, smallest compliance scope | Least control, vendor lock-in |
| Single PSP, own API integration | More control of the user experience | You own state handling and retries |
| Multiple PSPs behind your own layer | Resilience and cost routing | Normalizing differences, more reconciliation |
This chapter's design assumes your own payment layer in front of one or more PSPs. That choice is why the later chapters spend time on state machines, ledgers, and reconciliation. A team using hosted checkout can skip some of them, but it still needs idempotency and reconciliation.
Capture the constraints as a short document
Write the result as a one-page requirements record. Keeping it structured makes review easy and gives tests something to check. This sketch shows the idea in Python.
from dataclasses import dataclass, field
@dataclass
class PaymentScope:
currencies: list[str]
peak_payments_per_second: int
p95_authorization_ms: int
availability_target: float
stores_raw_card_data: bool
in_scope: list[str] = field(default_factory=list)
out_of_scope: list[str] = field(default_factory=list)
def problems(self) -> list[str]:
found = []
if self.stores_raw_card_data:
found.append("raw card storage widens PCI DSS scope; prefer tokens")
if not self.currencies:
found.append("no currencies listed")
if not 0 < self.availability_target < 1:
found.append("availability target must be a fraction such as 0.9995")
if not self.out_of_scope:
found.append("out-of-scope list is empty")
return found
scope = PaymentScope(
currencies=["USD", "EUR", "JPY"],
peak_payments_per_second=1000,
p95_authorization_ms=2000,
availability_target=0.9995,
stores_raw_card_data=False,
in_scope=["authorize", "capture", "void", "refund", "webhooks"],
out_of_scope=["subscriptions", "fraud model design"],
)
print(scope.problems())The check is simple on purpose. Its value is that the requirements become something a reviewer can read and a build can verify.
Common mistakes
- Treating "payment" as one event. A payment passes through authorization, capture, and settlement over days. Requirements must name the step each one covers.
- Assuming the happy path. List what happens on timeout, decline, duplicate request, and partial refund before design starts.
- Ignoring currency differences. Yen has no minor unit and some currencies have three decimal places. Fix the currency list early.
- Leaving compliance for later. Storing card numbers by accident can force a much larger PCI DSS audit. Decide on tokens now.
- Optimizing latency before correctness. A fast system that double-charges has failed.
How scope shapes the rest of the design
Each requirement points to a later chapter. The flow below shows how the decisions in this chapter feed the design.
Summary
You now have a scope for the rest of the tutorial: a payment platform that sits in front of PSPs, supports authorize, capture, void, and refund, and ranks correctness above speed. The requirements are written as testable statements with assumed numbers, and the exclusions are explicit. The next chapter introduces the parties that take part in a card payment.
Exercise
A teammate proposes that the payment service accept a request even when the database is down, and write it to the database later, to keep availability high. Using the ranked requirements in this chapter, should you accept the proposal? Explain.
Show answer
No. Correctness and durability rank above availability. If the service acknowledges a payment it has not recorded, a crash can lose it, or a retry can charge the customer twice. Rejecting the request with a clear error lets the client retry safely later with the same idempotency key. A defined degraded behavior, such as returning a temporary error, is better than an unrecorded acknowledgement.
Exercise
List three questions you would ask about the requirement "A captured payment can be refunded", and say why each matters to the design.
Show answer
Examples: (1) Are partial refunds allowed, and how many? This affects the refund state and the ledger entries, because the refunded total must never exceed the captured amount. (2) Is there a time limit on refunds? This affects validation rules and what the API returns after the window closes. (3) What happens if a refund is requested after the merchant has been paid out? This affects whose balance is debited and whether the merchant's balance can go negative.