Overview
Thecancel() method handles cancellation requests for authorized payments that have not yet been settled. This is typically called when a customer cancels an order or when a payment needs to be voided before capture.
Method signature
Parameters
The cancellation request object from VTEX.
Unique identifier for the original payment transaction
Unique identifier for this cancellation request
Authorization identifier from the original authorization response
Amount to cancel in cents (may be partial)
Transaction identifier from the original authorization
Response
The cancellation response indicating the result of the cancellation request.
Echo of the payment identifier from the request
Unique identifier for this cancellation (present when approved)
Response code from the payment provider
Human-readable message describing the cancellation result
Echo of the request identifier from the request
Response types
- Approved
- Denied
Cancellation was successfully processed.
Implementation
The test suite implementation shows the basic pattern:Common scenarios
Full cancellation
Cancel the entire authorized amount:Partial cancellation
Some payment providers support canceling only part of the authorized amount:Idempotent cancellations
Handle duplicate cancellation requests:Helper methods
The@vtex/payment-provider package provides helper methods for creating responses:
Error handling
Payment not found
Payment not found
If the payment ID doesn’t exist, deny the cancellation with an appropriate message:
Already settled
Already settled
If the payment has already been settled, cancellation is not possible:
Already cancelled
Already cancelled
If the payment was already cancelled, return the original cancellation:
Provider timeout
Provider timeout
If the payment provider times out, you can return a pending status or deny:
Best practices
- Always verify payment status before attempting cancellation. Check that the payment is authorized but not yet settled.
-
Implement idempotency by storing cancellation responses and returning the same response for duplicate
requestIdvalues. - Include meaningful error messages to help merchants understand why a cancellation was denied.
- Log cancellation attempts for audit trails and troubleshooting.
- Handle partial cancellations if your payment provider supports them.
Payment lifecycle
Cancellation is only possible for authorized payments. Once settled, use refund() instead.Related routes
- authorize() - Create the initial authorization
- settle() - Settle an authorized payment
- refund() - Refund a settled payment