Back to the live walkthrough

Pricing taps continuously: design note

Architecture

In one line: the reader decides whether to let the passenger on, the cloud works out the price, and the bank only ever receives the difference between what a card’s trips now cost and what we have already charged. Late, repeated and out-of-order taps then stop being special cases.

Assumptions

Key decisions

1. Re-run one pure function. The Part 1 TripPricer returns the same trips whatever order the taps arrive in and however often they repeat. On every new tap, the trip engine re-prices that card’s whole travel period. A card has a handful of taps a day, so this is cheap. It removes the need for a fragile state machine.

2. An open tap on is not yet a charge. In production the pricer takes an asOf time. A tap on still inside the operator’s maximum journey time is OPEN, and the ledger ignores it. A one-off timer (EventBridge Scheduler) fires when that window ends. If no tap off has arrived by then, the trip becomes INCOMPLETE and the maximum fare is charged. If the tap off turns up later, decision 1 refunds the difference.

3. The ledger charges deltas, never totals. For each card and period, the ledger stores what we have priced and what we have already submitted. It emits only the difference, with the idempotency key (operator, cardToken, period, ledgerVersion).

4. Order and dedupe at the edges. Ingest writes each tap with a conditional put on tapId, so a resend does nothing. Kinesis is partitioned by card token, so a card’s taps are processed in order whichever bus sent them.

5. Versioned fares per operator. This is already in the Part 1 code as FareCatalogue and Tariff: each operator’s tariffs carry an effectiveFrom date. A trip is priced with the version in force at its tap-on time, and that version is stored on the trip. Adding a new operator is a configuration change, not a deployment.

6. Quick visibility versus cost. The industry norm is one aggregated charge per day. It is cheaper and it makes capping possible, but the passenger sees nothing until the next morning. What I would build first: authorise each trip when it closes, so a pending amount shows within minutes of tapping off, then make one aggregated capture when the period ends. Later adjustments can then only lower the final amount. Whether our acquirer and the card schemes allow topping up an authorisation across trips needs confirming; I have not verified it. If they don’t, the fallback is to authorise and capture each trip separately, at a higher transaction cost.

7. Declines feed the deny list. On a decline, the card token is pushed to the readers’ deny list as a delta through IoT jobs within minutes; card scheme rules expect a block within about an hour. Retries to recover the debt follow a configuration table per card scheme.

Card data

Only the card data environment ever sees a PAN: the token vault (CloudHSM or KMS), the payment service and the acquirer gateway. Ingest swaps the encrypted PAN for a token straight away, and everything else handles tokens only. That keeps most of the system out of PCI DSS scope. A plain PAN hash has too little entropy to be a safe token, so tokens are random values held in the vault or keyed HMACs computed in the HSM. We store the scheme’s Payment Account Reference where it exists, so a phone and a plastic card for the same account can be linked later.

Left out at first

Build order

  1. Idempotent ingest with tokenisation.
  2. The trip engine and fare catalogue.
  3. The ledger, outbox and payment service against an acquirer sandbox.
  4. Timers for open tap ons.
  5. The deny-list loop.
  6. From day one, a daily reconciliation of taps, trips and charges against the acquirer’s settlement file. That is how we find wrong or duplicate charges before passengers do.