feat: cart, checkout, sales lifecycle and tickets (#26) #96

Merged
amiryahaya merged 17 commits from feat/26-transactions-checkout into main 2026-10-25 17:47:36 +01:00
Owner

Closes #26.

What

Server-side cart, idempotent checkout, sale lifecycle and tickets (internal/sales, ADR 0011).

  • PUT/GET/DELETE /v1/me/cart; POST /v1/me/checkout (required Idempotency-Key, payment_method wallet|mpesa|card) creates a priced Pending sale with seats held.
  • Status machine pending -> processing -> completed|cancelled, enforced in Go and by DB triggers; every change writes transaction_logs.
  • Complete issues tickets (master + one per person per line) only after completion; Cancel/expire release seats. sales.expire job runs every minute; pending sales expire after sale_pending_minutes (default 10).
  • Free sales (subtotal 0) complete inside checkout. A positive subtotal that rounds to a zero total is refused with 422 ([verify] against legacy).
  • Customer reads: GET /v1/me/transactions (page max 25), /v1/me/transactions/{id}, /v1/me/tickets; owner only, other users' rows 404.
  • No partial payments. waive is refused until #30.

Rulings and follow-ups

  • Complete on a cancelled sale returns the sale with a nil error: callers must check sale.Status; Cancelled means refund (#36).
  • #28: the free-sale check needs changing for a 100% promo. #30: pass fixed non-internal cancel reasons (customers read them), reconcile processing sales never paid, validate payment Reference.
  • Invoice numbers can be burned on replays, 409/503 and invalid keys. GET /v1/me/tickets is unpaged.
  • Deploy: the worker (KWS_MODE=worker) must run before checkout goes live; the app DB role must not be owner/superuser. Migration 0017 is new.

Verification

  • Reviewed per task, then a final dual pass: Pass 1 ponytail (no Must fix) and Pass 2 correctness; fixes re-checked.
  • go test -race ./..., make lint (0 issues), make contract pass.
  • Coverage: 90.0% (make cover, minimum 80%).

🤖 Generated with Claude Code

Closes #26. ## What Server-side cart, idempotent checkout, sale lifecycle and tickets (`internal/sales`, ADR 0011). - `PUT/GET/DELETE /v1/me/cart`; `POST /v1/me/checkout` (required `Idempotency-Key`, `payment_method` wallet|mpesa|card) creates a priced Pending sale with seats held. - Status machine pending -> processing -> completed|cancelled, enforced in Go and by DB triggers; every change writes `transaction_logs`. - Complete issues tickets (master + one per person per line) only after completion; Cancel/expire release seats. `sales.expire` job runs every minute; pending sales expire after `sale_pending_minutes` (default 10). - Free sales (subtotal 0) complete inside checkout. A positive subtotal that rounds to a zero total is refused with 422 (**[verify]** against legacy). - Customer reads: `GET /v1/me/transactions` (page max 25), `/v1/me/transactions/{id}`, `/v1/me/tickets`; owner only, other users' rows 404. - No partial payments. `waive` is refused until #30. ## Rulings and follow-ups - `Complete` on a cancelled sale returns the sale with a nil error: callers must check `sale.Status`; Cancelled means refund (#36). - #28: the free-sale check needs changing for a 100% promo. #30: pass fixed non-internal cancel reasons (customers read them), reconcile processing sales never paid, validate payment Reference. - Invoice numbers can be burned on replays, 409/503 and invalid keys. `GET /v1/me/tickets` is unpaged. - **Deploy:** the worker (`KWS_MODE=worker`) must run before checkout goes live; the app DB role must not be owner/superuser. Migration 0017 is new. ## Verification - Reviewed per task, then a final dual pass: Pass 1 ponytail (no Must fix) and Pass 2 correctness; fixes re-checked. - `go test -race ./...`, `make lint` (0 issues), `make contract` pass. - **Coverage: 90.0%** (`make cover`, minimum 80%). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Migration 0017: one cart per user with its items; ticket sales with a
database-enforced status machine, immutable amounts and identity, and
final timestamps; append-only lines and status logs; tickets insertable
only for a completed sale, never deleted, active -> cancelled only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST /v1/me/checkout prices the cart on the server (pricing.Service.Quote
inside the sale transaction, totals.Calculate), writes a pending sale with
its lines and first log row, takes one seat hold for limited products and
empties the cart. Idempotent by Idempotency-Key (idempotency.Handle) with
transactions.checkout_key as a second net. Adds the sale_pending_minutes
setting; idempotency.Rejection can now carry field errors and Retry-After.
A free sale is handed to a completer; until Task 5 the app wires a stand-in
that refuses it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
After the seat hold and before the cart is deleted, share-lock the unflagged
lines' park products; one flagged meanwhile makes the checkout ErrBusy (503)
so the retry resolves its pool. Pool -> product matches SetSeatPool's order.
Adds a double-click test (one cart, two keys, one sale). Field-error
rejections no longer set a Status they ignore.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
FOR SHARE locks only rows the WHERE clause returns, so filtering on
limited_seats never locked an unflagged product and an in-flight flag
UPDATE slipped past. Lock all of them (ascending id), read the flag and
decide in Go: an in-flight flag now makes checkout wait, then ErrBusy; a
flag issued after the guard waits for the sale.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Completer (Complete, Cancel, MarkProcessing) locks the sale row before
seats, issues the master and one park-entry ticket per person with
128-bit codes, refuses a different amount, and cancels a sale whose
hold was released (ErrSeatsReleased; the caller commits and refunds).
The sales.expire job cancels expired pending sales every minute, one
transaction per sale with SKIP LOCKED; processing sales are exempt.
A free checkout now completes with tickets.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Complete returns the cancelled sale with a nil error when the hold was
released, so a plain InTx commits the cancellation together with the
caller's payment record; callers check sale.Status. MarkProcessing on a
released hold writes nothing and returns ErrSeatsReleased. ExpirePending
keeps a cursor, logs and skips a sale whose cancel fails and returns
the first error after the batch.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ExpirePending counts only cancelled sales against its limit and makes
one ascending pass, so a run always reaches healthy sales past any
number of failing ones; failures are still rolled back, logged, skipped
and the first error returned at the end.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Complete on an already cancelled sale returns it with a nil error, like
a released hold: one rule for callers, check sale.Status. MarkProcessing
refuses a sale past expires_at (ErrSeatsReleased, writes nothing). A
cart with a positive sub total whose grand total rounds to zero is
refused (422) instead of being completed for free.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Migration 0017 (not deployed) gains a BEFORE INSERT guard on
transaction_lines (line_sale_not_pending) and a composite foreign key
tying a park-entry ticket's parent to the same sale.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The three copies of the signed-in check become one package helper;
LogEntry drops ActorID/Reason/RequestID (never filled, never to be
shown to customers); GET /v1/me/transactions caps limit at 25.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
docs(api): checkout 422 names the too-small total (#26)
All checks were successful
ci / changes (pull_request) Successful in 25s
ci / clients (pull_request) Successful in 1m10s
ci / api (pull_request) Successful in 16m12s
ci / ci-gate (pull_request) Successful in 3s
d1eaf5c1a7
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
primatekuntech/kws!96
No description provided.