# 承运商打单

向承运商下单：列出运输方式 → 询价 → 创建面单 → 下载 PDF → 追踪 → 接收事件通知 → 取消（或日结，如该方式要求）。

本专题仅覆盖下列接口，请按顺序调用。

请将 `YOUR_HOST`、`ACCESS_TOKEN` 与 `shipping_method` 替换为实际环境中的值。运输方式 `id` 因账号而异，请勿硬编码。

## 1. 登录

```bash
curl -X POST https://YOUR_HOST/api/v1/user/login \
  -H "Content-Type: application/json" \
  -d '{"email":"you@example.com","password":"your_password"}'
```

```
Authorization: Bearer ACCESS_TOKEN
```

GraphQL 请求提交至 `POST /api/graphql`，并使用相同的 Authorization 请求头。

[REST 手册](/api/documentation#/paths/v1-user-login/post) · [GraphQL 手册](/api/graphql/documentation#/user/userLogin)

**验证：** 登录成功后返回 `access_token`。后续请求若未携带该令牌，将返回 `401`。

## 2. 列出运输方式

**REST：** `POST /api/v1/labelservice/getShippingMethodList` — [REST 手册](/api/documentation#/paths/v1-labelservice-getShippingMethodList/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/getShippingMethodList \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"detail": true}'
```

返回列表中的每一项包含：

| 字段 | 用途 |
|---|---|
| `id` | 后续请求中的 `shipping_method` 参数 |
| `name` | 显示名 |
| `unique_identifier` | 稳定编码 |
| `options.signature_option` | 是否支持签名 |
| `options.insurance_option` | 是否支持保险 |
| `options.multi_package` | 是否支持多件 |

**GraphQL：** `labelserviceGetShippingMethodList` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceGetShippingMethodList))（JSON 标量）。

**验证：** 列表非空。你选出一个 `id`，并知道该方式是否支持签名、保险、多件。空列表说明账号上还没启用任何方式。

## 3. 询价

试算。会问承运商要价格，但不会订舱。报文形状与创建相同。`shipping_method` 必填。

**REST：** `POST /api/v1/labelservice/rate` — [REST 手册](/api/documentation#/paths/v1-labelservice-rate/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/rate \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "shipping_method": 59,
    "name": "Jane Recipient",
    "telephone": "5555555555",
    "email": "jane@example.com",
    "address_1": "6701 RUE HADLEY",
    "city": "Montreal",
    "province": "QC",
    "postcode": "H4E3R3",
    "country": "CA",
    "weight": 1,
    "length": 30,
    "width": 20,
    "height": 10,
    "dimension_unit": 2,
    "weight_unit": 2,
    "signature_option": 0,
    "insurance_option": 0,
    "package_type": "parcel",
    "ref": "DEV-LABEL-001",
    "sender_name": "Acme Warehouse",
    "sender_telephone": "5555555555",
    "sender_address_1": "2667 boul des sources",
    "sender_city": "Pointe-Claire",
    "sender_province": "QC",
    "sender_postcode": "H9S2H9",
    "sender_country": "CA"
  }'
```

`weight_unit`：`1` 克，`2` 千克，`3` 盎司，`4` 磅。`dimension_unit`：`1` 毫米，`2` 厘米，`3` 米，`4` 英寸。

多件用 `packages: [{ ref, weight, length, width, height, weight_unit, dimension_unit }]`。`shipping_from`（地址簿 id）或 `shipping_from_code` 可以代替 `sender_*`。

**GraphQL：** `labelserviceRate` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceRate))。

**验证：** `result` 为 true，并且有价格（承运商给了时效时也会带回）。没有价格就先改目的地 / 包裹 / 方式，**不要创建**。

## 4. 创建面单

此时会向承运商订舱。

**REST：** `POST /api/v1/labelservice/submitOrder` — [REST 手册](/api/documentation#/paths/v1-labelservice-submitOrder/post)

请求体与第 3 步相同，并须包含 `Idempotency-Key`。

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/submitOrder \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: dev-label-001" \
  -d '{
    "shipping_method": 59,
    "name": "Jane Recipient",
    "telephone": "5555555555",
    "email": "jane@example.com",
    "address_1": "6701 RUE HADLEY",
    "city": "Montreal",
    "province": "QC",
    "postcode": "H4E3R3",
    "country": "CA",
    "weight": 1,
    "length": 30,
    "width": 20,
    "height": 10,
    "dimension_unit": 2,
    "weight_unit": 2,
    "signature_option": 0,
    "insurance_option": 0,
    "package_type": "parcel",
    "ref": "DEV-LABEL-001",
    "sender_name": "Acme Warehouse",
    "sender_telephone": "5555555555",
    "sender_address_1": "2667 boul des sources",
    "sender_city": "Pointe-Claire",
    "sender_province": "QC",
    "sender_postcode": "H9S2H9",
    "sender_country": "CA"
  }'
```

**GraphQL：** `labelserviceSubmitOrder` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceSubmitOrder))。

```json
{
  "result": true,
  "id": 12345,
  "shipping_price": "12.50",
  "tracking_numbers": ["1Z999AA10123456784"],
  "external_id": "EXT_12345"
}
```

| 字段 | 用途 |
|---|---|
| `id` | Superroute 订单 id——下载和取消 |
| `tracking_numbers` | 提供给收件人；公开追踪接口可查询这些运单号 |
| `external_id` | 承运商货件 id |
| `shipping_price` | 实扣金额 |

**验证：** `tracking_numbers` 非空。保存 `id` 和运单号。同一 `Idempotency-Key` 不得再买第二张面单。

## 5. 下载 PDF

**REST：** `POST /api/v1/labelservice/getShippingLabel` — [REST 手册](/api/documentation#/paths/v1-labelservice-getShippingLabel/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/getShippingLabel \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "id": "12345",
    "type": "ORDER_ID",
    "base64": 1
  }'
```

