A card number does not contain money. A generator computes a test identifier; it does not open or fund an account, grant credit or inspect an issuer’s records. The useful engineering question is how an application distinguishes money reserved, money captured and money returned. This guide works through those amounts without pretending to know a bank account’s balance.
For the input-generation boundary, see what card generators actually produce. Here the examples start after an imaginary provider has confirmed an authorization; no generated number is submitted to a real payment system.
Give each payment stage its own meaning
Authorization approves a request under the issuer/provider’s rules and can reserve an amount. Capture submits the authorized amount, or an allowed part of it, for collection. Clearing and settlement concern subsequent processing and movement of funds between payment participants. Payout is the provider’s transfer to the merchant’s bank account. These are not interchangeable statuses.
Stripe documents manual capture, authorization expiry and release of the unused amount after a final partial capture in Place a hold on a payment method1. Validity windows and support depend on the method and transaction. Some integrations support incremental authorization, overcapture or multiple captures; the example below deliberately does not.
A higher final restaurant charge is not explained by “capturing less.” Tips and increased amounts need the relevant supported authorization/capture flow. Likewise, a pending entry disappearing does not by itself identify which provider event occurred; reconcile the payment object and its history.
Work through 100 EUR without inventing an account balance
The fixture uses cents: 10000 represents €100.00. It models one authorization, one final capture and refunds against that captured amount. Each row replays the stated event history from the same starting authorization.
| Case | Input / condition | Expected result |
|---|---|---|
| balance-authorized | 100 EUR authorized; nothing captured | {"authorized":10000,"captured":0,"held":10000,"netCaptured":0,"refundPending":0,"refunded":0,"released":0} |
| balance-captured | Final capture of 80 EUR releases the remaining 20 EUR | {"authorized":10000,"captured":8000,"held":0,"netCaptured":8000,"refundPending":0,"refunded":0,"released":2000} |
| balance-refund-pending | 30 EUR refund requested; not yet completed | {"authorized":10000,"captured":8000,"held":0,"netCaptured":8000,"refundPending":3000,"refunded":0,"released":2000} |
| balance-refunded | 30 EUR refund completed | {"authorized":10000,"captured":8000,"held":0,"netCaptured":5000,"refundPending":0,"refunded":3000,"released":2000} |
| balance-duplicate | Repeated capture and refund events do not change totals | {"authorized":10000,"captured":8000,"held":0,"netCaptured":5000,"refundPending":0,"refunded":3000,"released":2000} |
| balance-expired | Authorization expires without capture | {"authorized":10000,"captured":0,"held":0,"netCaptured":0,"refundPending":0,"refunded":0,"released":10000} |
After the €80 capture, €20 of the original hold is released. A €30 pending refund reserves refund capacity in this model but does not yet reduce confirmed net capture. Once it succeeds, net capture is €50. That figure is not the shopper’s available bank balance or the merchant’s payout: other purchases, holds, fees, reserves and settlement timing are outside this ledger.
A refund can remain pending or fail. Do not tell the customer it has arrived merely because your request was accepted. Use the provider’s refund state and explain the relevant timing; see Stripe refund states2.
Make retries harmless and contradictory events visible
The runnable ledger keeps capture and refund amounts in integer minor units. It records event IDs, checks that a repeated ID has the same normalized content, and also binds each refund ID to its amount and state. Two notifications about the same completed refund cannot subtract it twice.
test('Pending refunds reserve capacity without reducing confirmed net capture', () => {
const pendingState = replayLedger(10000, [capture,pending]);
assert.equal(pendingState.netCaptured, 8000);
assert.equal(pendingState.refundPending, 3000);
const completed = replayLedger(10000, [capture,pending,success]);
assert.equal(completed.netCaptured, 5000);
assert.equal(completed.refundPending, 0);
assert.deepEqual(replayLedger(10000, [capture,capture,pending,success,success]), completed);
assert.throws(() => replayLedger(10000, [capture,pending,
{id:'second',type:'refund-pending',refundId:'refund-b',amountMinor:6000}]));
});Additional tests reject capture above the hold under this restricted policy, a second final capture, refunds exceeding unreserved captured funds and conflicting terminal refund states. The invariants are held + captured + released = authorized and netCaptured + refunded = captured. A failed pending refund releases its reservation, allowing a separately identified retry.
The implementation is included as replayLedger in regional-guide-core.mjs. Its in-memory maps demonstrate decisions; they are not durable idempotency storage. Real consumers must verify provider messages, match merchant/payment/currency/amount, persist changes atomically and reconcile out-of-order events with provider state. The Stripe webhook example covers that transport and storage boundary.
Keep bank balances and test fixtures out of each other’s contracts
A debit card may reference deposit funds, a credit card an available credit facility and a prepaid product a stored-value balance. Product and issuer records determine those amounts. Neither the length of a number nor a passing checksum reveals them. The card validator therefore reports local input properties, not spending power.
Provider sandbox balances and transaction simulations are also test state. Use the documented test-card reference when a provider requires a particular scenario. Do not advertise an offline fixture as a funded card, or treat a matching local profile as evidence of account ownership.
Reproduce the ledger
Extract the complete runnable ledger project and use Node.js 22 or later:
node guide-demo.mjs balance-ledger
node --test guide-examples.test.mjs regional-guide-examples.test.mjs
The table is generated from the same fixture records those tests execute. Add your provider’s actual authorization windows and refund states in a separate adapter, preserving the distinction between a deterministic teaching model and provider-confirmed financial state.