Cancel a payment
You can cancel the transactions that have been successfully paid in the Alipay system by using the cancel API. The cancellation cannot be performed if being out of the cancellable period that is specified in the contract. Usually, transactions on the transaction (T) day can be canceled before T+1 00:15, and the time zone is GMT +8.
When to use the cancel API
For payment scenarios with a specific transaction closing time of the merchant, for example, a flash sale, hotel reservation, or flight ticket purchase, the cancel API must be integrated. You can initiate a payment cancellation for one of the following cases:
- After the payment is successful and within the cancellable period, if the payment needs to be canceled, call the cancel API. If the transaction is canceled successfully, the full amount will be refunded. There is no fee charged for cancellation, but the cancellable period is usually only one day.
- If the transaction is not paid and the transaction needs to be canceled in advance before Alipay's payment expiration time (one minute), call the cancel API. If the transaction is not paid when the transaction is canceled but is paid after you cancel the transaction, the full amount will also be refunded to users.
- If you cannot receive the asynchronous notification or get a confirmative result (
S
orF
) from the inquiryPayment response, terminate the transaction by calling the cancel API to keep the transaction status consistent between you and the user.
Notes:
- After the transaction is canceled, the transaction status will be changed to
CANCELLED
. Even if an asynchronous notification of payment success is received after the transaction is canceled, the order status in the system is stillCANCELLED
.- A transaction that has been refunded cannot be canceled. An error code of
PROCESS_FAIL
will be returned if you retry to cancel a transaction that has been refunded.
How to call the cancel API
To cancel a transaction, call the cancel API by specifying the paymentId or paymentRequestId field with the same value as that of the transaction to be canceled. Assume that the domain name is open-na.alipay.com
, the request URLs are:
- Sandbox environment:
https://open-na-global.alipay.com/ams/sandbox/api/v1/payments/cancel
- Production environment:
https://open-na-global.alipay.com/ams/api/v1/payments/cancel
The example below shows a cancellation request that uses the paymentRequestId field to cancel the transaction. The paymentRequestId field is assigned by the merchant to identify each unique request. It is suggested to use this field for the cancellation process.
{
"paymentRequestId": "pay_1089760038715669_102775745075669"
}
The example below shows a cancellation request that uses the paymentId field to cancel the transaction:
{
"paymentId":"20190608114010800100188820200355883"
}
Cancellation result
As long as the cancel request is initiated, Alipay will guarantee that the transaction will be canceled or refunded:
- If the user has not completed the payment, the transaction will be canceled.
- If the user has completed the payment, the transaction will be refunded.
The example below shows a cancellation response. The time when the payment cancellation succeeds is indicated by the value of cancelTime.
{
"result":
{
"resultCode":"SUCCESS",
"resultStatus":"S",
"resultMessage":"success"
},
"paymentRequestId":"pay_1089760038715669_102775745075669",
"paymentId":"20190608114010800100188820200355883",
"cancelTime":"2019-06-12T19:07:11+08:00"
}
The cancellation result is returned in the result.resultStatus field of the cancel response. The following graphic shows the processing logic of calling the cancel API:
Figure 1. Further actions for different cancel results
S
: indicates that the cancellation succeeded.F
: indicates that the cancellation failed. Take further actions according to resultCode and then retry the cancel API until a result of S is obtained.U
: indicates that the cancellation result is unknown. Use the same request parameters to retry the cancellation request. If you call the cancel request three times and still getsU
, contact Alipay Technical Support.
Notes: