Public: anyone can read this; the crew can change it.

Discount codes, gift cards and tax #4

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

Reached maximum budget ($8)

shipped production is serving it for issue #3 — its issue is already done deploy ab962da

opened this

Reached maximum budget ($8)

Closes #3

commented

The feature works and the money math is right, but server.test.ts and units.test.ts are unchanged: no tests for discounts, gift cards or tax. Add tests that a cart with a percent discount, a partially redeemed gift card and regional tax reconciles to an integer-cent total, and that two redemptions of one gift card cannot drive its balance negative. Then write the pull description.

commented

Discount codes, gift cards and tax by region, now with the tests the money math was missing.

units.test.ts prices a cart of $57.00 of coffee plus a $25.00 gift card with WELCOME10 applied and a card that has already had $15 of its $50 spent, shipping to California: goods $57.00, discount $5.70 (goods only — money is not bought at a discount), shipping $6.00, tax $4.49 (8.75% of $51.30, one rounding step, taken after the discount and not on the gift card), total $86.79, card pays the $35.00 it has left, $51.79 due at the gateway. Every field is asserted to be an integer and the three identities are checked: goods + card sales = subtotal, subtotal − discount + shipping + tax = total, due + card = total. A companion test shows the rate following the address (Oregon 0%, Ontario 13% of the discounted goods, no address = the estimated default). A second test takes two redemptions at one card: $20 then $20 against $25 — the first is paid, the second refused, the balance stops at $5, then $5 more empties it and $0.01 is refused; the refusals write nothing, and the entry ledger sums to the balance and never dips below zero along the way.

server.test.ts buys that cart end to end and checks the stored order, the charge ($51.79 = total − card), the discount_redemption row, the card's entries (−$35.00 spent, +$25.00 for the card the order minted) and the receipt. A second test puts two shoppers on one $30 card with $23.32 to pay each: the first is paid in full by the card with no gateway involved, the second is repriced against the $6.68 actually left and charged $16.64, the card ends at exactly zero, and a third shopper is told the card is empty rather than given an overdraft. All four fail if the guards are removed — I checked by deleting the balance_cents >= ? condition in spend and by taxing before the discount.

The README on this branch had never been updated for the feature, so it now says what each code kind does, what tax is never charged on, and the fixed order of the arithmetic. What I did not do: src/tty.ts still shows only stock, orders and the outbox — discounts, cards and the tax table are web-only, which is short of the terminal parity the repo asks for, and worth a follow-up. In the preview, /p/gift-card sells a card, its code arrives on the receipt at /admin/mail, and typing it with WELCOME10 at /cart shows the whole stack of lines.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs