Repository navigation
feat(discounts): promo codes at checkout and in the admin - #23
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ne off Stripe coupon Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ter by code Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r and email Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ild ticked Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A code that came back applied from an earlier Pay (AC-9) started with an empty text state, so a Pay refusal (AC-4) blanked the field under its message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
log.ts owns the checkout.* events; this was the one written inline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…abel The confirmation page, the account order and the email already use it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lper The refusal shape was declared in ApplyDiscountError, StartCheckoutError, the preview and the evaluation, and the optional minimumGapCents spread was copied three times. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e reads Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
zonedDayStart and zonedDateTimeToUtc carried the same two-pass offset steps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…scount.ts applyLockedCode, wantsCode and the Stripe coupon create leave start-checkout.ts, which keeps the expiry on a failed coupon. AppliedCode now uses DiscountType, the undiscounted snapshot is computed once, and the applied code is a const. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… recompute The status filter already runs on the SQL CASE; deriving it again in toSummary was a second copy that could drift from the filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…elpers, no let Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds the discounts context file and its CLAUDE.md pointer, links it from the root AGENTS.md, notes the promo code in the checkout and orders context, and marks spec 0014 accepted and scope item 14 done. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds promo codes end to end. Admins create and manage codes (percent or fixed amount off the items, with optional dates, use limits and a minimum spend) on new
/admin/discountspages. Customers type one code on/checkout, see the new total right away, and Stripe charges exactly that total through a one off coupon made per order.Why
Implements spec 0014 (Slice 8 in the scope). It also settles two follow ups left open by spec 0002: the discounted total never drops below Stripe's minimum charge (AC-2), and the rule for splitting an order discount across its lines (AC-6). It amends spec 0011 AC-4 and spec 0012 AC-12 so the email and account order page name the code.
Changes
Discount rules (pure, no I/O)
src/lib/discounts/rule.ts: the raw discount, both caps (keep free delivery when that is cheaper for the customer, stay at or above Stripe's minimum), the six refusal reasons in their fixed order, the line split (rounded down, leftover cents to the largest remainders), the derived code status and the label. OneevaluateDiscountfeeds Apply, the prefill and Pay.src/lib/discounts/schema.ts: code format and the sharednormalizeEmail, which the checkout email schema now uses too.snapshotOrdertakes the applied code and writes the split onto the lines.Checkout
applyDiscountCodeaction (read only, public) and aDiscountFieldin the summary: Apply, Remove, Enter runs Apply, a polite live region, and the refusal message linked witharia-describedby.startCheckouttakesdiscountCode. Inside the order transaction it locks the code row after the customer and cart locks, checks the code again against database prices, live settings and the database clock, then writes the order snapshot, the line split and onediscount_redemptionsrow. A refusal returnsdiscount_refused, creates no order and keeps the form.order_<orderId>with our exactamount_off(idempotency keycoupon:<orderId>) and passes it as the session'sdiscounts. If coupon creation fails, the order is expired and the use is freed.checkoutPrefillbrings the code back when the customer returns from Stripe within 24 hours.Use accounting
markExpiredandmarkCancelled(pending path) delete the order's redemption, so every way out ofpending_paymentfrees the held use. Paid orders keep their use through cancel, refund and undo cancel. A pending order older than 35 minutes stops counting.Admin
/admin/discountslist (search, status filter, uses, discount given, newest 200), plus new and edit pages, with a Discounts entry in the admin nav.updateDiscountCodeanddeleteDiscountCodetake the same code row lock. Code, type and value lock after the first use, a stale form is refused, and a used code cannot be deleted (deactivate instead). Every page and action callsrequireAdmin()./admin/orders?discount=<id>filter with a removable chip; the admin order page shows "Discount (CODE, 10%)" linking to the code.src/lib/dates.ts:datetime-localhelpers that read and show dates inSTORE_TIMEZONE.Customer facing rows
/checkout/complete,/account/orders/[number]and the order confirmation email show "Discount (CODE)" with the amount.Logs
discount.created,discount.updated,discount.deleted,checkout.discount_refused,checkout.coupon_failed;checkout.startedgainsdiscountCentsanddiscountCodeId. No email, and never the typed text of a code that does not exist.How to test / verify
pnpm test: rule edges, schemas, dates, snapshot, session params, list params, email render.pnpm db:start, thenpnpm test:db tests/db/discounts-checkout.db.test.ts tests/db/discounts-admin.db.test.ts tests/db/orders.db.test.ts: last use race for total and per email limits, refusal order, lifecycle (expire, cancel, paid kept, restart with a limit of 1), prefill, Pay reading prices, delivery fee, account email and the clock from the database, coupon failure and reuse, refund suggestions, admin create, update, lock, stale and delete.pnpm test:e2e tests/e2e/checkout/discounts.spec.ts tests/e2e/admin/discounts.spec.ts: apply, remove, each visible refusal, Enter runs Apply, Pay refused after deactivation, admin create, edit lock, delete refusal, list and filter, axe and keyboard.Risk & rollout
20261017152429_discount_code_admin: addsnote,created_by_admin_id(NOT NULL, no default) andupdated_by_admin_idtodiscount_codes, two FK indexes, and CHECKs on the code format and note length. Safe only because the table is empty before this ships (no code could be created until now). No data change.discount_redemptionsand callscoupons.createwhen a code is applied. Orders without a code follow the same path as before (no coupon, nodiscountsparam).Residual risks accepted from the review (docs/reviews/2026-10-18-feat-discounts-promo-codes.md), no blockers or majors:
timeoutraise if campaigns grow.clearPendingOrderruns before the code check, so any refused code on Pay already ended the customer's earlier open Stripe session.Notes for reviewers
start-checkout.ts(customer, cart, then code) and the same code lock taken first by admin update and delete, so an edit cannot race a redemption./syncto update in the spec: theorders.discount_code_idindex already existed, and the admin queries file isadmin-queries.ts.discountCode: z.unknown()in the checkout schema (validated later byparseCodeText), andzonedDateTimeToUtcshifting a wall time inside a spring forward gap instead of returning null.🤖 Generated with Claude Code