Skip to content

fix(refunds): update purchases only after the store refunds, reverse the ledger - #253

Merged
rvegajr merged 1 commit into
developfrom
fix/refund-correctness
Oct 15, 2026
Merged

rvegajr merged 1 commit into
developfrom
fix/refund-correctness

Conversation

@rvegajr

@rvegajr rvegajr commented Oct 12, 2026

Copy link
Copy Markdown
Member

Bugs fixed

  1. Purchase marked refunded before any money moved. issueRefund and issueManualRefund set refunded / partially_refunded before calling the marketplace store. If the store call failed, the purchase still showed as refunded, and the "already refunded" check blocked any retry.
    • Now processMarketplaceRefund updates the purchase only after the store accepts the refund. processedAt is written last, so a failure in between leaves the refund retryable.
  2. No way to retry a failed refund. A refund left unprocessed by a failed store call can now be retried.
    • The retry reuses the refund's id, which is the store's idempotency key (refund-<id>), so it can't refund twice.
    • A manual retry must use the same amount. A different amount is rejected with "already pending".
  3. Refunds never reversed the ledger. LedgerService.createRefundLedgerEntries had no callers, so coach earnings stayed too high after a refund.
    • It now runs on every successful refund, once per refund id (findByReference('refund', id)).
    • A ledger failure is logged and doesn't fail the refund, because the money has already moved. This matches PurchaseFulfillmentService.
  4. Partial refunds recorded as full. webhooks.marketplace.ts set refunded on every refund event. It now compares money.refundedCents with the purchase amount and sets partially_refunded when less was refunded. Older events without refundedCents keep the old behavior.

Wiring

RefundService takes two new constructor dependencies, a LedgerService and the ledger reader. They're wired in routes/admin.ts, which is the only place it's constructed.

Not covered

Refunds started outside FieldView, such as from the store's own dashboard, still only update the purchase status through the webhook. They don't write ledger rows yet. That needs the per-refund amount from the store's event, and the v1 event contract isn't written down in this repo.

Tests

Written first: 8 of the new tests failed before the fix.

  • RefundService.test.ts: 12 tests covering ordering, partial status, store rejection, ledger writes and idempotency, ledger failure, and the retry rules.
  • webhooks.marketplace.v1.test.ts: added partial and full refund cases.
  • Full apps/api Vitest: 96 files, 625 passed, 1 skipped.
  • apps/api tsc --noEmit: clean.
  • Lint: no new errors in the changed files. RefundService.ts dropped from 21 to 16 because its imports were sorted.

🤖 Generated with Claude Code

…the ledger

- RefundService marked a purchase refunded before calling the marketplace
  store. If the store call failed, the purchase showed as refunded but no
  money went back, and the "already refunded" check blocked a retry. The
  status now changes only after the store accepts the refund.
- A refund left pending by a failed store call can now be retried. The
  retry reuses the refund id, which is the store's idempotency key, so it
  can't refund twice.
- Refunds now write the ledger reversal (createRefundLedgerEntries had no
  callers), once per refund id, so coach earnings drop after a refund.
- The marketplace webhook marked every refund event as a full refund. It
  now uses money.refundedCents to set partially_refunded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rvegajr
rvegajr merged commit e71fe1c into develop Oct 15, 2026
3 checks passed
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.

1 participant