cancel

Use the cancel API to cancel a payment if the payment result is not returned for a long time, or to cancel a payment authorization after the authorization is completed. 

A couple of things to note before calling this API:

To cancel the payment, you must provide one of the following parameters:

  • paymentId: The original payment ID of the payment request to be cancelled, generated by Alipay when the merchant initiates the original payment.
  • paymentRequestId: The original paymentRequestId of the payment request to be cancelled.   

Structure

A message consists of a header and body. The following sections are focused on the body structure. For the header structure, see: 

Note: Set the data type of each field (except array) as String. This means that you must use double quotation marks (" ") to enclose the field value. Examples:

  • If the data type of a field is Integer and its value is 20, set it as "20". 
  • If the data type of a field is Boolean and its value is true, set it as "true". 

Request parameters

paymentId String  

The unique ID that is assigned by Alipay to identify a payment. paymentId and paymentRequestId cannot both be null. A one-to-one correspondence between paymentId and paymentRequestId exists.

More information about this field:

  • Maximum length: 64 characters

paymentRequestId String  

The unique ID that is assigned by a merchant to identify a payment request. paymentRequestId and paymentId cannot both be null. If both paymentRequestId and paymentId are provided, paymentId takes precedence.

More information about this field:

  • Maximum length: 64 characters

Response parameters

result Result object REQUIRED

The request result contains information such as status and error codes.

Child parameters

paymentId String  

The unique ID that is assigned by Alipay to identify a payment.

More information about this field:

  • Maximum length: 64 characters

paymentRequestId String  

The unique ID that is assigned by a merchant to identify a payment request. 

More information about this field:

  • Maximum length: 64 characters

cancelTime Datetime  

The actual execution completion time of the payment cancellation process, that is, the date and time when the payment cancellation succeeds. This parameter is returned only when the cancellation succeeds.

More information about this field:

  • The value follows the ISO 8601 standard format. For example, "2019-11-27T12:01:01+08:30".
API Explorer

Request

Method
POST
Endpoint
/v1/payments/cancel
Header
Accept: application/json
URL
https://<domain_name>/ams/api/<endpoint>
Domain name
North America: https://open-na.alipay.com
Asia: https://open-sea.alipay.com
Europe: https://open-eu.alipay.com
Request Body

Response

Case
Response Body

Result process logic

For different request results, different actions are to be performed. See the following list for details:

  • If the value of result.resultStatus is S, the cancellation succeeds.

If the value of result.resultStatus is F, the cancellation fails. See the returned error code and determine further actions.

  • If the value of result.resultStatus is U, the cancellation result is unknown. Use the same request parameters to retry the cancellation request.
  • If no response is returned, use the same request parameters to retry the cancellation request. 

Error codes

Error codes are usually classified into the following categories:

  • Common error codes: common for all online and in-store payment APIs.
  • API-specific error codes: listed in the following table.  

Result/Error codes

CodeValueMessageFurther action
SUCCESSSSuccess

The order is canceled. The merchant needs to set the status of receipt to cancel.

CANCEL_WINDOW_EXCEEDFThe cancellation date exceeds the period during which cancellation is supported. The period is agreed in the contract.

The transaction cannot be canceled anymore. Initiate a refund against the transaction.

ORDER_NOT_EXISTFThe order does not exist.

Check whether paymentId is correct.

CLIENT_INVALIDFThe client is invalid.

Check whether the clientId is correct.

METHOD_NOT_SUPPORTEDFThe server does not implement the requested HTTP method.

Check whether the HTTP method is correct.

MEDIA_TYPE_NOT_ACCEPTABLEFThe server does not implement the media type that is acceptable to the client.

Check whether the media type is correct.

MERCHANT_BALANCE_NOT_ENOUGHFThe merchant balance is not enough.

Call the interface again after the merchant has sufficient balance.