Skip to content

case study

Confirming a payment exactly once for a held seat

My role: I built this in 2026 as a demonstration of how a reservation system confirms a payment exactly once. It runs on invented data against a simulated payment provider, uses no client or employer material, and the source is public.

Scope and constraints

Ownership
A solo demonstration on invented data with a simulated payment provider. Not client or employer work.
Scale
Nine small source files, a simulated provider and 65 tests in 10 files. No dependencies and no network.
Constraints
A seat is never sold twice, a payment is never recorded twice, and a customer who paid too late is flagged for a refund, whatever happens to the provider’s notifications.
Judgment calls
Ask the provider instead of trusting a notification, record in one place, fail closed when payments cannot be recorded, and decide late payments by the provider’s payment time.

What was delivered and checked

  • 65 tests, run with the Node test runner, all pass.
  • A notification is delivered once, twice and five times, delayed, reordered, lost and recovered by the poller, with an invalid signature, with a changed body, and while the recorder is down.
  • 250 seeded random sequences of 60 steps each (hold, pay, duplicate, lose, delay, expire, reconcile, recorder outage) check after every step that no seat is confirmed twice, every confirmed reservation has exactly one recorded payment, no seat is both held and confirmed, and no reservation is confirmed for a payment made after its hold expired.
  • A register of twelve failure modes is read by a test that fails if a named guard no longer exists.

What it is

A small Node demonstration of how a reservation system can hold a seat while a customer pays through a payment provider, and then confirm that payment exactly once, even when the provider's notifications are missed, repeated, late or out of order. The seats, holds and payments are invented, and the payment provider is simulated.

The system only learns about a payment from a notification, and notifications are not reliable. What it has to end up with is a state a customer would accept: a seat is not sold twice, a payment is not recorded twice, a paid customer is not left without a seat when the system could have known, and a customer who paid too late is flagged for a refund.

The path of one payment

  1. HoldThe seat goes from free to held, with an expiry time. The free check and the hold are one step, so two callers cannot both win.
  2. PayThe customer pays on the provider’s side. The system only learns about it from a notification.
  3. Notify or pollA notification and a poller both end in the same function. A notification can be lost, repeated, late or out of order.
  4. Record onceThat function asks the provider for the payment’s status, then records the payment once.
  5. Expire or confirmPaid before the expiry time and still holding the seat: confirmed. Otherwise: late payment, refund required.
Both the notification handler and the poller end in the same recording function, which asks the provider before it records anything.
Illustration with invented sample data. Times are simulated milliseconds, and the hold lasts 1000. The payment is judged by when the provider says it was paid, not by when the system heard about it.

What the tests throw at it

The simulated provider can lose a notification, repeat it, delay it, send two out of order or sign one wrongly, and the tests use each of those. The recorder can also be switched off.

Illustration with invented sample data. Each row is a case the test suite injects; the simulated provider behaves as the test tells it to.

Decisions

  • A hold with an expiry

    A held seat cannot be taken by anyone else, and it goes back to free when its time runs out. Any other move between states throws. The cost is that a slow payer can lose the seat and is then flagged for a refund.

  • One recording function that asks the provider

    recordPayment is the only place a payment is recorded. It looks up the status with the provider instead of trusting what a notification says, so the number, order and timing of notifications cannot change the outcome. A notification is only a prompt to look. The cost is a status query each time it runs for a payment with no record, and the store refuses a second record for the same payment as a second line of defense.

  • A rule for a late payment

    A payment is in time if the provider says it was made before the hold’s expiry time and the reservation still holds the seat. Otherwise it is recorded as late payment, refund required, and it never confirms a seat that was released or is now held by someone else. The cost is that a customer who paid in time but whose hold was already released gets a refund flag, not a seat.

  • Fail closed

    Checkout checks that payments can be recorded before it holds a seat. If they cannot, it refuses with a clear message and takes no payment. The cost is that checkout is unavailable while the recorder is down, even if the provider is fine.

  • A transport boundary at the network edge

    Every message to or from the provider goes through an injected in-memory transport that records its destination. Tests assert that nothing was addressed outside the allowed in-memory set, and a source scan finds no network module. The cost is that this shows where traffic went, not how a real network behaves.

  • The poller covers expired holds too

    Reconciliation looks at every payment with no record, including holds that already ended, because a customer can pay for a hold that expired and that payment still has to be found. The expiry job also asks about a payment before it releases a hold, so a lost notification does not cost a paid customer the seat.

Scope and limits

What this does not show:

  • No real payment provider

    The simulated provider behaves as the tests tell it to. It shows that the design copes with the listed behaviors, not that a real provider behaves like that.

  • No persistence

    The store is in memory, and "the recorder is unavailable" is a switch, not a real outage.

  • No multi-process concurrency

    Interleavings are simulated in one process by yielding at await points, and the hold is one synchronous step. A shared database would need a real uniqueness constraint or lock.

  • Simulated time and flagged refunds

    Clock skew between the provider and the system is not modeled, and the provider’s payment time is taken as correct. Refunds are flagged, not issued.

  • Evidence, not proof

    The random sequences are seeded and finite, so passing them is evidence, not proof. The failure register test proves a guard exists, not that it works.

Source and tests

The source and the tests are in the hold-and-confirm repository, under the MIT license. It needs Node.js 20 or later and has no install step; npm test runs the tests.

Stack

  • Node.js
  • JavaScript
  • Node test runner

More case studies

    Engineering notes: the decisions behind the work