# Cancel

> Use the cancel API to actively cancel a transaction.

You can use the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API to actively cancel a transaction. The cancellation period is subject to the contract.

Before issuing a cancellation, you must be familiar with the following information:

-   If the transaction is canceled before the payment is successful, the buyer will not be able to continue the payment.
-   For transactions cancelled after successful payment, the funds will be returned to the user's account.
-   **Commission** **fee**: There are no fees charged for canceled transactions.
-   **Partial cancellation**: Only a full cancellation is allowed.
-   **Cancel** **period**: See the following table for details:

| **Payment method** | **Cancel period** |
| --- | --- |
| Indonesia Local Payment Method and [JKOPay](https://docs.antom.com/ac/antomop/jkopay.md) | Cancellation is not supported. |
| Thai online banking, mobile banking APP and bank transfer payment methods | - Order time to pay success before the transaction can be canceled. - Payment success can not cancel the transaction. |
| Card Payment | - Payment success after seven days can cancel the transaction. - Transactions that are successful cannot be canceled. |
| Mercado Pago (Brazil, Chile, Mexico and Peru) | The transaction can be canceled within seven days after the payment is successful. |
| Other payment methods | Payment success can be in T +1 day of 00:15(UTC +8:00) before the cancellation. |

Table 1. Payment method cancellation period

# When to cancel a transaction

If you have business scenarios where there are transactions that need to be canceled (such as flash sales, hotel booking services, and ticket selling services). You can cancel a transaction by using the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API. After a transaction is canceled, the payment status is `CANCELLED`. Even if a notification of a successful payment is received after the transaction is canceled, the payment status remains `CANCELLED`. You can cancel a transaction in the following scenarios, and Antom handles the cancellation accordingly:

| **Scenario** | **Result** |
| --- | --- |
| The cancellation request is issued when the transaction is paid and within the cancellation period. | Full refund without charging the commission fee. |
| The cancellation request is issued when the transaction has been paid, and the transaction result cannot be obtained through Antom asynchronous notification and calling the [**inquiryPayment**](https://docs.antom.com/ac/ams/paymentri_online.md) API. | Close the transaction. A full refund will be issued without charging the commission fee if an asynchronous payment success notification is subsequently received. |
| The buyer does not submit payment: For example, the buyer does not click the confirmation button in AlipayHK. | The buyer is still able to authorize and pay. |
| The buyer submit payment and initiate cancellation within the 10-minute payment period. | The buyer is unable to pay, but can continue to authorize. |

Table 2. Cancel transaction scenario and processing

> **Note**:
>
> -   Refunded transactions (including full refund and partial refund) cannot be canceled. Canceling refunded transactions will return `PROCESS_FAIL` error code.
> -   Even if you receive a payment success asynchronous notification after the transaction is canceled, the system's transaction status should remain the `CANCELLED`.

# How to cancel a transaction

## Step 1: Call the cancel API

When you close an order and receive an asynchronous payment success notification from Antom, you need to call the **[cancel](https://docs.antom.com/ac/ams/paymentc_online.md)** API to cancel the transaction. Pass in one of the following two IDs in the request:

| **Parameter** | **Required** | **Description** |
| --- | --- | --- |
| _paymentId_ | No | The unique identifier that Antom assigns to a transaction. |
| _paymentRequestId_ | No | The unique identifier that you assign to a request. |

Table 3. Details of the request parameters of the **cancel** API

#### Tab: Call API using paymentRequestId

The following code shows how to call the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API using 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
    }
}
}
```

#### Tab: Call API using paymentId

The following code shows how to call the [**cancel**](https://docs.antom.com/ac/ams/paymentc_online.md) API using 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": "2019061218401080010018882020035****"
}

{
  "paymentRequestId": "paymentRequestId****"
}
```

The following code shows a sample of the response message:

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

## Step 2: Obtain the cancellation result

After you call the **[cancel](https://docs.antom.com/ac/ams/paymentc_online.md)** API, Antom will use the _result.resultStatus_ field of the cancel response to synchronously return the transaction cancellation result.

The table below displays the possible returned values and please follow the corresponding instructions for handling:

| **_result.resultStatus_** | **Message** | **Handling suggestions** |
| --- | --- | --- |
| `S` | The cancellation is successful. | No further action is required. Note: If the buyer does not submit payment when the API is called, the payment process will continue. |
| `F` | The cancellation failed. | Please process accordingly based on the _resultCode_. |
| `U` | Cancellation failed for unknown reason. | Keep the original request unchanged and re-call the API to solve the problem. If the problem is not resolved, contact Antom Technical Support. |

Table 4. Possible values returned in the _result.resultStatus_

> **Note**: If you do not receive a response message, it may be due to a network timeout. Keep the original request unchanged and re-call the API to solve the problem. If the problem is not resolved, contact Antom Technical Support.