feat(payments): refund refund_due payments to the customer's wallet (#36, refunds slice) #100

Merged
amiryahaya merged 4 commits from feat/36-refunds into main 2026-10-26 14:23:34 +01:00
Owner

Refunds slice of #36 (does not close it: overstay, vehicle add-on and top-up tickets remain).

What

  • payments.Service.Refund: refunds a refund_due payment to the customer's wallet (payment's currency, wallet created in the same transaction), marks it refunded. Idempotent; ledger key refund:<payment id>.
  • POST /v1/admin/payments/{id}/refund (Idempotency-Key) and GET /v1/admin/payments/refunds-due?limit= (1..200). Permission admin_portal.finance.manage, platform scope only. OpenAPI + live contract tests.
  • Lock order sale -> payment -> wallet (ADR 0012). walletstore.EnsureWalletTx creates the wallet inside the caller's transaction (a separate connection hung the pool under parallel refunds; pinned by a starvation test).
  • ADR 0015, amendments to ADR 0012, spec and CLAUDE.md. Plan: docs/superpowers/plans/2026-10-26-refunds.md.

Rulings and known limits (ADR 0015)

  • Refund goes to the e-wallet only (as legacy did), never back to card or M-Pesa; whole amount only.
  • A POS guest sale has no customer: refused with 409, stays refund_due, refunded outside the system.
  • Refunding a succeeded payment (customer cancellation, tour-operator refunds) is not built; it belongs with #37.
  • List has no cursor; no notification email (#34).

Review

Dual pass done: Pass 1 (ponytail) no Must fix; Pass 2 found a pool-hang in the wallet creation, fixed in 9635bc9, Pass 1 re-check: Ship. Open nit: no direct test that a rolled-back refund leaves no wallet.

Checks

make cover: 89.3% (minimum 80%). go test -race ./..., make lint, make contract green (implementer run). make clients is left to CI.

Deploy

No migration, no new environment variables, no worker change.

🤖 Generated with Claude Code

Refunds slice of #36 (does **not** close it: overstay, vehicle add-on and top-up tickets remain). ## What - `payments.Service.Refund`: refunds a `refund_due` payment to the customer's wallet (payment's currency, wallet created in the same transaction), marks it `refunded`. Idempotent; ledger key `refund:<payment id>`. - `POST /v1/admin/payments/{id}/refund` (Idempotency-Key) and `GET /v1/admin/payments/refunds-due?limit=` (1..200). Permission `admin_portal.finance.manage`, platform scope only. OpenAPI + live contract tests. - Lock order sale -> payment -> wallet (ADR 0012). `walletstore.EnsureWalletTx` creates the wallet inside the caller's transaction (a separate connection hung the pool under parallel refunds; pinned by a starvation test). - ADR 0015, amendments to ADR 0012, spec and CLAUDE.md. Plan: `docs/superpowers/plans/2026-10-26-refunds.md`. ## Rulings and known limits (ADR 0015) - Refund goes to the e-wallet only (as legacy did), never back to card or M-Pesa; whole amount only. - A POS guest sale has no customer: refused with 409, stays `refund_due`, refunded outside the system. - Refunding a `succeeded` payment (customer cancellation, tour-operator refunds) is not built; it belongs with #37. - List has no cursor; no notification email (#34). ## Review Dual pass done: Pass 1 (ponytail) no Must fix; Pass 2 found a pool-hang in the wallet creation, fixed in 9635bc9, Pass 1 re-check: Ship. Open nit: no direct test that a rolled-back refund leaves no wallet. ## Checks `make cover`: **89.3%** (minimum 80%). `go test -race ./...`, `make lint`, `make contract` green (implementer run). `make clients` is left to CI. ## Deploy No migration, no new environment variables, no worker change. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Service.Refund locks the sale, then the payment, credits the payment's
amount to the customer's wallet in its currency (created when missing)
under the ledger key refund:<payment id> and marks it refunded. Guest
sales are refused (ErrNoRecipient); anything not refund_due is
ErrNotRefundable; a refunded payment is returned unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST /v1/admin/payments/{id}/refund and GET /v1/admin/payments/refunds-due,
guarded by admin_portal.finance.manage at platform scope (the waive check,
generalised to take the permission). OpenAPI operations, live contract
calls, ADR 0015, and ADR 0012/spec/CLAUDE.md updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix(payments): create the refund wallet in the caller's transaction (#36)
All checks were successful
ci / changes (pull_request) Successful in 12s
ci / clients (pull_request) Successful in 1m1s
ci / api (pull_request) Successful in 18m47s
ci / ci-gate (pull_request) Successful in 14s
9635bc951d
EnsureWallet opened a second pool connection while the refund held the
idempotency transaction and the sale and payment locks, so enough
parallel refunds hung the node. Add walletstore.EnsureWalletTx and use
it in Service.Refund. Make every iteration of the wallet-pay race test
race, drop the unused sales.Owned wrapper on the refunds-due list and
note guest-sale refunds in the spec.

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!100
No description provided.