> ## 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 a Subscription

Localpayment allows you to cancel active subscriptions when customers no longer wish to continue their recurring payments. This guide details the process for canceling a subscription through Localpayment's API, including immediate cancellations and end-of-billing period options.

## 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.

***

## Step 1: Cancel a Subscription

To cancel an existing subscription, send a `PATCH` request to the [Cancel a Subscription](https://docs.localpayment.com/reference/cancel-subscription.md) endpoint.

### Key Request Parameters

The request requires several key objects:

| Parameter | Description | Required |
| - | - | - |
| `status` | Must be set to `CANCEL` 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/payin/subscriptions/{externalId}/status \
 		 --header 'Authorization: Bearer <your_access_token>' \
     --header 'accept: application/json' \
     --header 'content-type: application/json'
     --data-raw '{
    "status": "CANCEL"
}'

```

> [!WARNING]
>
> **Important**
>
> Once a subscription is cancelled, it cannot be reactivated. You will need to create a new subscription if the customer wishes to resume service.

***

## Step 2: Handle the Response

### Successful Response

A successful cancellation returns the subscription cancellation confirmation:

```json theme={"theme":{"light":"github-light-high-contrast","dark":"github-dark-high-contrast"}}
{
    "externalId": "a2966037-52a1-4a73-9173-b682969b34ff",
    "internalId": "1ef01791-0e51-4b66-9d45-229895cd49d0",
    "status": {
        "code": "903",
        "description": "CANCELLED",
        "detail": "The subscription was canceled - merchant request"
    }
}
```

### Key Response Fields

| Parameter | Description | Use Case |
| - | - | - |
| `internalId` | Localpayment's unique transaction identifier. | Transaction tracking and support. |
| `externalId` | Your unique reference for the subscription. | Internal reconciliation. |
| `status.code` | Cancellation status code (`903` for successful cancellation). | Status monitoring. |
| `status.description` | Human-readable status description. | User interface display. |
| `status.detail` | Detailed cancellation information. | Audit trail. |

### Error Response

When a subscription cancellation 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 externalID Subscription Not Found"
    }
}
```

***

## Step 3: Track Transaction Status

After cancelling a subscription, monitor the status 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 Status Codes

| Code | Status | Description |
| :- | :- | :- |
| `400` | `ERROR` | Cancellation request failed. |
| `903` | `CANCELLED` | Subscription has been 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 an active subscription with valid credentials.
2. **Already Cancelled Subscription**: Attempt to cancel already cancelled subscription.
3. **Nonexistent Subscription**: Cancel a subscription that doesn't exist.
4. **Authentication Errors**: Cancel with invalid or missing authentication.
5. **Webhook Notifications**: Confirm cancellation webhooks are triggered

***

## Next Steps

After cancelling a subscription, you may need to perform additional actions:

> [card] [Create new subscription](https://docs.localpayment.com/docs/create-a-subscription.md)
>
> Create a new subscription if the customer wishes to resume service with a
> different plan or payment method.

> [card] [Set up webhooks](https://docs.localpayment.com/docs/webhooks.md)
>
> Configure webhooks to receive real-time notifications for subscription
> events.


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