Sub topic 1
Clarifying Requirements and Scope
Prerequisites
Assume you know basic HTTP and REST APIs, relational database transactions and unique constraints, and distributed-system concepts such as timeouts and retries. You should also be able to read short Python and SQL snippets.
Before designing endpoints, define what the payment system guarantees:
- Which payment methods and currencies are supported?
- Does the system authorize and capture immediately, or allow delayed capture?
- Can a merchant partially capture, refund, or void a payment?
- Which party owns fraud decisions, disputes, and compliance duties?
- What must happen if the client times out after the provider accepted the payment?
A useful requirement is: a client retry must not create a second charge.
Who Takes Part in a Card Payment
The merchant accepts payment from the customer. Your platform may coordinate the transaction, while a payment service provider (PSP) connects to financial networks. The acquirer processes transactions for the merchant. The card network routes messages, and the issuer decides whether the customer’s account can fund the transaction.
These actors can report different statuses at different times. Your system must preserve both its internal state and the provider’s external identifiers.
Authorization, Capture, and Settlement
Authorization asks the issuer to approve and reserve funds. Capture tells the payment system to complete the charge. Settlement moves funds through the financial system to the merchant. A payment can therefore be authorized but not yet captured.
| Stage | Meaning | Typical system state |
|---|---|---|
| Authorization | Funds are approved or reserved | authorized |
| Capture | The approved amount is charged | captured |
| Settlement | Funds are transferred between institutions | settled |
Representing Money Correctly
Store amounts as integer minor units, such as 1999 cents, with an explicit currency. Do not use binary floating-point values for financial calculations.
from decimal import Decimal
def to_minor_units(amount: str, exponent: int = 2) -> int:
value = Decimal(amount).scaleb(exponent)
if value != value.to_integral_value():
raise ValueError("too many fractional digits")
return int(value)
assert to_minor_units("19.99") == 1999Designing the Payment API
A create-payment request should include an amount, currency, payment method token, and merchant-defined idempotency key. The key lets the server recognize a retry of the same operation. Return a stable payment identifier and a status such as requires_action, authorized, or failed.
Keep provider details behind the platform API. Store the original request hash so reusing a key with different data can be rejected.
Exercise
Why should a payment API require an idempotency key?
Show answer
It lets the server return the original result after a timeout instead of creating a second charge.