Pricing taps continuously: design note

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
- Readers decide go/no-go offline: they check the card is genuine and look it up on a local deny list. They then queue taps and upload them when there is signal, so taps can arrive hours late, more than once, or out of order.
- Every tap carries a unique
tapId(device ID plus a sequence number) and a time from a clock synced over GNSS or NTP. We price by tap time, never by arrival time. - The reader encrypts the PAN. “Visible in the banking app” means we have sent an authorisation to the acquirer. When it appears after that is up to the issuer.
- 2,000 buses at around 400 taps each per day is about 800k taps a day, roughly 100 a second at peak. Throughput is not the hard part. Correctness under messy delivery is.
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.
- A late tap off that turns a $7.30 incomplete trip into a $3.25 completed one needs no special code: the new price is lower, so the difference is refunded.
- The same goes for a tap off that arrives before its tap on.
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).
- The version increases only when the card’s priced state changes (a new tap, or a timer closing an open trip), so a replayed tap mints no second charge.
- Writes are conditional on the version, so two re-pricings running at once cannot both commit.
- Deltas go to the payment service through an outbox, so a crash between deciding and sending neither loses nor doubles a charge.
- A card with an open data-integrity anomaly, such as a reused tap ID, is held back until someone reviews it.
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
- Capping, transfers and concession fares: extra rules in the same function, and product decisions to make after the charging path is proven.
- A rider-facing trip history portal.
- Multi-region active-active: taps already wait on the bus, so an outage delays pricing without losing taps.
- ML fraud scoring.
Build order
- Idempotent ingest with tokenisation.
- The trip engine and fare catalogue.
- The ledger, outbox and payment service against an acquirer sandbox.
- Timers for open tap ons.
- The deny-list loop.
- 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.