A checkout amount is an output of server-owned product, quantity, discount, tax, and delivery rules. A browser cart can propose items and quantities, but it cannot certify prices or final totals. Store monetary values in the currency’s defined minor unit where that unit exists, and keep currency attached to every amount. Some currencies have different decimal conventions, so a universal divide-by-one-hundred rule is wrong. Before starting payment, persist an order snapshot that records the accepted line prices, adjustments, currency, and version of the rules used. A later catalog update must not silently rewrite a pending order.
Server-Priced Order Snapshots and Money Units
Working case
A field-equipment store receives a cart with three pressure sleeves at 4,700 minor units each and one inspection seal at 2,200. Delivery adds 900, yielding 17,200 minor units before any separately defined tax or discount. The client posts product IDs and quantities. The server looks up current prices, checks limits and stock, applies its own delivery and tax policies, creates order 47, and records the priced snapshot. If the browser changes a display total to 200, the payment session still uses 17,200. If stock changes before acceptance, the server reprices or rejects and asks for a new confirmation.
Implementation boundary
function orderTotalMinor(lines, shippingMinor) {
return lines.reduce((total, line) => total + line.unitMinor * line.quantity, shippingMinor);
}
console.log(orderTotalMinor([{ unitMinor: 4700, quantity: 3 }, { unitMinor: 2200, quantity: 1 }], 900));
// Output: 17200Validate product identity, quantity bounds, availability, address eligibility, applicable discounts, taxes, and shipping on the server. Make calculation rules explicit and record their version. Use integer minor units or an exact decimal representation appropriate to the currency; never use a binary floating-point sum as an accounting ledger. Check overflow and negative adjustments. Associate a stable order ID with a unique payment attempt and preserve the snapshot after provider handoff. Return the authoritative total for display and require an explicit confirmation when it differs from the cart preview. Treat inventory reservation and payment authorization as separate transitions with expiry and release rules.
Cost and boundaries
Pricing n lines is O(n) plus catalog and policy lookups. Batch fetching avoids n individual database calls, while reserving inventory adds contention around popular stock. Immutable snapshots increase storage by the number of line items but provide an audit trail when catalog prices change. Repricing on every browser refresh can cause contradictory totals; price at a named order transition and define when that quote expires. Measure cart-to-order mismatch, abandoned reprices, rounding corrections, and reservation expiry. A faster client total is useful for preview, but only the server total can be charged.
Failure trace
The browser submits an amount beside its cart and the server forwards it to the payment provider. A modified request reduces a 17,200 order to 200. Recompute from trusted product IDs and rule versions. Another implementation stores 0.1-style floating values, then rounds only after discounts and tax, producing inconsistent totals across languages. Keep exact units and a documented rounding order. Test invalid quantities, missing catalog items, currency mismatch, expired discounts, concurrent stock changes, and a catalog price update after order creation. Reject an amount that cannot be represented safely.
Verification
- Provider amount equals the accepted server snapshot.
- Currency and rounding rules are explicit for each charge.
- Changed browser fields cannot rewrite a priced order.
Practice drill
Create order 47 with the listed products and delivery charge. Compare the persisted snapshot, display total, and provider request in one test. Mutate the browser total and a line price; neither should change the server result. Reprice after a catalog update according to the quote-expiry rule, and confirm an accepted snapshot stays unchanged. Exercise a currency with different minor-unit rules, a zero-quantity line, an out-of-stock line, and an adjustment that would make the payable amount negative.
Decision note
Price on the server and persist one accepted, currency-aware snapshot before payment handoff.
Common Mistakes
- Trusting a client-submitted total.
- Applying one minor-unit convention to every currency.
- Mutating an accepted order when catalog data changes.
Connected lessons
Checkout and Payment State; Hosted Checkout Sessions and Browser Returns; Payment Webhook Reconciliation and Idempotent Fulfillment; Refunds, Reversals, and Payment Ledger State; Transactions and Concurrent Writes; Idempotent Write Requests and Lost Responses; Backend and API Systems.
Apply and check
Build Project: checkout reconciliation and review Web Development: passkey and checkout decisions quiz.