`type` 可以是 `ORDER_ID`、`TRACKING_NUMBER` 或 `REF`。`base64: 0` 下发 PDF 流。这是**承运商官方面单**。件数在订舱时已固定。

**GraphQL：** `labelserviceGetShippingLabel` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceGetShippingLabel))。

**验证：** PDF 可正常打开，并显示第 4 步的承运商条码 / 运单号。测试面单仅供验证，请勿交付承运商。

## 6. 追踪

无需令牌。使用 `tracking_numbers` 里的号：

```bash
curl https://YOUR_HOST/api/v1/tracking/1Z999AA10123456784
```

[REST 手册](/api/documentation#/operations/getPublicTracking) · [GraphQL 手册](/api/graphql/documentation#/tracking/trackingPublic)

**GraphQL：**

```graphql
query {
  trackingPublic(trackingNumber: "1Z999AA10123456784") {
    result
    deliveried
    is_third_party_tracking
    data {
      tracking_event_status_id
      tracking_event_key
      description
      updated_at_localized
    }
    proofs { file_id type full_url signed_url }
  }
}
```

事件来自承运商时 `is_third_party_tracking` 为 true。按 `tracking_event_status_id` / `tracking_event_key` 分支，不要按 `description`。`data` 最新在前。承运商扫描前，早期事件可能仍是「信息已提交」。`500` 是已送达；此时 `proofs[]` 可能有签名（`type` `1`）或照片（`type` `2`）。

**验证：** 能查到刚创建的那票。未知运单号返回 `result: false` 或 HTTP 404。

## 7. 配置事件通知

| 配置项 | 事件 | 触发时机 |
|---|---|---|
| `order_create_webhook_url` | `order.created` | 保存 `id` 和承运商运单号 |
| `tracking_event_webhook_url` | `tracking.event` | 扫描、派送中、已送达 |
| `order_status_change_webhook_url` | `order.status_change` | 业务系统中的订单状态 |
| `order_cancel_failed_webhook_url` | `order.cancel_failed` | 因承运商已拿到货而取消失败 |

**REST:** `PUT /api/v1/webhook-settings` — [REST 手册](/api/documentation#/paths/v1-webhook-settings/put)

**GraphQL:** `webhookSettingsUpdate` ([GraphQL 手册](/api/graphql/documentation#/webhooks/webhookSettingsUpdate)).

```bash
curl -X PUT https://YOUR_HOST/api/v1/webhook-settings \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "order_create_webhook_url": "https://erp.example.com/hooks/superroute",
    "tracking_event_webhook_url": "https://erp.example.com/hooks/superroute",
    "webhook_sign_secret": "YOUR_LONG_RANDOM_SECRET",
    "webhook_verify_ssl": 1
  }'
```

用**原始报文**校验 **v2**：`HMAC_SHA256(timestamp + "." + raw_body, secret)` 对照 `X-Webhook-Signature-V2`。用 `X-Webhook-Event-Id` 去重。**3 秒内返回 2xx**。

```php
$raw = file_get_contents('php://input');
$ts  = $_SERVER['HTTP_X_WEBHOOK_TIMESTAMP'] ?? '';
$sig = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE_V2'] ?? '';
$expected = hash_hmac('sha256', $ts . '.' . $raw, $sharedSecret);
if (!hash_equals($expected, $sig)) {
    http_response_code(401);
    exit;
}
```

**验证：** 一次测试 `submitOrder` 能产生带那些 `tracking_numbers` 的 `order.created`。签名校验失败时，接收端应返回 `401`。

## 8. 取消

仅在承运商仍允许时（通常是揽收前）。

**REST：** `POST /api/v1/labelservice/cancelShippingLabel` — [REST 手册](/api/documentation#/paths/v1-labelservice-cancelShippingLabel/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/cancelShippingLabel \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"id":"12345","type":"ORDER_ID"}'
```

**GraphQL：** `labelserviceCancelShippingLabel` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceCancelShippingLabel))。

承运商已经拿到货会拒绝——对应 `order.cancel_failed`。

**验证：** 第二次取消是安全的。公开追踪不再将该运单视为有效在途件。

## 9. 日结（仅当该方式要求时）

有的承运商需要每日舱单。

**REST：** `POST /api/v1/labelservice/endofday` — [REST 手册](/api/documentation#/paths/v1-labelservice-endofday/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/labelservice/endofday \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"shipping_method": 59}'
```

**GraphQL：** `labelserviceEndofday` ([GraphQL 手册](/api/graphql/documentation#/labelservice/labelserviceEndofday))。

当天最后一张面单之后调用一次。第 2 步的方式没有这项要求时跳过。

**验证：** 成功体（或承运商确认）列出当天的面单。先在测试方式上跑通。

## 验收清单

用你能控制的目的地，以及可以取消的方式：

- [ ] 运输方式列表非空，并已记录一个 `id`。
- [ ] 该方式 + 目的地询价能返回价格。
- [ ] 提交返回 `tracking_numbers`；同一 `Idempotency-Key` 不会买第二张面单。
- [ ] 面单 PDF 可正常打开，并显示承运商运单号。
- [ ] 公开追踪能用该号查到。
- [ ] 应收到 `order.created`，且 v2 签名校验通过。
- [ ] 取消成功，**或**你已确认该方式订舱后不能取消。
- [ ] 若该方式需要日结，测试跑通无报错。
