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

# Cancel Subscription

> Cancel an active or pending subscription

Cancel an active or pending subscription. The behavior depends on the current subscription status.

```
POST /v1/actions/subscription-order/cancel-order
```

**Authentication:** API Key (merchant, owner role)

<Note>
  This endpoint also has a customer-side flow with session-token auth: see [Cancel Subscription (Customer)](/api-reference/endpoints/subscriptions/cancel-subscription-customer).
</Note>

## Cancellation Behavior

| Current Status | Action                         | Result Status                           |
| -------------- | ------------------------------ | --------------------------------------- |
| `pending`      | Immediate cancel               | `canceled`                              |
| `active`       | Cancel at period end (via PSP) | `canceling` -> `canceled` at period end |

<Note>
  * **pending**: Directly canceled, status becomes `canceled`
  * **active**: PSP cancellation is triggered (takes effect at billing period end). Local status becomes `canceling`, then updated to `canceled` via Webhook when the period ends
</Note>

## Request Body

| Field     | Type   | Required | Description                                       |
| --------- | ------ | -------- | ------------------------------------------------- |
| `orderId` | string | Yes      | Subscription order ID (Short ID format `ORD_xxx`) |

## Example Request

<CodeGroup>
  ```typescript TypeScript (SDK) theme={"system"}
  import { WaffoPancake } from "@waffo/pancake-ts";

  const client = new WaffoPancake({
    merchantId: process.env.WAFFO_MERCHANT_ID!,
    privateKey: process.env.WAFFO_PRIVATE_KEY!,
  });

  const result = await client.orders.cancelSubscription({
    orderId: "ORD_2aUyqjCzEIiEcYMKj7TZtw",
  });

  console.log(result.orderId); // "ORD_2aUyqjCzEIiEcYMKj7TZtw"
  console.log(result.status);  // "canceling" or "canceled"
  ```

  ```typescript TypeScript (Manual) theme={"system"}
  // Assumes callApi() is defined as shown in the Authentication guide
  const result = await callApi("POST", "/v1/actions/subscription-order/cancel-order", {
    orderId: "ORD_2aUyqjCzEIiEcYMKj7TZtw",
  });
  ```

  ```java Java theme={"system"}
  // Assumes callApi() is defined as shown in the Authentication guide
  String result = callApi("POST", "/v1/actions/subscription-order/cancel-order",
      "{\"orderId\":\"ORD_2aUyqjCzEIiEcYMKj7TZtw\"}");
  ```

  ```python Python theme={"system"}
  # Assumes call_api() is defined as shown in the Authentication guide
  result = call_api("POST", "/v1/actions/subscription-order/cancel-order", {
      "orderId": "ORD_2aUyqjCzEIiEcYMKj7TZtw",
  })
  ```

  ```go Go theme={"system"}
  // Assumes callAPI() is defined as shown in the Authentication guide
  result, err := callAPI("POST", "/v1/actions/subscription-order/cancel-order",
      `{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}`)
  ```

  ```rust Rust theme={"system"}
  // Assumes call_api() is defined as shown in the Authentication guide
  let result = call_api("POST", "/v1/actions/subscription-order/cancel-order",
      r#"{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}"#
  ).await?;
  ```

  ```c C theme={"system"}
  // Assumes call_api() is defined as shown in the Authentication guide
  call_api("/v1/actions/subscription-order/cancel-order",
      "{\"orderId\":\"ORD_2aUyqjCzEIiEcYMKj7TZtw\"}");
  ```

  ```cpp C++ theme={"system"}
  // Assumes call_api() is defined as shown in the Authentication guide
  auto result = call_api("/v1/actions/subscription-order/cancel-order",
      R"({"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"})");
  ```

  ```bash cURL theme={"system"}
  MERCHANT_ID="MER_2aUyqjCzEIiEcYMKj7TZtw"
  TIMESTAMP=$(date +%s)
  BODY='{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}'
  BODY_HASH=$(echo -n "$BODY" | openssl dgst -sha256 -binary | base64 -w 0)
  CANONICAL="POST
  /v1/actions/subscription-order/cancel-order
  $TIMESTAMP
  $BODY_HASH"

  SIGNATURE=$(echo -n "$CANONICAL" | openssl dgst -sha256 -sign private_key.pem | base64 -w 0)

  curl -X POST "https://api.waffo.ai/v1/actions/subscription-order/cancel-order" \
    -H "Content-Type: application/json" \
    -H "X-Merchant-Id: $MERCHANT_ID" \
    -H "X-Timestamp: $TIMESTAMP" \
    -H "X-Signature: $SIGNATURE" \
    -d "$BODY"
  ```

  ```bash wget theme={"system"}
  MERCHANT_ID="MER_2aUyqjCzEIiEcYMKj7TZtw"
  TIMESTAMP=$(date +%s)
  BODY='{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}'
  BODY_HASH=$(echo -n "$BODY" | openssl dgst -sha256 -binary | base64 -w 0)
  CANONICAL="POST
  /v1/actions/subscription-order/cancel-order
  $TIMESTAMP
  $BODY_HASH"

  SIGNATURE=$(echo -n "$CANONICAL" | openssl dgst -sha256 -sign private_key.pem | base64 -w 0)

  wget -qO- --post-data="$BODY" \
    --header="Content-Type: application/json" \
    --header="X-Merchant-Id: $MERCHANT_ID" \
    --header="X-Timestamp: $TIMESTAMP" \
    --header="X-Signature: $SIGNATURE" \
    "https://api.waffo.ai/v1/actions/subscription-order/cancel-order"
  ```
