> For the complete documentation index, see [llms.txt](https://lmile.gitbook.io/lmile-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lmile.gitbook.io/lmile-docs/webhooks/order-cancelled.md).

# Routing Cancel — 確保前キャンセル通知

[Routing Cancel](/lmile-docs/routing-api/cancel-update.md)（`/api/routing/cancel`）で**注文がキャンセルされたとき**、まだ**確保（secure）前**だった出荷元（shipment）について 1 通ずつ通知します。確保前に外部システムへ反映済みだった引き当て（例: 店舗 POS の理論在庫の先行減算）を**取り消す**用途を想定しています。確保後（物理確保済み）の shipment は通知しません。

> **Base URL** `https://api.lmile.io` · **認証** `x-ingest-secret` ヘッダー（[詳細](/lmile-docs/getting-started/authentication.md)） · `shop` はリクエストボディに含めます（省略するとキーのアカウント）

> 配信・リトライ・暗号化など共通の仕組みは [Webhooks 概要](/lmile-docs/webhooks/overview.md)を参照。

## 設定（送信先）

**専用の設定エンドポイントはありません。** [Routing Allocation Webhook](/lmile-docs/webhooks/allocation.md) の送信先設定（`POST /api/routing/allocation-webhook-config`・`shop` 単位）を**再利用**します（同一宛先 URL・同一認証 secret）。引き当て暫定確定通知と確保前キャンセル通知は同じチャネルに届き、受信側は後述の `event` フィールドで判別します。Routing Allocation の設定を登録済みであれば、追加の登録は不要です。

ペイロードは固定の cancel 専用テンプレート（`event: "routing_order_cancelled"`）で描画します（Routing Allocation の `payloadTemplate` 設定は引き当て通知専用で、本通知には適用されません）。

## ペイロード

送信ボディ（固定形・差し替え不可）:

```json
{
  "event": "routing_order_cancelled",
  "shop": "demo-store.myshopify.com",
  "routingOrderId": "a1b2c3d4-...",
  "shipmentId": "f9e8d7c6-...",
  "externalOrderRef": "1001",
  "sourceSystem": "shopify",
  "fromLocationId": "11112222-...",
  "cancelledAt": "2026-06-24T09:00:00.000Z",
  "changedAt": "2026-06-24T09:00:00.123Z"
}
```

| フィールド                               | 説明                                                                                               |
| ----------------------------------- | ------------------------------------------------------------------------------------------------ |
| `event`                             | 固定値 `routing_order_cancelled`（受信側はこの値で引き当て通知と判別）                                                 |
| `shop`                              | 店舗ドメイン                                                                                           |
| `routingOrderId`                    | ルーティング注文 id（`sot.lm_routing_orders.id`）                                                          |
| `shipmentId`                        | キャンセルされた出荷元ヘッダ id（`sot.lm_fulfillment_shipments.id`）。**受信側の冪等キー**。これを使って自身が登録済みの外部レコードを引いて取り消します |
| `externalOrderRef` / `sourceSystem` | 外部注文参照 / 取込元（相関用）                                                                                |
| `fromLocationId`                    | キャンセルされた shipment の引き当て先（在庫ロケーション）id（相関用）                                                        |
| `cancelledAt`                       | キャンセル確定時刻（cascade の `cancelled_at`、ISO8601）                                                      |
| `changedAt`                         | 通知生成時刻（ISO8601）                                                                                  |

明細（`lines`）は**含めません**。受信側は `shipmentId` をキーに、引き当て通知（`routing_allocation_decided`）受信時に作成した自身のレコードを引いて取り消すため、明細を運ぶ必要がなく、PII も載せません。

## 挙動

* **発火タイミング**: [`/api/routing/cancel`](/lmile-docs/routing-api/cancel-update.md) のキャンセル確定直後にバックグラウンド送信します（注文取消自体は常に確定し、本通知の成否に影響されません）
* **送信条件（確保前のみ）**: キャンセルされた shipment のうち、**確保前**（`secured_at IS NULL`）かつ**未出荷**（`status != shipped`）のものごとに 1 通。確保後（物理確保済み）・出荷済みの shipment は通知しません（= 自動で取り消さない）
* **イベントの分割**: 注文に複数の出荷元がある場合、確保前の shipment ごとに 1 通ずつ届きます
* **冪等性**: 注文単位のキャンセルは冪等で、確定済み注文の再キャンセルでは発火しません。受信側は `shipmentId` を冪等キーに、取り消し済みなら no-op としてください（同一 shipment への重複通知で二重取消しないこと）
* **耐久性**: イベントは送信前に outbox（`sot.lm_routing_cancel_events`）へ起票され、`pending` / `failed` のまま残ったものは [Webhooks 概要](https://lmile.gitbook.io/lmile-docs/webhooks/pages/OotbCjshtf2WFaZaqjCi#耐久性outbox)の自動 drain で回収・再送されます

## エラー

エンドポイント固有のエラーはありません（本通知の送信先設定は [Routing Allocation Webhook](/lmile-docs/webhooks/allocation.md) と共用）。共通エラーは [Webhooks 概要](https://lmile.gitbook.io/lmile-docs/webhooks/pages/OotbCjshtf2WFaZaqjCi#エラー) / [Error Codes](/lmile-docs/reference/error-codes.md) を参照。

## データモデル

* `sot.lm_routing_cancel_events` — キャンセルイベント outbox + 送信ログ（`shipment_id` / `cancelled_at` / `delivery_status` を記録。明細なし）
* 送信設定: `sot.lm_routing_allocation_webhook_config`（[Routing Allocation Webhook](/lmile-docs/webhooks/allocation.md) と共用）
* キャンセルの元: [`sot.lm_fulfillment_shipments`](/lmile-docs/fulfillment-api/fulfillment.md)（`secured_at IS NULL` の未出荷ヘッダのみ対象）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://lmile.gitbook.io/lmile-docs/webhooks/order-cancelled.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
