# Cancel

> Learn how to cancel a transaction using the APO API and how to know the cancellation result.

After the buyer places an order, you can cancel the transaction within a specific expiration time using the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) API. The expiration time for cancellation, fees, and other details depend on the contract with the acquirer. Currently, only full cancellations are supported while partial cancellations are not available.

For Antom, please refer to the rules for cancellations in [Cancel](https://docs.antom.com/ac/cashierpay/cancel.md).

## How to cancel a transaction

1.  Initiate a cancellation request
2.  Obtain the cancellation result

### Step 2: Initiate a cancellation request server-side

You can initiate the cancellation of a single transaction using the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) AP. The following sample code shows how to call the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) API:

#### Tab: Call the cancel API through paymentId

```java
public static void payCancel() {
    AlipayPayCancelRequest alipayPayCancelRequest = new AlipayPayCancelRequest();

    // replace with your paymentId
    alipayPayCancelRequest.setPaymentId("yourPaymentId");

    AlipayPayCancelResponse alipayPayCancelResponse = null;
    try {
        alipayPayCancelResponse = CLIENT.execute(alipayPayCancelRequest);
    } catch (AlipayApiException e) {
        String errorMsg = e.getMessage();
        // handle error condition
    }
}
```

#### Tab: Call the cancel API through paymentRequestId

```java
public static void payCancel() {
    AlipayPayCancelRequest alipayPayCancelRequest = new AlipayPayCancelRequest();

    // replace with your paymentRequestId
    alipayPayCancelRequest.setPaymentRequestId("yourPaymentRequestId");

    AlipayPayCancelResponse alipayPayCancelResponse = null;
    try {
        alipayPayCancelResponse = CLIENT.execute(alipayPayCancelRequest);
    } catch (AlipayApiException e) {
        String errorMsg = e.getMessage();
        // handle error condition
    }
}
```

You must pass in one of the following parameters in your request:

| **Parameter name** | **Whether required** | **Description** |
| --- | --- | --- |
| _paymentId_ | No | The unique identifier that APO assigns to a transaction. |
| _paymentRequestId_ | No | The unique identifier that you assign to a request. |

Table 1. Details of the request parameters of the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) API

The following shows the sample code of a request:

```java
{
  "paymentId": "2019061218401080010018882020035XXXX"
}

{
  "paymentRequestId": "paymentRequestIdXXXX"
}
```

### Step 2: Obtain the cancellation result server-side

After calling the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) API, APO will return the cancellation result through the _result.resultStatus_ parameter in the response:

```json
{
  "cancelTime": "2019-06-12T19:07:11+08:00",
  "paymentId": "2019061218401080010018882020035XXXX",
  "paymentRequestId": "pay_2089760038715669_20277574507XXXX",
  "result": {
    "resultCode": "SUCCESS",
    "resultMessage": "Success",
    "resultStatus": "S"
  }
}
```

| **_result.resultStatus_** | **Message** | **Further actions** |
| --- | --- | --- |
| `S` | Indicates that the cancellation is successful. | Please note that the buyer will be able to proceed with completing the payment if the payment is not submitted when the [**cancel**](http://docs.antom.com/ac/apo/paymentc_online) API is called. |
| `F` | Indicates that the cancellation failed. | You can handle the situation according to the error codes. |
| `U` | Indicates that the cancellation result is unknown. | Use the same _paymentId_ or _paymentRequestId_ to try again. Please contact APO technical support if the problem persists. |

Table 2. Explanation of _result.resultStatus_

> **Notes****:**
>
> -   If you did not receive a response message, it might be due to a network timeout. Please use the same _paymentId_ or _paymentRequestId_ to try again. Please contact APO technical support if the problem persists.
> -   Transactions that have already been refunded (including full refunds and partial refunds) do not support cancellation. Trying to cancel a refunded transaction will result in the error code `PROCESS_FAIL`.
> -   Even if a successful payment asynchronous notification is received after the transaction is canceled, the transaction status in your system will remain as `CANCELLED`.
> -   Card payments do not support cancellation during capture or after capture is successful.
>
> **Common Questions**
>
> **Q: Can an order be canceled after it has been successfully captured?**
>
> A: Once an order has been captured, cancellation is not supported. Instead, you can initiate a refund for the captured order.
>
> **Q:** **Will there be an asynchronous notification for cancellation?**
>
> A: Currently, the result of the cancellation is returned synchronously, and asynchronous notifications are not supported.
>
> **Q:** **What is the expiration time for cancellation?**
>
> A: The expiration times depends on the acquirers, please reach out to the respective acquirer.