Sub topic 13
Integrating and routing across PSPs
This chapter assumes you know how authorization and capture work, how idempotency keys protect against duplicate charges, and how retries and timeouts behave. Here we look at connecting your system to more than one payment service provider (PSP) and choosing which one handles each payment.
Why use more than one PSP
A single PSP is simple, but it is also a single point of failure. Teams add more PSPs for these reasons:
- Availability: if one PSP is down, traffic can move to another.
- Cost: fees differ by card type, country, and volume.
- Coverage: some PSPs support local payment methods or acquirers that others lack.
- Approval rates: the same card can be approved by one acquirer and declined by another.
Put an adapter in front of each PSP
Every PSP has its own request format, error codes, and status names. Do not let these spread through your code. Define one internal interface and write one adapter per PSP that translates to and from it.
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class AuthResult:
status: str # "authorized", "declined", or "unknown"
psp_reference: str | None
retryable: bool
class PspAdapter(ABC):
name: str
@abstractmethod
def authorize(self, payment_id: str, amount_minor: int,
currency: str, token: str) -> AuthResult: ...
class ExamplePspAdapter(PspAdapter):
name = "example_psp"
def authorize(self, payment_id, amount_minor, currency, token):
# Call the PSP here, passing payment_id as its idempotency key.
# Map PSP-specific codes to the three internal statuses.
return AuthResult("authorized", "ref_123", retryable=False)The adapter maps every PSP error into a small internal set. The most important distinction is between a decline, which is final, and an unknown outcome, such as a timeout, where you must check the result before doing anything else.
Routing rules
A router picks the PSP for each payment. Start with simple, explainable rules and keep them in configuration, not in code.
| Input | Example rule |
|---|---|
| Currency or country | Send EUR payments to the PSP with a local acquirer |
| Card brand | Route a given brand to the cheapest PSP |
| Payment method | Use the PSP that supports the local method |
| Health | Skip a PSP whose recent error rate is high |
Failover without double charging
Failover is the risky part. If a request to PSP A times out, PSP A may still have authorized the card. Sending the same payment to PSP B could then charge the customer twice, and the two PSPs do not share idempotency keys.
Follow these rules:
- Fail over immediately only when you know the request never took effect, for example a connection refused or a clear "service unavailable" response.
- On a timeout, retry the same PSP with the same idempotency key, or query the payment status, until you learn the outcome.
- If you must switch PSPs after an unknown outcome, first void or confirm the first attempt.
- Record which PSP handled each payment, along with its reference, so later captures, refunds, and reconciliation go to the right place.
Keep it observable
Track approval rate, latency, and error rate per PSP, and per card brand and country. Health-based routing depends on these numbers, and they show whether a routing change actually helped.
Exercise
A request to PSP A times out. Your router immediately sends the same payment to PSP B, and both succeed. What went wrong, and what should the router have done?
Show answer
A timeout means the outcome is unknown, and PSP A may have authorized the card. Because the two PSPs do not share idempotency keys, the customer was charged twice. The router should have retried PSP A with the same idempotency key or queried its status, and failed over only after confirming the first attempt did not succeed, or after voiding it.