From 15a822f392b87cee05241be5de5f07ef68885624 Mon Sep 17 00:00:00 2001 From: Matthew Yates <17630073+mtyates@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:47:42 -0400 Subject: [PATCH] Compute order totals in integer cents Co-Authored-By: Claude Opus 5.5 --- src/data/store.ts | 8 ++++---- src/domain/pricing.ts | 18 ++++++++++++++---- test/checkout.test.ts | 4 ++-- test/orders.test.ts | 10 +++++----- test/pricing.test.ts | 11 ++++++++--- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/data/store.ts b/src/data/store.ts index 7dd725a..137e75d 100644 --- a/src/data/store.ts +++ b/src/data/store.ts @@ -125,26 +125,26 @@ export function createStore(): Store { { id: 'li_2', productId: 'prod_shears', quantity: 1, unitPrice: 19.99 }, ], shippingAddress: seedAddress, paymentId: 'pay_1001', - payment: { id: 'pay_1001', status: 'CAPTURED', method: 'card', last4: '4242', amount: 47.62, capturedAt: '2026-09-02T15:04:05.000Z' }, + payment: { id: 'pay_1001', status: 'CAPTURED', method: 'card', last4: '4242', amount: 4762, capturedAt: '2026-09-02T15:04:05.000Z' }, }, { id: 'ord_1002', number: 'TH-1002', customerId: 'cus_avery', status: 'FULFILLED', createdAt: '2026-09-14T18:30:00.000Z', items: [{ id: 'li_3', productId: 'prod_terrarium', quantity: 1, unitPrice: 38.5 }], shippingAddress: seedAddress, paymentId: 'pay_1002', - payment: { id: 'pay_1002', status: 'CAPTURED', method: 'card', last4: '1881', amount: 41.68, capturedAt: '2026-09-14T18:30:04.000Z' }, + payment: { id: 'pay_1002', status: 'CAPTURED', method: 'card', last4: '1881', amount: 4168, capturedAt: '2026-09-14T18:30:04.000Z' }, }, { id: 'ord_1003', number: 'TH-1003', customerId: 'cus_jordan', status: 'PENDING', createdAt: '2026-10-20T09:12:00.000Z', items: [{ id: 'li_4', productId: 'prod_tray', quantity: 2, unitPrice: 12.75 }], shippingAddress: { ...seedAddress, line1: '18 Birch Court', city: 'Seattle', region: 'WA', postalCode: '98101' }, paymentId: 'pay_1003', - payment: { id: 'pay_1003', status: 'AUTHORIZED', method: 'card', last4: '0005', amount: 27.6 }, + payment: { id: 'pay_1003', status: 'AUTHORIZED', method: 'card', last4: '0005', amount: 2760 }, }, { id: 'ord_1004', number: 'TH-1004', customerId: 'cus_jordan', status: 'REFUNDED', createdAt: '2026-08-28T12:00:00.000Z', items: [{ id: 'li_5', productId: 'prod_can', quantity: 1, unitPrice: 29.0 }], paymentId: 'pay_1004', - payment: { id: 'pay_1004', status: 'REFUNDED', method: 'paypal', amount: 31.39, capturedAt: '2026-08-28T12:00:03.000Z' }, + payment: { id: 'pay_1004', status: 'REFUNDED', method: 'paypal', amount: 3139, capturedAt: '2026-08-28T12:00:03.000Z' }, }, ]; for (const { payment, ...order } of orders) { diff --git a/src/domain/pricing.ts b/src/domain/pricing.ts index bad63c4..d1bef60 100644 --- a/src/domain/pricing.ts +++ b/src/domain/pricing.ts @@ -16,13 +16,23 @@ export function roundCurrency(amount: number): number { return Math.round((amount + Number.EPSILON) * 100) / 100; } +/** Converts a decimal USD amount to integer cents. */ +export function toCents(amount: number): number { + return Math.round(amount * 100); +} + export function lineTotal(line: PricedLine): number { return roundCurrency(line.unitPrice * line.quantity); } -/** Computes subtotal, tax, and total in USD for a set of priced lines. */ +/** + * Computes subtotal, tax, and total for a set of priced lines. + * + * All arithmetic is done in integer cents so totals never drift from + * floating-point rounding (e.g. 0.1 + 0.2). + */ export function computeTotals(lines: PricedLine[]): OrderTotals { - const subtotal = roundCurrency(lines.reduce((sum, line) => sum + line.unitPrice * line.quantity, 0)); - const tax = roundCurrency(subtotal * TAX_RATE); - return { subtotal, tax, total: roundCurrency(subtotal + tax) }; + const subtotal = lines.reduce((sum, line) => sum + toCents(line.unitPrice) * line.quantity, 0); + const tax = Math.round(subtotal * TAX_RATE); + return { subtotal, tax, total: subtotal + tax }; } diff --git a/test/checkout.test.ts b/test/checkout.test.ts index bf01a92..321489f 100644 --- a/test/checkout.test.ts +++ b/test/checkout.test.ts @@ -19,8 +19,8 @@ describe('checkout', () => { input: { cartId: 'cart_avery', email: 'avery@example.com', shippingAddress: address, cardToken: 'tok_visa_4242' }, }); expect(errors).toBeUndefined(); - expect(data.checkout.order).toMatchObject({ number: 'TH-1005', status: 'PAID', total: 73.07 }); - expect(data.checkout.payment).toEqual({ status: 'CAPTURED', last4: '4242', amount: 73.07 }); + expect(data.checkout.order).toMatchObject({ number: 'TH-1005', status: 'PAID', total: 7307 }); + expect(data.checkout.payment).toEqual({ status: 'CAPTURED', last4: '4242', amount: 7307 }); expect(store.carts.get('cart_avery')!.items).toHaveLength(0); }); diff --git a/test/orders.test.ts b/test/orders.test.ts index 486d092..76ac29f 100644 --- a/test/orders.test.ts +++ b/test/orders.test.ts @@ -17,16 +17,16 @@ const ORDER = /* GraphQL */ ` `; describe('orders', () => { - it('resolves totals in dollars', async () => { + it('resolves order totals', async () => { const { execute } = setup(); const { data, errors } = await execute(ORDER, { id: 'ord_1001' }); expect(errors).toBeUndefined(); expect(data.order).toMatchObject({ number: 'TH-1001', status: 'PAID', - subtotal: 43.99, - tax: 3.63, - total: 47.62, + subtotal: 4399, + tax: 363, + total: 4762, currency: 'USD', }); }); @@ -34,7 +34,7 @@ describe('orders', () => { it('captured payment amount matches the order total', async () => { const { execute } = setup(); const { data } = await execute(ORDER, { id: 'ord_1001' }); - expect(data.order.payment).toEqual({ status: 'CAPTURED', method: 'card', last4: '4242', amount: 47.62 }); + expect(data.order.payment).toEqual({ status: 'CAPTURED', method: 'card', last4: '4242', amount: 4762 }); expect(data.order.payment.amount).toBe(data.order.total); }); diff --git a/test/pricing.test.ts b/test/pricing.test.ts index ddfa6ba..fc0c16a 100644 --- a/test/pricing.test.ts +++ b/test/pricing.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { computeTotals, lineTotal, roundCurrency } from '../src/domain/pricing'; +import { computeTotals, lineTotal, roundCurrency, toCents } from '../src/domain/pricing'; describe('pricing', () => { it('rounds to whole cents', () => { @@ -7,13 +7,18 @@ describe('pricing', () => { expect(roundCurrency(1.005)).toBe(1.01); }); - it('computes subtotal, tax, and total in dollars', () => { + it('converts dollars to integer cents', () => { + expect(toCents(19.99)).toBe(1999); + expect(toCents(0.1 + 0.2)).toBe(30); + }); + + it('computes subtotal, tax, and total without rounding drift', () => { expect( computeTotals([ { quantity: 1, unitPrice: 24.0 }, { quantity: 1, unitPrice: 19.99 }, ]), - ).toEqual({ subtotal: 43.99, tax: 3.63, total: 47.62 }); + ).toEqual({ subtotal: 4399, tax: 363, total: 4762 }); }); it('multiplies line totals by quantity', () => {