Skip to content

feat(lite): coupon codes - #74

Open
cavewebs wants to merge 1 commit into
mainfrom
claude/lite-coupons
Open

cavewebs wants to merge 1 commit into
mainfrom
claude/lite-coupons

Conversation

@cavewebs

Copy link
Copy Markdown
Contributor

Adds coupon codes to DashCommerce Lite, the sandboxed plugin in the EmDash registry. This is the first item on the plan to bring Lite closer to core's feature set.

What shoppers and merchants get

  • Shoppers enter a code on the cart. A coupon can take:

    • a percentage off the item subtotal,
    • a fixed amount off the item subtotal, or
    • the flat shipping charge off.

    Optional rules: a minimum subtotal, start and end dates (UTC days) and a use limit. One coupon per cart; a new code replaces the old one. Codes are not case sensitive.

  • Merchants get a new Block Kit Coupons page: list, create, edit, turn off and delete. The page shows the use count of a coupon that has a limit. The order page and the CSV export show the coupon code and discount.

How it works

  • Rules come from core. Lite maps its coupon onto core's Coupon and calls validateCoupon and resolveDiscount. These are added to shared.ts, so both plugins apply the same rules.
  • Uses are counted, not stored. A coupon's uses are the orders that carry its code (new couponCode index) with payment status paid or partially-refunded.
    • They're counted with one count call, only when the coupon has a limit.
    • The webhook makes no extra calls.
    • A fully refunded order gives its use back.
    • The limit is checked when the code is applied and again at checkout, so two shoppers paying at the same moment can both get the last use (noted in DESIGN.md).
  • Checkout.
    • The first request re-reads the coupon (and its use count), keeps the result on the cart next to the products it already read, and answers CHECKOUT_CONTINUE.
    • If the coupon was turned off, expired or used up, it's removed and the shopper gets COUPON_INVALID with the updated cart.
    • If the merchant edited the coupon, the new total comes back as CART_CHANGED.
  • Stripe gets the discount as a single-use coupon (core's createOneTimeCoupon: amount_off = cart discount, duration=once). It's created with an idempotency key derived from the checkout attempt, so a double submit reuses it. The hosted page charges exactly the order total, so the webhook's amount check is unchanged. A free-shipping coupon needs no Stripe coupon.
  • Storefront kit: adds applyCoupon and removeCoupon. The cart view gains discountTotal and coupon. Cart.astro gets a coupon field and a discount line (coupons={false} hides the field). The starter's copies are updated with kit:sync.

Sandbox limits

  • Calls per request (max 8, cap 10):
    • A cart with a coupon checks out in 2 requests (6 and 7 calls).
    • A 20-line cart that reads its products one by one takes 6 requests (8, 8, 8, 8, 6, 7). That's within the kit's limit of 8 checkout requests.
    • cart/coupon uses at most 5 calls, cart/coupon/remove 3, and every Coupons admin interaction stays within 8 (all asserted in tests).
  • Bundle: backend.js grows from 60.6 KB to 73.5 KB (cap 128 KB per file; bun run size working budget 96 KB). Total 82.4 KB, 26.5 KB gzipped.
  • No new permissions. The manifest adds a coupons storage collection, two order indexes and the Coupons admin page. emdash-plugin validate passes.

Testing

  • New test/unit/coupons.test.ts, 20 tests:
    • cart pricing (percent, fixed capped at the subtotal, free shipping, flat tax on the discounted subtotal)
    • refusal codes and messages
    • the minimum, and replacing or removing a coupon
    • checkout through the Stripe coupon and a paid webhook with no amount mismatch
    • double-submit idempotency
    • used-up and refunded-order counting
    • a merchant edit giving CART_CHANGED
    • the 20-line cart
    • admin form parsing and create/duplicate/edit/delete
    • call budgets on every path
    • CSV columns
  • New sandbox (workerd) test: creates a coupon through the admin page, applies it, checks out in 2 requests (Stripe coupon then session), pays through the signed webhook, then confirms a second cart is refused once the one use is taken. This exercises the real storage count with an in filter on the new indexes.
  • Results:
    • bun run typecheck, bun run build, bun run test: Lite 164 pass, core 243 pass, 0 fail.
    • Lite: validate, bundle + size, test:sandbox (7/7).
    • bun install --frozen-lockfile and Biome format/lint on the changed files.
  • Not done: no browser run of the updated Cart.astro (the starter's Astro check passes), and no live Stripe test-mode checkout.

Release

Lite is private on npm and released to the registry by hand (PUBLISHING.md), so the Lite version isn't bumped; the changelog entry is under Unreleased. A patch changeset is included for @dashcommerce/starter, whose sandboxed cart component changed.


Generated by Claude Code

Shoppers can enter a coupon code on the cart. A coupon takes a percentage
or a fixed amount off the item subtotal, or removes the flat shipping,
with an optional minimum subtotal, UTC start and end dates and a use
limit. One coupon per cart.

- Coupon rules are core's validateCoupon/resolveDiscount; coupons live in
  a new coupons collection keyed by the upper-cased code.
- Uses are the paid orders that carry the code (new couponCode index),
  counted when needed, so the webhook makes no extra calls and a fully
  refunded order gives its use back.
- New routes cart/coupon and cart/coupon/remove. The cart view gains
  discountTotal and coupon.
- Checkout re-reads the coupon in a request of its own (kept on the cart
  like the products), drops it with COUPON_INVALID if it was turned off,
  expired or used up, and charges the discount through a single-use
  Stripe coupon so the hosted total matches the order. Every request stays
  within 8 bridge calls.
- Orders record couponCode and discountTotal; the order page and CSV
  export show them. New Block Kit Coupons admin page.
- Storefront kit: applyCoupon/removeCoupon, and a coupon field and
  discount line in Cart.astro (copied into the starter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014cPGCx1ERBtVzqTiRTtsJw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants