03 — Engine

What actually exists

Everything on this page is computed at render time by packages/core — the same pure modules checkout will use. 71 unit tests cover them. The storefront you just browsed is mock fixtures; this is the real build so far.


01 · A live quote

Order of operations is fixed: line = unit × qty → subtotal → discount (allocated across lines) → shipping → tax → total.

LineQtyUnitSubtotalDiscountTaxable
li_tee2$65.00$130.00−$26.00$104.00
li_hood1$140.00$140.00−$28.00$112.00
Totals$270.00−$54.00$216.00

Applied: BRUISED20 (value)

pricingHash 14adea7767d34dbdf50f722afbad942d…

The hash fingerprints every input that determines the price. Checkout recomputes it and returns 409 PRICE_CHANGED on mismatch — which is what stops a variant repriced mid-drop from silently charging a stale quote.

02 · Clothing tax thresholds

New York exempts clothing under $110 per item; Massachusetts $175. Stripe recovers the per-unit price by dividing amount ÷ quantity, so both must travel together and both must be post-discount.

Cart lineQtyAmount sentPer unitNY treatment
Rot Tee ×22$130.00$65.00Exempt
Hood 01 ×11$140.00$140.00Taxable
Coach Jacket ×11$200.00$200.00Taxable
Tee ×2 after 20% off2$104.00$52.00Exempt
The bug this prevents. Sending two $65 tees as amount 13000, quantity 1 makes Stripe see a $130 garment and charge NY sales tax on clothing that is individually exempt. And note row 4: a $100 item discounted to $80 crosses below the threshold, so sending gross amounts would tax something exempt at the price actually paid.

Tax lines as sent to Stripe:

[
  {
    "reference": "li_tee",
    "amount": 10400,
    "quantity": 2,
    "taxCode": "txcd_30011000"
  },
  {
    "reference": "li_hood",
    "amount": 11200,
    "quantity": 1,
    "taxCode": "txcd_30011000"
  }
]

03 · Allocation

Largest-remainder, so the parts always sum to the whole. Naive rounding splits 100¢ three ways as 33/33/33 = 99¢ — and a missing cent fails the database CHECK subtotal − discount + shipping + tax = total, so the order cannot be saved at all.

SplitPartsSum
100¢ ÷ 3 equal34 · 33 · 33100¢ ✓
$10 by 65/35 weight650 · 3501000¢ ✓

04 · Order state machine

21 legal transitions. This table generates the Postgres trigger, so the app guard and the database backstop cannot drift — a test regenerates and diffs it. A refund never changes the lifecycle: a delivered, fully refunded order is still delivered.

FromEventTo
pendingCHECKOUT_STARTEDawaiting_payment
pendingRESERVATION_EXPIREDexpired
pendingCANCELLEDcancelled
awaiting_paymentPAYMENT_PROCESSINGprocessing
awaiting_paymentPAYMENT_SUCCEEDEDpaid
awaiting_paymentPAYMENT_FAILEDpayment_failed
awaiting_paymentRESERVATION_EXPIREDexpired
awaiting_paymentCANCELLEDcancelled
processingPAYMENT_SUCCEEDEDpaid
processingPAYMENT_FAILEDpayment_failed
processingAMOUNT_MISMATCHunder_review
processingRESERVATION_EXPIREDexpired
payment_failedPAYMENT_RETRIEDawaiting_payment
payment_failedRESERVATION_EXPIREDexpired
payment_failedCANCELLEDcancelled
under_reviewREVIEW_APPROVEDpaid
under_reviewREVIEW_REJECTEDcancelled
paidFULFILLEDfulfilled
paidCANCELLEDcancelled
fulfilledDELIVEREDdelivered
fulfilledRETURN_RECEIVEDfulfilled
Not built yet: the database is schema-only (34 tables, migration generated, never run against a live Neon branch). No Stripe, no PayPal, no attribution, no admin. The storefront pages are fixtures, not queries.