</CodeGroup>

## Success Response (200) -- Active Subscription

```json theme={"system"}
{
  "data": {
    "orderId": "ORD_2aUyqjCzEIiEcYMKj7TZtw",
    "status": "canceling"
  }
}
```

## Success Response (200) -- Pending Subscription

```json theme={"system"}
{
  "data": {
    "orderId": "ORD_2aUyqjCzEIiEcYMKj7TZtw",
    "status": "canceled"
  }
}
```

### Response Fields

| Field     | Type   | Description                                  |
| --------- | ------ | -------------------------------------------- |
| `orderId` | string | Order ID (Short ID)                          |
| `status`  | string | New order status (`canceling` or `canceled`) |

## Errors

> **Retry policy:** Never retry 4xx — fix the request and resubmit. Retry 5xx with exponential backoff (start 5s, max 3 attempts).

| Status | `errors[0].message`                                  | What it means                                                                               | Recommended handling                                      |
| ------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------------- | --------------------------------------------------------- |
| 400    | `Missing X-Context-Merchant-Id header`               | Merchant context missing in API Key auth                                                    | Verify the auth pipeline                                  |
| 400    | `Missing required field: orderId`                    | `orderId` was not provided in the body                                                      | Fix the request body, then resubmit                       |
| 400    | `Expected format: ORD_xxx, got "..."`                | `orderId` Short ID could not be decoded                                                     | Fix the `orderId` format, then resubmit                   |
| 400    | `Subscription cannot be canceled, current status: X` | Order status is not `pending` or `active` (e.g. already `canceled`, `canceling`, `expired`) | The subscription is no longer cancellable                 |
| 401    | `Authentication failed`                              | Invalid API Key signature                                                                   | Verify auth headers                                       |
| 403    | `Order does not belong to user`                      | Ownership check failed                                                                      | Verify the caller owns the order                          |
| 404    | `Order not found`                                    | Order does not exist                                                                        | Verify the order ID                                       |
| 500    | `Internal server error`                              | Unexpected server-side failure                                                              | Retry with exponential backoff (start 5s, max 3 attempts) |
| 502    | `Failed to cancel subscription`                      | Local update or PSP cancellation failed                                                     | Retry with exponential backoff (start 5s, max 3 attempts) |
