# 仓储与出库

先将货物入库存储，再将在库包裹出库发运：读取配置 → 询价 → 创建仓储订单 → 支付 → 查询在库包裹 → 出库估价 → 创建出库订单 → 支付 → 追踪 → 取消。

本专题仅覆盖下列接口，请按顺序调用。本流程使用**客户**账号。

请将 `YOUR_HOST` 与 `ACCESS_TOKEN` 替换为实际环境中的值。

## 1. 登录（客户）

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

```
Authorization: Bearer ACCESS_TOKEN
```

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

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

## 2. 仓储配置

**REST：** `GET /api/v1/customer/storage-orders/config` — [REST 手册](/api/documentation#/paths/v1-customer-storage-orders-config/get)

```bash
curl https://YOUR_HOST/api/v1/customer/storage-orders/config \
  -H "Authorization: Bearer ACCESS_TOKEN"
```

**GraphQL：** `customerStorageOrderConfig` ([GraphQL 手册](/api/graphql/documentation#/customer/customerStorageOrderConfig))

**验证：** 已记录仓库 `id`；若包装目录非空，同时记录包装 `id`。

## 3. 仓储询价

**REST：** `POST /api/v1/customer/storage-orders/calculate-price`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders/calculate-price \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "warehouse_id": 7,
    "start_date": "2026-09-10",
    "end_date": "2026-10-10",
    "items": [{
      "qty": 1,
      "length": 30,
      "width": 20,
      "height": 15,
      "dimension_unit": 2,
      "weight": 2,
      "weight_unit": 2
    }]
  }'
```

**验证：** `success` 或 `result` 为 true，并且有价格。缺少 `warehouse_id` / 日期是 `400`。

## 4. 创建仓储单

**REST：** `POST /api/v1/customer/storage-orders`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: dev-store-001" \
  -d '{
    "warehouse_id": 7,
    "start_date": "2026-09-10",
    "end_date": "2026-10-10",
    "items": [{
      "description": "Box A",
      "qty": 1,
      "length": 30,
      "width": 20,
      "height": 15,
      "dimension_unit": 2,
      "weight": 2,
      "weight_unit": 2,
      "value": 100
    }]
  }'
```

**验证：** 响应包含 `data.id`。请保存该仓储订单 id。

## 5. 支付仓储

**REST：** `POST /api/v1/customer/storage-orders/{id}/pay`

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/storage-orders/1024/pay \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

可先调用 `GET /api/v1/customer/storage-orders/{id}/payment-info`。

**REST：** `GET /api/v1/customer/storage-orders/{id}` — [REST 手册](/api/documentation#/paths/v1-customer-storage-orders-id/get)

**GraphQL：** `customerStorageOrderShow` ([GraphQL 手册](/api/graphql/documentation#/customer/customerStorageOrderShow))

**验证：** 仓储单已支付 / 确认。`402` 表示须先为钱包充值后再重试。

出库接口仅在仓库完成收货后可用。测试时须等待收货完成后再继续。

## 6. 查询在库包裹

**REST：** `GET /api/v1/customer/shipout-orders/available-items` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-available-items/get)

```bash
curl "https://YOUR_HOST/api/v1/customer/shipout-orders/available-items?warehouse_id=7" \
  -H "Authorization: Bearer ACCESS_TOKEN"
```

**GraphQL：** `customerShipoutAvailableItems` ([GraphQL 手册](/api/graphql/documentation#/customer/customerShipoutAvailableItems))

**验证：** 已记录至少一个 `storage_package_ids`（示例 `5001`）。空列表说明尚未收货，请勿创建出库订单。`403` 表示该客户未开通出库功能。

## 7. 出库估价并创建

**REST：** `GET /api/v1/customer/shipout-orders/services?warehouse_id=7` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-services/get)

记录 `service_code`。

**REST：** `POST /api/v1/customer/shipout-orders/services/{serviceCode}/estimate` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-services-serviceCode--estimate/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/services/intl_express/estimate \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "warehouse_id": 7,
    "delivery_postcode": "M5V2H1",
    "delivery_country": "CA",
    "packages": [{
      "weight": 2.5,
      "length": 30,
      "width": 20,
      "height": 15,
      "weight_unit": 2,
      "dimension_unit": 2
    }]
  }'
```

**REST：** `POST /api/v1/customer/shipout-orders/services/{serviceCode}/orders` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-services-serviceCode--orders/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/services/intl_express/orders \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: dev-shipout-001" \
  -d '{
    "warehouse_id": 7,
    "storage_package_ids": [5001],
    "delivery_name": "Jane Recipient",
    "delivery_telephone": "5555555555",
    "delivery_email": "jane@example.com",
    "delivery_address_1": "123 King St W",
    "delivery_city": "Toronto",
    "delivery_province": "ON",
    "delivery_country": "CA",
    "delivery_postcode": "M5V2H1"
  }'
```

**GraphQL：** `customerCreateShipoutOrder` ([GraphQL 手册](/api/graphql/documentation#/customer/customerCreateShipoutOrder))

**验证：** 响应包含出库订单 `id`。所选仓储包裹已锁定至该出库请求。

## 8. 支付出库、追踪并订阅事件

**REST：** `POST /api/v1/customer/shipout-orders/{id}/pay` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-id--pay/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/8001/pay \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

**GraphQL：** `customerPayShipout` ([GraphQL 手册](/api/graphql/documentation#/customer/customerPayShipout))

**REST：** `GET /api/v1/customer/shipout-orders/{id}` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-id/get)

有运单号时：

```bash
curl https://YOUR_HOST/api/v1/tracking/SR123456789012
```

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

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

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

校验 **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;
}
```

**验证：** 支付成功并记录金额（或 `402` / `422` 原因清楚）。生成运单号后，公开追踪接口可查询该票。

## 9. 取消测试出库订单

**REST：** `POST /api/v1/customer/shipout-orders/{id}/cancel` — [REST 手册](/api/documentation#/paths/v1-customer-shipout-orders-id--cancel/post)

```bash
curl -X POST https://YOUR_HOST/api/v1/customer/shipout-orders/8001/cancel \
  -H "Authorization: Bearer ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
```

**GraphQL：** `customerCancelShipout` ([GraphQL 手册](/api/graphql/documentation#/customer/customerCancelShipout))

取消成功后将释放仓储包裹锁定。仓储订单在仍允许取消时，使用 `POST /api/v1/customer/storage-orders/{id}/cancel` 取消。

**验证：** `422` 表示当前状态不允许取消。出库取消成功后，第 6 步将重新列出这些包裹。

## 验收清单

- [ ] 仓储配置返回仓库 `id`。
- [ ] 仓储询价返回价格。
- [ ] 创建仓储返回 `data.id`。
- [ ] 仓储支付成功，**或**已确认钱包需充值。
- [ ] 在库件列出已收货包裹（`storage_package_ids`）。
- [ ] 出库估价返回价格或 `has_items_needing_quote`。
- [ ] 创建出库返回 `id` 并锁定这些包裹。
- [ ] 出库支付成功（或理解 `402` / `422`）。
- [ ] 生成运单号后，公开追踪接口可查询该票。
- [ ] 出库取消释放包裹，**或**该状态不能取消。
