Process, approve, deny, and manage refund requests with the Merchant SDK
The X402rMerchant class provides a complete set of methods for handling refund requests from payers. Every refund-related method requires a nonce: bigint parameter that identifies which specific charge the refund targets.
The nonce parameter corresponds to the record index from the PaymentIndexRecorder. For the first charge against a payment, the nonce is 0n. Each subsequent charge increments the nonce.
Use getRefundStatus() to retrieve the current status of a refund request. Returns a RequestStatus enum value.
Copy
import { RequestStatus } from '@x402r/core';const status = await merchant.getRefundStatus(paymentInfo, 0n);switch (status) { case RequestStatus.Pending: console.log('Awaiting your decision'); break; case RequestStatus.Approved: console.log('You approved this refund'); break; case RequestStatus.Denied: console.log('You denied this refund'); break; case RequestStatus.Cancelled: console.log('Payer cancelled the request'); break;}
Use getRefundRequestByKey() to look up a refund request directly by its composite key (the keccak256(paymentInfoHash, nonce) value returned from paginated queries).
Use getPendingRefundRequests() to retrieve paginated refund request keys for the current receiver address. This method uses the wallet address associated with your X402rMerchant instance.
Copy
// Get the first 10 refund request keysconst { keys, total } = await merchant.getPendingRefundRequests(0n, 10n);console.log(`Showing ${keys.length} of ${total} total refund requests`);// Look up each request by its composite keyfor (const key of keys) { const request = await merchant.getRefundRequestByKey(key); console.log(`Key: ${key}`); console.log(` Amount: ${request.amount}`); console.log(` Status: ${request.status}`);}
For pagination, adjust the offset and count parameters:
Copy
// Page through all refund requests, 20 at a timeconst pageSize = 20n;let offset = 0n;let hasMore = true;while (hasMore) { const { keys, total } = await merchant.getPendingRefundRequests(offset, pageSize); for (const key of keys) { const request = await merchant.getRefundRequestByKey(key); // Process each request... } offset += pageSize; hasMore = offset < total;}
Approving a refund request changes its status but does not transfer funds. You must also call refundInEscrow() or refundPostEscrow() to execute the actual token transfer.