Sub topic 16
Fraud and risk checks
This chapter assumes you know the payment lifecycle, idempotent APIs, and the payment state machine from earlier chapters. It also assumes you can read short Python snippets. Fraud checks decide whether a payment attempt should proceed, be challenged, or be refused, before money moves.
Where risk checks fit
A risk check runs after the API accepts a request and before the service asks the PSP to authorize. Running it earlier saves PSP fees and keeps bad traffic away from the card networks. Running it later lets you use PSP signals such as the AVS result (address match) and the CVC check result.
Most systems check at two points: a fast pre-authorization check, then a re-check on the PSP response.
Kinds of checks
- Rules. Explicit conditions, such as "more than five attempts from one card in ten minutes" or "shipping country differs from card country on orders above a limit". Rules are easy to explain and change quickly.
- Velocity counters. Counts of events per card, device, IP address, or email over a time window.
- Scores from a model. A machine-learning model returns a number, for example 0 to 1. You choose thresholds that map the score to an action.
- Lists. Blocklists for known bad cards or devices, and allowlists for trusted customers.
A simple decision function
The sketch below combines a velocity count and a model score into one decision. The thresholds are examples, not recommendations.
from dataclasses import dataclass
@dataclass
class Signals:
attempts_last_10_min: int
model_score: float # 0.0 (safe) to 1.0 (very risky)
on_blocklist: bool
def decide(s: Signals) -> str:
if s.on_blocklist:
return "decline"
if s.attempts_last_10_min > 5 or s.model_score >= 0.9:
return "decline"
if s.model_score >= 0.6:
return "challenge" # ask for 3-D Secure
if s.model_score >= 0.4:
return "review"
return "allow"
print(decide(Signals(attempts_last_10_min=1, model_score=0.65, on_blocklist=False)))This prints challenge.
Design points
- Record the decision. Store the signals, the score, the rule version, and the outcome with the payment. You need this to explain a decline and to tune rules later.
- Fail in a chosen direction. If the risk service times out, decide ahead of time whether to allow (fail open) or decline (fail closed). Many systems fail open for low amounts and closed for high ones.
- Keep it idempotent. A retry with the same idempotency key must return the stored decision, not run a new check that might give a different answer.
- Measure both errors. A false positive blocks a real customer. A false negative lets fraud through and may later become a chargeback.
Common mistakes
- Treating the risk score as final truth, with no review path for borderline cases.
- Changing a rule without versioning it, which makes past decisions impossible to explain.
- Running slow checks inline, which raises payment latency. Keep the inline path fast and move heavy analysis to asynchronous jobs.
Exercise
A client times out and retries a payment with the same idempotency key. The first attempt was scored 0.65 and sent to 3-D Secure. Why should the retry not run the risk check again?
Show answer
A second check could use changed signals, such as a higher velocity count that now includes the first attempt, and return a different decision. Returning the stored decision for that idempotency key keeps the retry consistent and avoids treating the client's own retry as suspicious activity.