Feature/add idempotency key support to refund invoice - #314
Merged
levoski1 merged 2 commits intoJul 30, 2026
Merged
Conversation
|
@i-amdaveee Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Summary
Closes #200
Adds
Idempotency-Keyheader support torefund_invoice, and applies the samepattern to
pay_invoiceandcancel_invoiceto prevent double-processing on retries.Problem
backend/src/routes/refund.rshad no idempotency handling. A client retrying atimed-out refund request could cause the same invoice to be refunded twice.
What changed
New file:
backend/src/idempotency.rsDashMapwith configurable TTL (default 24 h)"{endpoint}:{invoice_id}:{idempotency_key}"to preventcross-endpoint collisions
backend/src/main.rsAppStatestruct holdingArc<SorobanClient>andArc<IdempotencyStore>AppStateviaState<AppState>backend/src/routes/refund.rs(primary change)Idempotency-Keyheader(status, body)keyed by the header valuebackend/src/routes/pay.rsandcancel.rsHow it works — request/response examples
First request:
POST /invoices/42/refund Idempotency-Key: a1b2c3d4-e5f6-7890-abcd-ef1234567890 Content-Type: application/json
{ "payer": "GPAYER...", "signed_xdr": "AAAA==" }
→ 200 OK { "status": "refund_requested", "transaction_hash": "abc123..." }
Retry with same key (e.g. after timeout):
POST /invoices/42/refund Idempotency-Key: a1b2c3d4-e5f6-7890-abcd-ef1234567890 Content-Type: application/json
{ "payer": "GPAYER...", "signed_xdr": "AAAA==" }
→ 200 OK ← returned from cache, Soroban NOT called { "status": "refund_requested", "transaction_hash": "abc123..." }
Request without header (backward compatible):
POST /invoices/42/refund Content-Type: application/json
{ "payer": "GPAYER...", "signed_xdr": "AAAA==" }
→ processed normally, no caching
cargo test output
test routes::cancel::tests::test_cancel_invoice_missing_body_returns_4xx ... ok test routes::cancel::tests::test_cancel_invoice_unreachable_rpc_returns_error ... ok test routes::cancel::tests::test_same_idempotency_key_returns_cached_response ... ok test routes::health::tests::returns_200_when_all_dependencies_are_healthy ... ok test routes::health::tests::returns_503_when_any_dependency_is_degraded ... ok test routes::pay::tests::test_pay_invoice_missing_body_returns_4xx ... ok test routes::pay::tests::test_pay_invoice_unreachable_rpc_returns_5xx_or_404 ... ok test routes::pay::tests::test_same_idempotency_key_returns_cached_response ... ok test routes::refund::tests::test_different_idempotency_key_is_independent ... ok test routes::refund::tests::test_refund_invoice_missing_body_returns_4xx ... ok test routes::refund::tests::test_refund_invoice_unreachable_rpc_returns_error ... ok test routes::refund::tests::test_same_idempotency_key_returns_cached_response ... ok
test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Notes
persistent/distributed deployment, swap
IdempotencyStorefor a Redis-backedimplementation — the interface is the same.
Idempotency-Keyheader is optional. Requests without it are processednormally (backward compatible).