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

# 取消订阅

> 取消活跃或待处理的订阅

取消活跃或待处理的订阅。行为取决于当前订阅状态。

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

**认证方式：** API Key（merchant、owner 角色）

<Note>
  此端点也有 customer 侧的 session-token 调用流程：见 [取消订阅（Customer）](/zh/api-reference/endpoints/subscriptions/cancel-subscription-customer)。
</Note>

## 取消行为

| 当前状态      | 操作               | 结果状态                              |
| --------- | ---------------- | --------------------------------- |
| `pending` | 立即取消             | `canceled`                        |
| `active`  | 在周期结束时取消（通过 PSP） | `canceling` -> 周期结束时变为 `canceled` |

<Note>
  * **pending**：直接取消，状态变为 `canceled`
  * **active**：触发 PSP 取消（在计费周期结束时生效）。本地状态变为 `canceling`，然后在周期结束时通过 Webhook 更新为 `canceled`
</Note>

## 请求体

| 字段        | 类型     | 必需 | 说明                             |
| --------- | ------ | -- | ------------------------------ |
| `orderId` | string | 是  | 订阅订单 ID（Short ID 格式 `ORD_xxx`） |

## 请求示例

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

## 成功响应 (200) -- 活跃订阅

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

## 成功响应 (200) -- 待处理订阅

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

### 响应字段

| 字段        | 类型     | 说明                               |
| --------- | ------ | -------------------------------- |
| `orderId` | string | 订单 ID（Short ID）                  |
| `status`  | string | 新的订单状态（`canceling` 或 `canceled`） |

## 错误响应

> **重试策略**：4xx 一律不要重试 — 修正请求后重发。5xx 指数退避重试（起步 5s，最多 3 次）。

| 状态码 | `errors[0].message`                                  | 含义                                                              | 推荐处理                 |
| --- | ---------------------------------------------------- | --------------------------------------------------------------- | -------------------- |
| 400 | `Missing X-Context-Merchant-Id header`               | API Key 认证下 merchant 上下文缺失                                      | 检查认证流水线              |
| 400 | `Missing required field: orderId`                    | 请求体未提供 `orderId`                                                | 修正请求体后重新提交           |
| 400 | `Expected format: ORD_xxx, got "..."`                | `orderId` Short ID 解码失败                                         | 修正 `orderId` 格式后重新提交 |
| 400 | `Subscription cannot be canceled, current status: X` | 订单状态非 `pending` 或 `active`（如已 `canceled`、`canceling`、`expired`） | 该订阅已不可取消             |
| 401 | `Authentication failed`                              | API Key 签名无效                                                    | 检查认证请求头              |
| 403 | `Order does not belong to user`                      | 归属校验失败                                                          | 验证调用方拥有该订单           |
| 404 | `Order not found`                                    | 订单不存在                                                           | 验证 order ID          |
| 500 | `Internal server error`                              | 服务端意外失败                                                         | 指数退避重试（起步 5s，最多 3 次） |
| 502 | `Failed to cancel subscription`                      | 本地更新或 PSP 取消失败                                                  | 指数退避重试（起步 5s，最多 3 次） |
