fix(console): guard against duplicate refund and use actual refund amount#28400
Open
PanAchy wants to merge 5 commits into
Open
fix(console): guard against duplicate refund and use actual refund amount#28400PanAchy wants to merge 5 commits into
PanAchy wants to merge 5 commits into
Conversation
…l refund amount Two bugs in the charge.refunded webhook handler: Bug 9: no idempotency check on timeRefunded, so Stripe retries of the same refund event each deduct balance again. Fix: early return if payment.timeRefunded is already set. Bug 10: balance deduction used payment.amount (original charge) instead of amount_refunded (actual refund). A partial refund on a larger charge would over-deduct the workspace balance. Fix: use centsToMicroCents(body.data.object.amount_refunded) instead.
3 tasks
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.
Issue for this PR
Closes #28398
Type of change
What does this PR do?
Fixes two bugs in the
charge.refundedwebhook handler (packages/console/app/src/routes/stripe/webhook.ts):Bug 1 — Partial refunds silently dropped:
The handler set
PaymentTable.timeRefundedon the first refund event and returned early on any subsequent one. Stripe sends a separatecharge.refundedevent per partial refund, so the second partial was silently discarded and the balance never updated.Bug 2 — Wrong amount deducted:
The balance deduction used
payment.amount(the full original charge, in microcents) instead of the actual refund amount from Stripe. For a partial refund this over-deducts.Fix:
Added
amountRefundedtoPaymentTableto track cumulative refunded amount. On each event, computedelta = event.amount_refunded - payment.amountRefunded. Skip ifdelta <= 0(idempotency). Deduct only the delta from balance. SettimeRefundedonly when the cumulative refunded amount reaches the full payment amount.How did you verify your code works?
bun typecheckinpackages/console— no errors.amount_refundedon the StripeChargeobject is cumulative. Delta approach correctly handles both partial and full refunds with idempotency across retries.centsToMicroCentsis already imported and used identically in other handlers in the same file.charge.refundedhandling requires a live DB and Stripe context outside the current test conventions.Screenshots / recordings
If this is a UI change, please include a screenshot or recording.
Checklist
If you do not follow this template your PR will be automatically rejected.