Skip to content

feat(core): harden refund idempotency - #113

Closed
alexasomba wants to merge 1 commit into
emdashCommerce:mainfrom
alexasomba:feat/provider-refund-idempotency
Closed

alexasomba wants to merge 1 commit into
emdashCommerce:mainfrom
alexasomba:feat/provider-refund-idempotency

Conversation

@alexasomba

Copy link
Copy Markdown

Part of the Flutterwave follow-up in #97. Independent of the split work.

Why

The admin refund route generated a fresh refund:${orderId}:${randomId()} for every click. If the provider accepted the refund but the response was lost, the operator's retry used a new key and could create a second refund. Flutterwave's duplicate guard is a comment tag plus GET /v3/refunds, and that list endpoint is unauthorized on some keys, so the package cannot always catch it.

What

  • Admin route accepts refundRequestId (1–200 letters, digits, :, _, -). If omitted, it still generates one, so existing callers are unchanged.
  • refundOrder looks up the request id before calling the provider. A repeated id returns the recorded refund, with no second provider call and no second email. This is the local ledger.
  • Admin UI generates one id per attempt and sends it. It is kept if the request throws (the provider may have accepted it before the response was lost) and reset after a success, an explicit provider failure, or an edit to the amount/reason.
  • The refund confirmation no longer says "Stripe"; it says "the payment provider".
  • A provider refund that comes back failed now shows an error instead of "Refund issued".

Tests

  • 3 new tests: the same id calls the provider once and returns the same row; a different id is a new refund; an empty id is rejected.
  • 318 core tests pass on upstream/main, 0 failures.
  • Typecheck clean.
  • Changeset: minor.

Notes

  • The ledger is a query, not a new unique index, so no storage migration. Two simultaneous requests with the same id can still race; the provider's idempotency key is the final guard (Stripe's Idempotency-Key, Flutterwave's comment tag).
  • A failed refund is returned as-is on a repeated id. The UI resets the id on an explicit failure so the operator can start a clean attempt.

@alexasomba

Copy link
Copy Markdown
Author

Superseded by #115. No review had started, so the two small safety fixes were combined to reduce the stack.

@alexasomba alexasomba closed this Nov 25, 2026
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