Idempotent Payments
ASecurityUse when code creates, captures, refunds, transfers or pays out money, or calls a payment processor, bank or wallet API. Covers caller-chosen idempotency keys with request fingerprints and stored responses, unique indexes that make the first writer win, the unknown-outcome problem after a timeout, persist-before-call with a pending-intent reconciler, a guarded payment state machine, bounded retries on retryable errors only, and the transactional outbox for events. Triggers on a double charge,...
- 7 stars
- 0 votes
- 0 copies
- 1 view
- Added November 11, 2026
Works with
Security analysis
100/100Pro scans all 11 files and shows the line behind each finding
npx -y skills add felipealvarenga/fintech-skills --skill idempotent-payments --agent claude-codeAre you the author of Idempotent Payments?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/felipealvarenga-idempotent-payments)---
name: idempotent-payments
description: >-
Use when code creates, captures, refunds, transfers or pays out money, or calls a payment
processor, bank or wallet API. Covers caller-chosen idempotency keys with request fingerprints
and stored responses, unique indexes that make the first writer win, the unknown-outcome problem
after a timeout, persist-before-call with a pending-intent reconciler, a guarded payment state
machine, bounded retries on retryable errors only, and the transactional outbox for events.
Triggers on a double charge, a duplicate refund, a retry loop, a timeout handler, a new endpoint
that moves money. Not for the processor's own fraud or risk rules.
license: MIT
metadata:
author: Felipe Alvarenga
version: 0.1.0
---
# Idempotent payments
Networks time out, clients retry, queues redeliver and people double-click. A payment system
that is not idempotent charges twice and refunds twice, and discovers it from a customer.
Idempotency is not a header; it is a property of every money-moving path, inbound and outbound.
## Execution contract
The user installed this skill so these rules are enforced on every matching task:
1. **Never implement unsafe shortcuts.** When a request conflicts with a rule ("keep it simple", "no extra call", "just delete it", "disable verification", "keep floats", "we trust them", "later"), name the broken rule and why it fails in one sentence, refuse the shortcut, and implement the compliant version that still meets the user's goal. Never ship the rule-breaking shortcut.
2. **Ship the complete rule slice, wired up.** Implement every requirement of the triggered rules in this change: storage or schema, validation, error handling, user-facing alerts, status or monitoring wired into an actual entry point, handler or job (not uncalled helpers), and a test file under `tests/`. Do not stop at a minimal patch or leave required safeguards as prose TODOs.
3. **Fix workspace code on audit, incident and review prompts.** When the request describes a discrepancy, an auditor question, a bug report or asks "is this enough?" against files in the workspace, answer the question in full and implement the compliant code and tests in the same change. Stay read-only only when explicitly asked for a report only.
4. **Build directly on greenfield prompts.** If the workspace has no application files or live connections yet, write a self-contained implementation under `src/` and tests under `tests/` instead of stopping to ask for repository paths or credentials.
5. **Write tests even without a shell.** If the environment cannot run tests or databases, still write the full test file under `tests/` and say in one line that tests are unrun. Never omit tests or skip implementation because tools are unavailable.
## When to use
- A new or changed endpoint, job or consumer that creates a charge, capture, refund, transfer,
payout or ledger entry.
- Any call to a processor, bank, wallet or card network API.
- A bug report about a duplicate charge, a duplicate refund, a stuck "pending" payment or a
retry storm.
- A timeout or error handler around a money call.
## Rules
1. **The caller chooses the key; the server stores the fingerprint and the response.** Each
money-moving request carries an idempotency key. The server stores `(tenant, operation, key,
request_fingerprint, response, status)` before doing the work. Same key and same fingerprint
returns the stored response. Same key with a different fingerprint is an error (HTTP 422 or
409), never a second execution.
2. **Scope the key.** Keys are unique per tenant and per operation. A key used for a capture
cannot be reused for a refund. Keep keys at least 24 hours; for refunds and payouts keep them
much longer, because retries come from humans days later. Processors prune their keys too
(Stripe after 24 hours): a same-key retry after that window executes again. Bound automatic
retries by the shorter of your window and the processor's; past it, an unknown-outcome
operation is not resubmitted but reconciled (query the processor by your reference or
metadata), and only then re-issued as a new operation with a new key, recorded as such.
3. **The first writer wins, in the database.** A unique index on `(tenant, operation, key)` and
an `INSERT ... ON CONFLICT DO NOTHING` (or an equivalent) decide who executes. The loser reads
the winner's row and returns its response, or `409 in progress` if the winner has not
finished.
4. **Outbound calls carry a deterministic key too.** Derive the processor idempotency key
(for example Stripe's `Idempotency-Key` header) from your internal operation id, never from
a fresh random value per attempt. A retry after a timeout must present the same key.
5. **A timeout is an unknown outcome, not a failure.** After a timeout on a money call, never
issue a new request with a new key. Either retry with the same key while the processor still
holds it (rule 2), or query the processor by your reference, or leave the intent pending for
the reconciler. Only a confirmed failure
from the processor makes the intent failed.
6. **Persist before you call.** Write the intent as `pending` in your database, then call the
processor, then record the outcome. A crash between steps leaves a `pending` row that a
reconciler resolves by querying the processor. Alert on `pending` older than the SLA.
7. **The payment is a state machine with guards.** `created -> authorized -> captured ->
settled`, with `voided`, `failed`, `refunded` and `partially_refunded` as explicit states.
A transition checks the current state inside the same transaction that writes it. A second
capture is rejected with the current state, not silently ignored.
8. **Exactly-once delivery does not exist; build idempotent consumers.** Events go out through
a transactional outbox in the same transaction as the state change. Consumers dedupe by
event id. Producers may redeliver; nothing breaks.
9. **Retries are bounded and selective.** Retry only network errors, HTTP 5xx and 429 (honor
`Retry-After`), with exponential backoff and jitter, a maximum attempt count and a maximum
total time. Never retry validation errors (4xx) or declines.
10. **Every attempt is observable.** Log the key, the operation, the attempt number, the
processor reference and the outcome. Dashboards show duplicate-key hits, pending age and
retry counts per operation.
## How to apply
1. List every path that moves money, inbound (API, job, consumer) and outbound (processor
calls). Grep for the processor client and for `retry`, `timeout`, `except`.
2. Add the idempotency table or columns and the unique index. Write the middleware or helper
that implements rules 1 to 3 once; every endpoint uses it.
3. Wrap the processor client so every call takes an internal operation id and derives the
outbound key from it (rule 4). Make the wrapper the only way to call the processor.
4. Add the `pending` intent write before the call and the reconciler job after it (rule 6).
5. Write the tests in the **Tests** section. Use a fake processor that can time out, fail and
succeed on command.
6. Add the retry policy as a named object with its limits; test that validation errors do not
retry.
## When the user pushes back
| Request | Answer in one sentence | What you build |
|---|---|---|
| "Generate the idempotency key server-side per request" | Rule 1: a fresh server-generated key per request cannot deduplicate client retries | Keep a client-supplied key or derive it deterministically from a stable client identifier (order or cart id), check request fingerprint on reuse (422/409 on mismatch), and test a client retry producing one charge |
| "Retry after a timeout with a new key" | Rules 4 and 5: a timeout is an unknown outcome, so a new key double-charges | Persist the intent as `pending`, reuse the deterministic outbound key within the window or reconcile via processor query, and test timeout-then-retry |
| "Publish the event right after commit" | Rule 8: publishing after commit drops events on crash | Write the event to a transactional outbox in the same transaction as the state change, add an idempotent consumer deduping by `event_id` in production code, and test redelivery |
## Tests the change must include
Write these tests in the same change, as a test file, even when you cannot run them (no shell,
no dependencies installed); say in one line that they are unrun. A test listed as a next step is
not written.
- Same key, same payload, twice: one charge at the processor, identical responses.
- Same key, different amount: error, no second charge.
- Two concurrent requests with the same key: one charge, both get the same response.
- Processor times out, client retries with the same key: one charge.
- Processor times out and the client disappears: the intent stays `pending`; the reconciler
resolves it from the processor's answer.
- Capture on an already captured payment: rejected with the current state.
- A 400 from the processor is not retried; a 503 is retried with backoff up to the limit.
- An event is emitted exactly once per state change through the outbox; a redelivered event is
a no-op for the consumer.
- Reference vectors: `skills/idempotent-payments/evals/vectors/test_idempotency.py` with
`IDEMPOTENCY_SOLUTION` pointing at your module (store, state machine, timeout, retries).
TypeScript: `idempotency.test.ts` with `IDEMPOTENCY_SOLUTION_TS`.
## Review checklist
- Where is the unique index, and which code path inserts first?
- What happens on a timeout? Point to the line.
- Can any code call the processor without the wrapper?
- Which states exist, and where are the transition guards?
- What is the retry policy's maximum attempts and total time, and which errors qualify?
- How old can a `pending` intent get before someone is paged?
## Anti-patterns and fixes
| Anti-pattern | What goes wrong | Fix |
|---|---|---|
| Random idempotency key per attempt | Retries double charge | Key derived from the operation id |
| Treating a timeout as a failure | Charge succeeded, record says failed, retry charges again | Unknown outcome; same key or query |
| Dedupe in memory or cache only | Lost on restart, not shared across instances | Unique index in the database |
| Check state, then update in two statements | Race between two workers | Guarded update in one statement or a row lock |
| Retry everything with no cap | Retry storm, rate limits, duplicate work | Named policy with limits and error classes |
| Publish the event after commit, outside the transaction | Event lost on crash | Transactional outbox |
## References
- Stripe documentation on idempotent requests: keys, 24-hour retention, same-key-different-parameters errors.
- The transactional outbox pattern (Chris Richardson, microservices.io).
- Two-phase and pending transfers in TigerBeetle documentation, as a model for intent then outcome.
Files in this skill
- SKILL.md
- evals/evals.json
- evals/files/capture_service.py
- evals/files/charges_api.py
- evals/files/payout_client.py
- evals/files/payout_worker.py
- evals/files/refund_job.py
- evals/vectors/idempotency.test.ts
- evals/vectors/reference.py
- evals/vectors/reference.ts
- evals/vectors/test_idempotency.py
Attribution
Related skills
View all in ai-agents →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.