Repository navigation
fix(refunds): update purchases only after the store refunds, reverse the ledger - #253
Merged
Merged
Conversation
…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>
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.
Bugs fixed
issueRefundandissueManualRefundsetrefunded/partially_refundedbefore calling the marketplace store. If the store call failed, the purchase still showed as refunded, and the "already refunded" check blocked any retry.processMarketplaceRefundupdates the purchase only after the store accepts the refund.processedAtis written last, so a failure in between leaves the refund retryable.refund-<id>), so it can't refund twice.LedgerService.createRefundLedgerEntrieshad no callers, so coach earnings stayed too high after a refund.findByReference('refund', id)).PurchaseFulfillmentService.webhooks.marketplace.tssetrefundedon every refund event. It now comparesmoney.refundedCentswith the purchase amount and setspartially_refundedwhen less was refunded. Older events withoutrefundedCentskeep the old behavior.Wiring
RefundServicetakes two new constructor dependencies, aLedgerServiceand the ledger reader. They're wired inroutes/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.apps/apiVitest: 96 files, 625 passed, 1 skipped.apps/apitsc --noEmit: clean.RefundService.tsdropped from 21 to 16 because its imports were sorted.🤖 Generated with Claude Code