What are the typical risks in payment scenarios?
Typical payment risks come down to three causes: a repeated request, concurrency and an uncertain outcome. The defences are an idempotency key, an atomic debit or row lock, and establishing the status by reference. A race is reproduced only with parallel requests and judged by the final state, not the responses; a button disabled on the client is no protection. The key is created once per intent and reused on retries.
QA Fundamentals · Fintech specifics
Two clicks on "Pay" and the card was charged twice. How do you check this?
A user presses "Pay", nothing happens, and they press again. Or the network drops after the request is sent, and the app retries it automatically. Or the user opens the same payment in two tabs. In all three cases two identical requests reach the server, and the question is one: will the system charge the money twice. This lesson is about where the typical risks of payment scenarios come from, what they have in common and how to reproduce them with a test instead of waiting for a user to do it in production.
How it works and why. Most risks come down to three causes. The first is a repeated request (a double click, the Back button, an automatic retry by the client or a gateway). The second is concurrency, when two requests run at the same time and read the same data. The third is an uncertain result, when we do not know whether the request arrived. Each has its own protection: an idempotency key (a repeat of the same intent does not create a second operation), an atomic update or a row lock (the check and the write are not torn apart) and finding out the fact by reference or by reconciliation (instead of an assumption). Protection on the client, for example a disabled button, lowers the chances but guarantees nothing: a request can be sent from another tab, from Postman or by a library retry.
A race is the least obvious, because it is not reproduced by a click, only by two requests at one moment. The classic case: the balance is 100, there are two transfers of 80. Both requests read the balance, both see that 100 is enough, both write 20.
A race of two transfers on one balance
Hence the first conclusion for tests: such an error is not reproduced by sequential requests, parallel ones are needed. The second: the right fix is not "check once more" but to make the check and the charge one atomic step (a conditional update like "decrease the balance if it is not less than the amount", or a row lock in a transaction).
Here are the main risks and how to reproduce them:
| Risk | How to reproduce | What we expect |
|---|---|---|
| Double charge | A double click, a repeat of the same request, the Back button after payment | One operation, one charge, one notification |
| A race | Two parallel requests for a balance that is enough for one | One success, one refusal, the balance is not negative |
| A drop after sending | Offline or a delayed response in DevTools, a provider mock with a timeout | State Pending, not Failed; a repeat does not duplicate |
| Limits | Operations exactly at the limit, 0.01 above it, after many small ones | The boundary is handled as the requirement says, not "roughly" |
| Time zones | An operation at 23:59:59 and at 00:00:01, a clock change, a change of the user's zone | The "day" for a limit is defined unambiguously |
| Currencies | A payment in a currency other than the account's, a rate that changes between steps | The rate and the time it is fixed are defined, the shown amount equals the charged one |
Example: from a duplicate to idempotency. Initially a repeated request creates a second transfer: a test that calls POST /transfers twice with the same body sees two records and two charges. The fix adds an idempotency key: the client generates a unique Idempotency-Key for each intent, and the server stores the result of the first processing. The Idempotency-Key header is described in an IETF draft and supported by many payment APIs, but the exact behaviour depends on the product, so we check it against the contract:
POST /transfers Idempotency-Key: 7c1e-0001
{"from":"acc-1","to":"acc-2","amount":8000,"currency":"UAH"}
→ 201 {"id":"tr-77","status":"completed"}
POST /transfers Idempotency-Key: 7c1e-0001 (same key and body)
→ the response about tr-77, no new operation
POST /transfers Idempotency-Key: 7c1e-0001 (amount: 9000)
→ an error, not a second transfer
There are three checks here, not one: a repeat with the same key and body returns the same response without a new charge; the same key with a different body is rejected, because it is a different intent; a new key means a new operation. A key the client generates for every attempt gives no idempotency, and that is a frequent mistake: the key is created once per intent and repeated on a retry.
A race is checked at the API level, because an interface cannot send two requests at once. Any client with parallel requests will do; here is a sketch with Playwright APIRequestContext (different keys, because the balance is under test, not a repeat):
// balance of acc-1 is 100.00, each transfer is 80.00
const send = () =>
request.post('/transfers', {
headers: { 'Idempotency-Key': crypto.randomUUID() },
data: { from: 'acc-1', to: 'acc-2', amount: 8000, currency: 'UAH' },
});
const res = await Promise.all([send(), send()]);
const codes = res.map((r) => r.status()).sort();
// expected: one success and one refusal for insufficient funds
// (the exact refusal code comes from the API contract)
The test is run several times, because a race does not show up every time. After that you check not only the responses but also the state: the balance is not negative and the journal has exactly one charge.
Limits and alternatives. A parallel test does not prove the absence of a race, it only raises the chance of seeing one; resilience comes from atomic operations in the code, so it helps to read the code or ask developers how it is built. Load tools (k6, JMeter) give more concurrency than one script. For time boundaries you do not wait for midnight but replace the clock (in Playwright that is the Clock API, in API tests a test-environment parameter or the time in the data). For time zones the question is always the same: is a "day" by server time, user time or UTC; on a daylight-saving change a day lasts 23 or 25 hours. For the rate: fixing it at creation gives the user a predictable amount, fixing it at charging is more accurate for the bank, and the requirement must say which applies.
Common mistakes.
- Checking protection only with a disabled button: a server without idempotency will still double a request from Postman or after a retry.
- Testing a race with sequential requests: they always pass correctly.
- Checking only the response: after a duplicate the response is "OK" while the balance was charged twice. Look at the state in the DB or the history.
- Taking a limit boundary "roughly": errors by ±1 kopeck and at the day boundary are the most frequent.
- Setting the system time by hand on a test environment and not restoring it: the test leaves unreliable data for others.
- Assuming a timeout is a failure: see the lesson about payment states.
Check yourself
A test calls POST /transfers twice sequentially for a balance of 100 (80 each): the first succeeds, the second is rejected. Does this prove there is no race?
Answer
No. Sequential requests do not overlap in time, while the error appears when both read the balance before the first one has written the new one. Parallel requests, several runs and a check of the final state are needed: the balance is not negative, there is one charge.
The client generates a new Idempotency-Key on every retry. Does that protect from a duplicate if the first attempt reached the server but the response was lost?
Answer
No. For the server these are two different intents, and it will execute both. The key must be created once per intent (one payment) and repeated in all retries. The test: send two requests with the same key and body and check one operation, then the same key with a different amount and check a refusal.