Overview
Therefund() method handles refund requests for settled payments. Refunds return money to the customer after a payment has been captured. This typically occurs when a customer returns a product or when an order needs to be reversed after fulfillment.
Method signature
Parameters
The refund request object from VTEX.
Unique identifier for the original payment transaction
Unique identifier for this refund request
Settlement identifier from the original settlement response
Amount to refund in cents (may be partial)
Transaction identifier from the original authorization
Response
The refund response indicating the result of the refund request.
Echo of the payment identifier from the request
Unique identifier for this refund (present when approved)
Actual amount refunded in cents (may differ from requested amount)
Response code from the payment provider
Human-readable message describing the refund result
Echo of the request identifier from the request
Response types
- Approved
- Denied
Refund was successfully processed and funds will be returned.
Implementation
The test suite implementation shows the basic pattern:Common scenarios
Full refund
Refund the entire settled amount:Partial refund
Refund only part of the settled amount:Multiple refunds
Handle multiple refund requests for the same payment:Idempotent refunds
Handle duplicate refund requests:Time-based refund validation
Some payment providers have refund windows:Helper methods
The@vtex/payment-provider package provides helper methods for creating responses:
Error handling
Settlement not found
Settlement not found
If the original settlement doesn’t exist:
Refund period expired
Refund period expired
If the refund window has passed:
Already refunded
Already refunded
If the full amount has already been refunded:
Amount exceeds settlement
Amount exceeds settlement
If trying to refund more than settled:
Provider error
Provider error
If the payment provider returns an error:
Best practices
- Validate settlement exists and has been completed before attempting refund.
- Check refund windows - most providers have time limits (typically 90-180 days).
-
Implement idempotency by storing refund responses and returning the same response for duplicate
requestIdvalues. - Support partial refunds to allow merchants to refund portions of orders.
- Track total refunded amount when supporting multiple refunds for the same settlement.
- Include detailed error messages to help merchants understand refund failures.
- Log all refund attempts for audit trails and accounting reconciliation.
- Consider provider-specific limits - some providers have minimum refund amounts or maximum refund counts.
Payment lifecycle
Refunds can only occur for settled payments. Once fully refunded, no further operations are possible on the payment.Related routes
- authorize() - Create the initial authorization
- settle() - Settle an authorized payment (required before refund)
- cancel() - Cancel an authorized payment before settlement