# cancel

> Use this API to cancel the payment if the payment result is not returned after a long time. The cancellation cannot be performed if being out of the cancellable period specified in the contract.

`POST /v1/payments/cancel`

Use this API to cancel the payment if the payment result is not returned after a long time. The cancellation cannot be performed if being out of the cancellable period specified in the contract. Use the same _paymentId_ or _paymentRequestId_ that is used in the payment API to cancel the payment transaction.

# Structure

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

-   [Request header](https://docs.antom.com/ac/ams/api_fund.md#ML5ur)
-   [Response header](https://docs.antom.com/ac/ams/api_fund.md#WWH90)

> **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 original payment ID of the payment request to be canceled, generated by Antom to uniquely identify the payment when the merchant initiates the original payment. _paymentId_ and _paymentRequestId_ cannot both be null. A one-to-one correspondence between _paymentId_ and _paymentRequestId_ exists.

More information:

- Maximum length: 64 characters

#### paymentRequestId (String)

The original payment request ID of the payment request to be canceled, generated by the merchant to uniquely identify a payment request. _paymentRequestId_ and _paymentId_ cannot both be null. If both _paymentRequestId_ and _paymentId_ are provided, _paymentId_ takes precedence.

More information:

- Maximum length: 64 characters

#### merchantAccountId (String)

The unique ID to identify a merchant account.

> Note: Specify this parameter when you use a single client ID across multiple locations.

More information:

- Maximum length: 32 characters

## Response parameters

#### result (Result, REQUIRED)

Indicates whether this API is called successfully. If this API is successfully called, the payment is canceled successfully.

##### resultCode (String, REQUIRED)

Result code. Possible values are listed in the Result/Error codes table on this page.

More information:

- Maximum length: 64 characters

##### resultStatus (String, REQUIRED)

Result status. Valid values are:  

-   `S`: Indicates that the payment is canceled successfully.
-   `F`: Indicates that the payment failed to be canceled.
-   `U`: Indicates that the cancellation result is unknown.

##### resultMessage (String, REQUIRED)

Result message that explains the result code.

More information:

- Maximum length: 256 characters

#### paymentId (String)

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

> Note: This field is returned when the cancellation succeeds (the value of _result.resultStatus_ is `S`).

More information:

- Maximum length: 64 characters

#### paymentRequestId (String)

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

> Note: This field is returned when the cancellation succeeds (the value of _result.resultStatus_ is `S`).

More information:

- Maximum length: 64 characters

#### cancelTime (Datetime)

The actual execution completion time of the payment cancellation process, which is the date and time when the payment cancellation succeeds.

> Note: This field is returned when the cancellation succeeds (the value of _result.resultStatus_ is `S`).

More information:

- The value follows the [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) standard format. For example, "2019-11-27T12:01:01+08:00".

## 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 succeeded.
-   If the value of _result.resultStatus_ is `F`, the cancellation failed. 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.

## Result/Error codes

| Code | Value | Message | Further action |
| --- | --- | --- | --- |
| SUCCESS | S | Success | The order is canceled successfully. |
| ACCESS_DENIED | F | Access is denied. | Contact Antom Technical Support for detailed reasons. |
| INVALID_API | F | The called API is invalid or not active. | Contact Antom Technical Support to resolve the issue. |
| CANCEL_WINDOW_EXCEED | F | The cancellation date exceeds the period during which cancellation is supported. The period is agreed upon in the contract. | The transaction cannot be canceled anymore. |
| KEY_NOT_FOUND | F | The private key or public key of <span>Antom</span> or the merchant is not found. | Check whether the private key or public key exists. If not, upload the private key in Antom Developer Center. |
| MERCHANT_BALANCE_NOT_ENOUGH | F | The merchant balance is insufficient. | Call the interface again after the merchant has sufficient balance. |
| NO_INTERFACE_DEF | F | API is not defined. | Check whether the URL is correct. Please refer to the endpoint in the API documentation. |
| ORDER_NOT_EXIST | F | The order does not exist. | Check whether paymentId is correct. If correct, contact Antom Technical Support for specific reasons. |
| ORDER_STATUS_INVALID | F | The transaction cannot be further processed because the order status is invalid.  | Check the order status of the transaction or contact Antom Technical Support for detailed reasons. |
| PARAM_ILLEGAL | F | The required parameters are not passed, or illegal parameters exist. For example, a non-numeric input, an invalid date, or the length and type of the parameter are wrong. | Check and verify whether the required request fields (including the header fields and body fields) of the current API are correctly passed and valid. |
| PROCESS_FAIL | F | A general business failure occurred. | Do not retry. Human intervention is usually needed. It is recommended that you contact Antom Technical Support to troubleshoot the issue. |
| SYSTEM_ERROR | F | A system error occurred. | Do not retry, and contact Antom Technical Support for more details. |
| REQUEST_TRAFFIC_EXCEED_LIMIT | U | The request traffic exceeds the limit. | Call the interface again to resolve the issue. If not resolved, contact Antom Technical Support. |
| PAYMENT_METHOD_NOT_SUPPORTED | F | The payment method does not support canceling or refunding transactions if the payment status is successful. | Do not retry. Contact Antom Technical Support to get more information. |
| UNKNOWN_EXCEPTION | U | An API call has failed, which is caused by unknown reasons. | Call the interface again to resolve the issue. If not resolved, contact Antom Technical Support. |
| USER_NOT_EXIST | F | The user does not exist on the payment method side. | Contact Antom Technical Support for detailed reasons. |
| USER_STATUS_ABNORMAL | F | The user status is abnormal on the payment method side. | Contact Antom Technical Support to know the specific reasons. |

## Request

```json
{
  "paymentId": "2019060811401080010018882020035****",
  "merchantAccountId": "2188234232"
}
```

## Response

### SUCCESS

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

### TRANSACTION_TO_BE_CANCELED_NOT_EXIST

```json
{
  "result": {
    "resultCode": "ORDER_NOT_EXIST",
    "resultMessage": "The order does not exist.",
    "resultStatus": "F"
  }
}
```

### TRANSACTION_TO_BE_CANCELED_ALREADY_REFUNDED

```json
{
  "result": {
    "resultCode": "PROCESS_FAIL",
    "resultMessage": "A general business failure occurred.",
    "resultStatus": "F"
  }
}
```

### EXCEED_CANCELLABLE_WINDOW

```json
{
  "result": {
    "resultCode": "CANCEL_WINDOW_EXCEED",
    "resultMessage": "The cancellation date exceeds the period during which cancellation is supported. The period is agreed upon in the contract.",
    "resultStatus": "F"
  }
}
```