Payments

cancel

Cancel a payment when the payment result has not been returned or merchant decides not to accept.

Overview

POST /v1/payments/cancel is used to cancel a payment when the payment result has not been returned for an extended period, or when the merchant, after reviewing the customer's order, decides not to accept it. Upon successful invocation of the cancel API, the full amount paid by the customer will be returned.

To cancel a payment, you need to provide one of the following parameters:

  • paymentId: The original payment ID of the payment request to be canceled. It is generated by Alipay when a merchant initiates the original payment.
  • paymentRequestId: The original paymentRequestId of the payment request to be canceled.

All monetary amounts in requests and responses must be expressed in the smallest currency unit (IQD fils). For example, 100 IQD equals 100000.

Note: After merchant initiated capture, void, refund for a payment, it is not allowed to initiate a cancel.

  1. The Mini Program calls this my.getAuthCode JSAPI with scopes=USER_ID.
  2. The E-wallet App service calls authorization service to processes the authorization information.
  3. The E-wallet backend verifies the authorization information, generates the authCode and returns.
  4. The E-wallet App service returns the authCode to the Mini Program.
  5. The Mini Program sends the authCode to the merchant backend.
  6. The merchant backend calls the applyToken API with authCode to apply the accessToken.
  7. The E-Wallet backend returns accessToken information to the merchant backend, such as customerId, accessToken, refreshToken, etc.
  8. The merchant backend returns customerId to mini program.
  9. The Mini Program creates an order with customerId.
  10. The merchant backend calls the payment API to initiate payment flow, including customerId as referenceBuyerId, paymentNotifyUrl(optional), etc.
  11. The E-Wallet backend returns payment detail information the merchant backend, such as redirectionUrl.
  12. The merchant backend passes the payment detail information to the Mini Program.
  13. The Mini Program calls the my.tradePay JSAPI with redirectionUrl to conduct the payment.
  14. The E-Wallet App Service displays the cashier page with order information, such as amount, order details, etc.
  15. The user confirms the payment in the cashier page.
  16. The E-Wallet App Service calls payment service and execute the payment process.
  17. When the payment reaches the final status, the E-wallet backend returns the payment result to the Mini Program (Step 19).
  18. The E-Wallet App Service returns the payment result to mini program.
  19. Send cancel request within T day via /v1/payments/cancel.
  20. The E-Wallet backend returns cancel result to the merchant backend.

Endpoint

MethodPath
POST/v1/payments/cancel

API Structure

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

Request

FieldTypeRequiredDescriptionExample
paymentIdstringNoThe unique ID that is assigned by the wallet to identify a payment. Max. length: 64 characters."202312061112128001001660x"
paymentRequestIdstringNoThe unique ID is assigned by a merchant to identify a payment request. Max. length: 64 characters."20231127190741010007000x"
extendInfostringNoThe extension information that wallets and merchants want to describe. The format should be JSON format. Max. length: 2048 characters."extendInfo: This is additional information"
At least one of paymentId or paymentRequestId must be provided.

Sample Request

{
  "paymentId": "202312061112128001001660090000xxxx"
}

Response

FieldTypeRequiredDescriptionExample
resultobjectYesThe request result, which contains information such as result status and error codes.{ "resultCode": "SUCCESS", "resultStatus": "S", "resultMessage": "Success." }
paymentIdstringNoThe unique ID is assigned by the wallet to identify a payment. Max. length: 64 characters."202312061112128001001660x"
paymentRequestIdstringNoThe unique ID is assigned by a merchant to identify a payment request. Max. length: 64 characters."20231127190741010007000x"
cancelTimedatetimeNoThe actual time when the payment cancellation process is completed. This property is returned only when the cancellation succeeds. The value follows the ISO 8601 standard."2023-01-08T12:12:12+08:00"

Sample Response

{
  "result": {
    "resultCode": "SUCCESS",
    "resultStatus": "S",
    "resultMessage": "Success."
  },
  "paymentId": "202312061112128001001660090000xxxx",
  "paymentRequestId": "20231127190741010007000070000xxxx",
  "cancelTime": "2023-01-08T12:12:12+08:00"
}

Result Process Logic

In the response, the result.resultStatus field indicates the result of processing a request. The following table describes each result status:

resultStatusDescription
SIt means that the cancellation is successful. The corresponding result.resultCode is "SUCCESS", and the result.resultMessage is "Success.".
UIt means that the status of the cancellation result is unknown. The corresponding result.resultCode is UNKNOWN_EXCEPTION, and result.resultMessage is "An API calling is failed, which is caused by unknown reasons". For details, see the Common error codes section.
FIt means that the cancellation fails. The corresponding result.resultCode and result.resultMessage may vary based on different situations. For details, see the Error codes section.

Error Codes

Error codes are usually classified into the following categories:

  • Common error codes are common across all mini program's APIs.
  • API-specific error codes are listed in the following table.
resultStatusresultCoderesultMessage
FORDER_STATUS_INVALIDThe order is in invalid status such as CLOSED.
FORDER_HAS_BEEN_CAPTUREDThe cancellation does not support when the order has been captured.
FORDER_HAS_BEEN_REFUNDEDThe cancellation does not support when the order has been refunded.
FORDER_HAS_BEEN_VOIDEDThe cancellation does not support when the order has been voided.
FCANCEL_NOT_ALLOWEDThe cancellation not allowed in agreement.
FCANCEL_WINDOW_EXCEEDThe cancellation date is beyond the period that is agreed in the contract.
FORDER_NOT_EXISTThe order does not exist.
FKEY_NOT_FOUNDThe key is not found.
FCLIENT_INVALIDThe client is invalid.
FINVALID_SIGNATUREThe signature is invalid.
FMETHOD_NOT_SUPPORTEDThe server does not implement the requested HTTP method.
FMEDIA_TYPE_NOT_ACCEPTABLEThe server does not implement the media type that can be accepted by the client.
FPARTNER_STATUS_ABNORMALThe partner status is abnormal.
FPARTNER_BALANCE_NOT_ENOUGHThe partner balance is not enough.

Samples

The merchant can call this cancel API to cancel the payment transaction within T day under these circumstances and E-Wallet will not settle payment this transaction in T+N day either.

  • when the merchant decides to provide a full refund before settlement.
  • when the payment result does not return for a long time.
  • when the merchant cannot fulfill the order before accepting it.

For refunds after the money has been settled to the merchant, the merchant backend should call refund API instead.