Sub topic 5
Designing the payment API
This chapter assumes you know basic HTTP and REST, and that you can read short Python snippets. The API is the contract between merchants and your system. Mistakes here are hard to undo, because merchants build on every field you publish.
Model actions, not just resources
A payment is not a row that clients edit. It moves through states, so the API should expose the actions that move it. Clients create a payment, then ask for specific actions on it.
| Method and path | Purpose |
|---|---|
| `POST /v1/payments` | Create and authorize a payment |
| `GET /v1/payments/{id}` | Read the current state |
| `POST /v1/payments/{id}/capture` | Capture an authorized payment |
| `POST /v1/payments/{id}/void` | Cancel before capture |
| `POST /v1/payments/{id}/refunds` | Refund after capture |
Avoid PUT or PATCH on status. A client that can write the status directly can skip the state machine you build later.
Request rules
- Amounts are integers in minor units, with a currency code. Send
"amount": 1999, "currency": "USD", never19.99. - Cards are tokens. The request carries a
payment_method_token, never a card number. This keeps your servers out of the heaviest PCI scope. - Every
POSTthat moves money requires anIdempotency-Keyheader. The next chapter covers how the server uses it. - Reject unknown or malformed fields early with a clear error.
SUPPORTED_CURRENCIES = {"USD", "EUR", "GBP"}
def validate_create_payment(body: dict, headers: dict) -> list:
"""Return a list of error messages. An empty list means the request is valid."""
errors = []
if not headers.get("Idempotency-Key"):
errors.append("Idempotency-Key header is required")
amount = body.get("amount")
if not isinstance(amount, int) or isinstance(amount, bool) or amount <= 0:
errors.append("amount must be a positive integer in minor units")
if body.get("currency") not in SUPPORTED_CURRENCIES:
errors.append("currency is missing or unsupported")
if not body.get("payment_method_token"):
errors.append("payment_method_token is required")
return errors
print(validate_create_payment(
{"amount": 1999, "currency": "USD", "payment_method_token": "tok_123"},
{"Idempotency-Key": "order-42-attempt-1"},
))Synchronous or asynchronous responses
Authorization often finishes in under a second, but not always. Some methods, such as 3-D Secure challenges or bank redirects, need minutes. Design for both cases. Return the payment object with its status, and let clients poll GET or receive a webhook (a later chapter) when it changes.
Errors that clients can act on
Use status codes to say who must change what.
400or422: the request is invalid. Do not retry unchanged.409: the idempotency key was reused with a different body.429or503: retry later, with backoff.
A card decline is a valid business outcome, not an HTTP failure. Return a payment with status: "failed" and a stable decline_code field.
Exercise
A merchant sends "amount": 19.99 to your API. Why should the API reject this, and what should the merchant send instead?
Show answer
Decimal numbers can lose precision in JSON parsers and floating-point types. The amount should be the integer 1999 in minor units, together with "currency": "USD".