# Cancel

> Learn how to close an order using the cancel API within a limited period.

After the buyer places an order, you can close that order using the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API within a limited period. The supported cancellation period is subject to the contract.

Before issuing a cancellation, ensure to take note of the following information:

-   If a transaction is canceled before the payment is completed, the buyer will be unable to proceed with the payment.
-   If a transaction is canceled after the payment is completed, funds will be refunded to the buyer’s account.
-   **Partial cancellation**: Not allowed. Only a full cancellation is allowed.
-   **Cancellation period**: Varies by payment method. See the following table for details:

| **Payment method** | **Cancellation period** |
| --- | --- |
| Thailand-based online banking | Cancellation of the transaction can be done at any time from order placement to before payment is completed. Once the payment is successful, the transaction cannot be canceled. |
| Other payment methods | Transactions can be canceled at any time from the order placement until 00:15 (GMT+8) on T+1 day. |

# Cancellation steps

You can call the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API to cancel a transaction under the following scenarios:

-   You need to close an order while its status on Antom’s side remains processing
-   Other scenarios requiring transaction cancellation

### Step 1: Initiate a cancellation request

Call the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API to cancel a single transaction by passing one of the following IDs:

| **Parameter name** | **Required** | **Description** |
| --- | --- | --- |
| _paymentId_ | No | The unique transaction ID returned by Antom when an order is placed. |
| _paymentRequestId_ | No | The unique request ID assigned by the merchant when an order is placed. |

The code below shows a sample of calling the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API:

#### Tab: Using paymentRequestId

Initiate a cancellation request with _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
    }
}
```

The following code shows a sample of the request message:

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

#### Tab: Using paymentId

Initiate a cancellation request with _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
    }
}
```

The following code shows a sample of the request message:

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

### Step 2: Obtain cancellation result

After calling the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API, Antom returns a cancellation response:

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

The table below shows the possible values of _result.resultStatus_ returned in the response, please handle the result according to the guidance provided:

| _**result.resultStatus**_ | **Message** | **Further action** |
| --- | --- | --- |
| `S` | Cancellation is successful. _cancelTime_ indicates the success time. | No further action is needed. |
| `F` | Cancellation failed. | See **Result/Error codes** of the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API for troubleshooting. |
| `U` | Cancellation result is unknown. | Please retry the API call with the original request. If the issue persists, contact Antom Technical Support. |

> **Notes**:
>
> -   If no response is received, it may indicate a network timeout. Please retry the API call with the original request. If the issue persists, contact Antom Technical Support.
> -   Transactions with prior refunds (full or partial) cannot be canceled. Canceling such transactions returns error code `PROCESS_FAIL`.
> -   After cancellation, even if you receive an asynchronous notification of payment success, the transaction status in your system should still be maintained as `CANCELLED`.