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

# 取消订阅（Customer）

> 使用 customer session token 由 customer 浏览器侧取消订阅

本页是 [取消订阅（Merchant API Key）](/zh/api-reference/endpoints/subscriptions/cancel-subscription) 同一端点的 customer-token 调用视图。行为、请求体、响应结构完全一致 — 只有认证方式和调用方位置不同。

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

**认证方式：** Session Token — 见 [Customer Endpoints](/zh/api-reference/endpoints/auth/customer-endpoints)（customer 或 customer 角色）

<Note>
  `orderId` 必须属于该 session token 所绑定的 customer。为某 customer mint 的 token 不能取消另一 customer 的订阅。
</Note>

## 取消行为

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

## 请求体

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

## 请求示例

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

  const client = new WaffoPancake({
    sessionToken: window.WAFFO_SESSION_TOKEN, // 由商户 portal 注入
    environment: "prod",
  });

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

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

  ```typescript TypeScript (Manual) theme={"system"}
  const result = await fetch("https://api.waffo.ai/v1/actions/subscription-order/cancel-order", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${SESSION_TOKEN}`,
      "Content-Type": "application/json",
      "X-Environment": "prod",
    },
    body: JSON.stringify({
      orderId: "ORD_2aUyqjCzEIiEcYMKj7TZtw",
    }),
  }).then(r => r.json());
  ```

  ```bash cURL theme={"system"}
  curl -X POST "https://api.waffo.ai/v1/actions/subscription-order/cancel-order" \
    -H "Authorization: Bearer $SESSION_TOKEN" \
    -H "Content-Type: application/json" \
    -H "X-Environment: prod" \
    -d '{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}'
  ```

  ```bash wget theme={"system"}
  wget -qO- \
    --header="Authorization: Bearer $SESSION_TOKEN" \
    --header="Content-Type: application/json" \
    --header="X-Environment: prod" \
    --post-data='{"orderId":"ORD_2aUyqjCzEIiEcYMKj7TZtw"}' \
    "https://api.waffo.ai/v1/actions/subscription-order/cancel-order"
  ```
</CodeGroup>

## 成功响应 (200)

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

### 响应字段

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

## 错误响应

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

| 状态码 | `errors[0].message`                                  | 含义                                                              | 推荐处理                        |
| --- | ---------------------------------------------------- | --------------------------------------------------------------- | --------------------------- |
| 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`                              | Session token 无效、过期或格式错误                                        | 通过 Issue Session Token 重新签发 |
| 403 | `Order does not belong to user`                      | Token 绑定的 customer 不是订单所有者                                      | 为正确的 customer mint 一个 token |
| 404 | `Order not found`                                    | 订单不存在                                                           | 验证 order ID                 |
| 500 | `Internal server error`                              | 服务端意外失败                                                         | 指数退避重试（起步 5s，最多 3 次）        |
| 502 | `Failed to cancel subscription`                      | 本地更新或 PSP 取消失败                                                  | 指数退避重试（起步 5s，最多 3 次）        |
