The price on the server changed: making sure the total on screen is the total charged | jonathanjuliani.dev
Opinion·Nov 13, 2026 · 6 min
The price on the server changed: making sure the total on screen is the total charged
Server-side price validation: the backend owns the price, the front only shows it. A difference is a 409, the customer confirms, nothing charged quietly.
Jonathan Juliani
If the total the customer saw isn't the total you charged, your checkout is wrong. Customers will complain, and you'll lose sales. It doesn't matter which side the error is on: accepting a stale price from the frontend is wrong, and charging the new price without saying anything is wrong too. Server-side price validation is only half the rule. The other half is showing the change before you charge.
The server calculates everything. If the total matches, it charges. If it doesn't, it answers 409 with the repriced order, and charges nothing, and the frontend shows the difference before any confirmation or card prompt. And here we'll also talk about handling money as integer cents, from schema to database. We'll look at this with a code sample on GitHub, a self-service snack-bar kiosk. All the code is there to read, follow, test, and understand.
The basic argument: "the frontend already computes it, the backend just records"
The argument makes sense, so let's be fair to it and understand it. The frontend has to compute: the customer wants to see "$5.50" in the cart without a server round trip on every tap. Recomputing on the backend looks like a waste at first, duplication. But what if the price changed mid-order? (It seems rare, right?), and sometimes the difference is cents. Do you think stopping the sale is worse than charging the new price and continuing?
In practice there are four options. This is the table that decided the project (D6):
Option
Gains
Loses
Trust the client's price
Subscribe to the newsletter
Get new posts and videos by email as soon as they are out.
Reprice and answer 409 only when the total or availability changed
Authority on the server, the promise on screen
One extra round trip for the customer, and it rarely loses to the other options
Point 1: a price from the frontend is a price anyone with DevTools sets
Think of a supermarket register: it scans the barcode, it doesn't ask what the shelf tag said. Where the analogy breaks: in a supermarket the store prints the tag; in your app, the "tag" is JavaScript running on the customer's machine. A proxy or a bug in your own frontend sets the price your backend receives.
That's why the example kiosk's client doesn't send a price at all. So what does it send? It sends what the price is calculated from: what was chosen, how many, and which options. The price comes from the server's menu:
code
// apps/api/src/orders/pricing.ts (excerpt; the options comment is mine)const item = byId.get(line.itemId);if (!item) { issues.push({ path: `lines.${index}.itemId`, message: `Unknown item "${line.itemId}"` }); return;}let unitPriceCents = item.priceCents;// ... exactly one choice per group; each choice adds its deltaCentspriced.push({ /* ... */ unitPriceCents, lineTotalCents: unitPriceCents * line.qty });
An unknown item is a 400; the server doesn't guess. clientTotalCents is only compared, never charged. The rule is central enough to have its own section in the repo's AGENTS.md, the file any person or AI agent reads before touching the code.
Point 2: quietly charging the new price breaks the checkout's only promise
The checkout promises one thing: the number the person saw is the number that leaves the card. On a kiosk with no attendant, for example, there's nobody to explain "the price just changed."
Fifty cents? Nobody will complain.
Maybe they won't. Maybe they will. But they'll remember it on their statement. And the problem isn't only the amount, it's the pattern: if the system charges "whatever it decided" (because it updated a price without telling the customer), the total on screen becomes "just a suggestion" and the person is left wondering whether the number they saw is what will actually leave the card. Trust doesn't survive that.
So in our example kiosk, the API answers 409 with every line repriced and writes nothing (routes.ts#L109-L124). Every line, because the server doesn't know what the client displayed; the diff is the client's job (reduce.ts#L180-L209).
1The customer saw $5.50 and taps Pay. The kiosk sends items, options and the total it showed.
2The API prices from its own menu. Chips went from $2.50 to $3.00.
3The totals differ. The API returns every line repriced and neither writes nor charges anything.Reply
4The kiosk compares with what it showed and displays only what changed.
5The customer taps Pay new total. Now the totals match.
6Only now is there a charge, for the amount the customer confirmed.
The kiosk shows the change before any charge: old price struck through, new total, and the choice stays with the customer.
A lot of people have already seen this JavaScript "bug":
code
0.1 + 0.2; // 0.30000000000000004
Which isn't actually a bug. 0.1 has no exact base-2 representation, the same way 1/3 has none in base 10 (it is 1 part of 3 parts of a whole). And for money it matters, because the checkout compares totals with exact equality: with floats, an "equal" total becomes a phantom 409, or two roundings become a one-cent gap between screen and charge.
In the example kiosk there are no dollars in the code, only integer cents, the smallest unit of the currency, held in three layers:
formatCents uses only an integer value, with no rounding, so sending 5.0001 comes back 400
And because the schema lives in a package both the API and the web app import, changing a money field breaks both typechecks until they agree. For example, if you change the schema to accept cents with 2 decimal places, the typecheck breaks everywhere that schema is used. That's another validation layer, and a project decision that in this case keeps the API and web contracts in agreement.
To add up snacks in this example, integer cents work with zero dependencies. For interest, FX, or a fractional tax rate, we can reach for a library such as decimal.js, or another you prefer, so we don't hit rounding and precision problems or have to maintain and validate rounding and precision by hand.
Another point: the menu version doesn't decide the 409
The easy rule would be "the menu version changed, return 409". In this kiosk example it isn't. If the team sold out the hot dog and you only ordered chips, the menu version you have is stale, but the total is right.
So the test was: a stale version and a correct total come back 201. The version is for caching (ETag/304, menu/routes.ts#L11-L13). In this case, the total decides whether to charge or return 409.
And charging without asking the customer first makes sense to me when the person already agreed to the rule, for example: an announced subscription change, an FX notice, and so on. At a snack-bar counter, a kiosk, or a similar app, the person already agreed to a specific number.
Where the example stops, you can see it in COVERAGE.md: it doesn't include tax or discounts (those and other details would come in as further steps after priceOrder), and the example also has no stock counts, so two kiosks can sell the last hot dog (D19).
Conclusion: the server owns the price, the frontend owns the "yes"
The server is always the authority on the final amount. The frontend is the authority on what the person agreed to pay, and clientTotalCents is the record of that. When the two disagree, we don't go ahead. We check first, and the 409 returns the information and the person decides. All of it in integer cents, which makes the math and the comparisons easier.
To run the example: docker compose up, put some chips in the cart, open /ops → "Raise one item by $0.50", and pay.
When the person already agreed to the rule (this one is closer to a personal opinion)
Next steps
This is one of three posts on the same kiosk code sample. Another post is about the other way to charge wrong, which is charging twice: idempotency keys at checkout. And another post where we'll talk about when the API goes down mid-order.
Does your checkout reprice on the server? And when the total comes out different, does it charge the new one, the old one, or ask?
Do you think stopping a sale over 50 cents is overkill? Make the case in the comments. If I got something wrong, or you see a better way, say so and I'll update the post.