> ## 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）](/ja/api-reference/endpoints/subscriptions/cancel-subscription) と同一エンドポイントの customer-token 呼び出し視点です。動作、リクエストボディ、レスポンス構造は同じで、認証方式と呼び出し元のみが異なります。

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

**認証：** Session Token — [Customer Endpoints](/ja/api-reference/endpoints/auth/customer-endpoints) を参照（customer または customer ロール）

<Note>
  `orderId` は session token がバインドされている customer に属している必要があります。ある customer 用に mint されたトークンは、別の 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, // マーチャントのポータルから注入
    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`                      | トークンの customer が注文の所有者ではない                                                | 正しい customer 用にトークンを mint  |
| 404   | `Order not found`                                    | 注文が存在しない                                                                  | order ID を確認               |
| 500   | `Internal server error`                              | サーバ側の予期しない障害                                                              | 指数バックオフでリトライ（5s 開始、最大 3 回） |
| 502   | `Failed to cancel subscription`                      | ローカル更新または PSP キャンセル失敗                                                     | 指数バックオフでリトライ（5s 開始、最大 3 回） |
