> ## Documentation Index
> Fetch the complete documentation index at: https://docs.localpayment.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cancel an APM Payment

Localpayment enables you to cancel pending APM payments through API integration. This guide covers the complete cancellation workflow for APM transactions using the direct cancellation method that requires only the transaction's externalId.

## Before You Begin

Ensure you have:

> [card] [API Credentials](https://docs.localpayment.com/docs/api-keys-and-environments.md)
>
> Valid API credentials and access token for authentication.

> [card] [Account Setup](https://docs.localpayment.com/docs/make-your-first-test-payment.md)
>
> Proper account setup for your target country and business verification.

> [card] [IP Whitelisting](https://docs.localpayment.com/docs/dashboard-whitelist-your-ips.md)
>
> Whitelist your server IP addresses for API access.

> [card] [Webhook Endpoint](https://docs.localpayment.com/docs/webhooks.md)
>
> Set up a webhook URL to receive payment notifications.

### Additional Cancellation Requirements:

* User role with cancellation permissions (see [User Roles](https://docs.localpayment.com/docs/user-roles-and-permissions.md) documentation).
* The original transaction must be in a cancellable state.
* The `externalId` of the APM payment transaction you wish to cancel.
* Complete beneficiary information for data-required refunds.

***

## Direct Cancellation for APM Payments

APM payment cancellations are processed as **Direct Cancellations**, meaning you only need the original transaction's `externalId` and the `status` parameter set to `CANCELLED`. The system immediately stops any further processing of the payment.

> [!WARNING]
>
> **Important**
>
> APM payments can only be cancelled while they are in a pending or in-progress state. Once a APM payment has been completed or settled, you must process a refund instead of a cancellation.

***

## Step 1: Cancel APM Payment via API

To cancel a APM payment, send a `PATCH` request to the [Cancel](https://docs.localpayment.com/reference/cancel-payin.md) endpoint.

### Key Request Parameters

The request requires several key parameters:

| Parameter | Description | Required |
| - | - | - |
| `externalId` | The original transaction's externalId | ✅ |
| `status` | Must be set to `CANCELLED` to initiate cancellation | ✅ |

### Example Request

Below is an example using `curl`:

```bash curl theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
curl --request PATCH \
  --url 'https://api.stage.localpayment.com/api/transactions/{{externalId}}/status' \
  --header 'Authorization: Bearer <your_access_token>' \
  --header 'Content-Type: application/json' \
  --data '{
    "status": "CANCELLED"
  }'
```

See all [available parameters](https://docs.localpayment.com/reference/cancel-payin.md) in the request.

***

## Step 2: Handle the Response

### Successful Response

A successfully cancelled APM payment returns confirmation details:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
  "externalId": "1c364061-1f3d-4b94-c270-bf915c22e8f5",
  "internalId": "8a9abd31-31e0-429a-32a6-cca8b9e1e536",
  "status": {
    "code": "900",
    "description": "CANCELLED",
    "detail": "The payin was cancelled"
  }
}
```

### Key Response Fields

| Parameter | Description | Use Case |
| - | - | - |
| `externalId` | Your reference for the original transaction. | Reconciliation and tracking. |
| `internalId` | Localpayment's unique refund identifier. | Support and audit purposes. |
| `status.code` | Cancellation status code (`900` for successful cancellation). | Status monitoring. |

### Error Response

When a cancellation request fails, you'll receive error information:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
  "status": {
    "code": "400",
    "description": "ERROR",
    "detail": "Invalid operation"
  }
}
```

### Common Error Scenarios

| Error | Description | Solution |
| - | - | - |
| Transaction Not Found | The provided `externalId` does not exist or is invalid | Verify the externalId is correct and the transaction exists in your transaction history |
| Invalid Status | The original transaction is not in a cancellable state | Check the transaction status and ensure it is still pending or in progress before attempting cancellation |
| Permission Denied | Your user role lacks necessary refund permissions | Contact your account administrator to grant appropriate refund permissions for your role |
| Already Completed | The transaction has already been completed and cannot be cancelled | Process a refund instead of a cancellation for completed transactions |
| Invalid Parameter | The `status` parameter is missing or not set to `CANCELLED` | Ensure the request body contains `{"status": "CANCELLED"}` exactly as specified |

***

## Step 3: Track Cancellation Status

After initiating a cancellation, monitor its progress through these methods:

> [card] [Webhooks (Recommended)](https://docs.localpayment.com/docs/webhooks.md)
>
> Receive real-time notifications when transaction status changes. Most efficient for production environments.

> [card] [Status API Endpoint](https://docs.localpayment.com/reference/get-transaction-status.md)
>
> Query transaction status programmatically. Useful for polling or recovering lost webhook notifications.

> [card] [Business Portal View](https://docs.localpayment.com/docs/dashboard-overview.md)
>
> Visual interface for monitoring transactions. Great for manual oversight and detailed transaction analysis.

### Common Cancellation Status Codes

| Code | Status | Description |
| :- | :- | :- |
| `400` | `ERROR` | Cancellation request failed due to validation errors or invalid parameters. |
| `900` | `CANCELLED` | The payin was successfully cancelled. |

> [!NOTE]
>
> **Complete Reference**
>
> View all status codes in our [Transaction Status Documentation](https://docs.localpayment.com/reference/transaction-status-error-codes).

***

## Testing Your Integration

### Test Scenarios

Verify your integration handles these scenarios:

1. **Successful Cancellation**: Cancel a pending APM payment with valid externalId.
2. **Invalid Transaction**: Try to cancel non-existent externalId.
3. **Already Completed**: Attempt to cancel a completed transaction.
4. **Permission Validation**: Test with users having different permission levels.
5. **Missing Status Parameter**: Attempt cancellation without the required status field.

***

## Next Steps

> [card] [View transaction status](https://docs.localpayment.com/reference/get-transaction-status.md)
>
> Check the current status of transactions and cancellations using the
> Transaction Status API.

> [card] [Process refunds](https://docs.localpayment.com/docs/refund-apm-payment.md)
>
> Learn how to process refunds for completed APM payments that cannot be
> cancelled.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.