openapi: 3.1.0
info:
  title: Wasabi API
  version: '1.0'
  description: |
    **Wasabi** 数字商店的合作伙伴 API：订阅、游戏和充值码目录。
    下单、从合作伙伴余额扣款以及发货都在**一次请求**中完成——
    `POST /v1/orders` 的响应中会直接返回账号、兑换码或充值确认。

    ## 身份验证

    所有请求都需要在 `Authorization` 请求头中携带 API 密钥：

    ```http
    Authorization: Bearer wsb_live_9f2c4a7e1b8d3f6a
    ```

    密钥分为两种：`wsb_live_…` 为正式环境密钥，`wsb_test_…` 为测试密钥。
    密钥可在合作伙伴后台中创建和吊销。请勿在前端代码中暴露密钥，
    也不要将其提交到代码仓库。

    ## 幂等性

    创建订单时请传入 `Idempotency-Key` 请求头（任意不超过 64 个字符的唯一字符串，例如 UUID）。
    使用相同密钥重复请求将返回已创建的订单，**不会重复扣款**——因此在网络中断后可以安全地重试请求。
    该密钥保留 24 小时。如果使用相同密钥发送不同的请求体，API 将返回 `409 idempotency_conflict`。

    ## Webhook

    在合作伙伴后台填写 URL 后，Wasabi 会通过 `POST` 推送事件
    （`order.delivered`、`order.processing`、`order.failed`、`balance.topup`）。
    每个请求都带有签名：`X-Wasabi-Signature` 请求头包含使用 Webhook 密钥（`whsec_…`）
    对请求体计算的 HMAC-SHA256 值（hex 格式）。

    ```
    X-Wasabi-Signature: t=1764763200,v1=5f8a3c…e91b
    ```

    签名基于字符串 `{t}.{raw_body}` 计算。请以恒定时间比较签名，
    并拒绝超过 5 分钟的事件。如果您的服务器未返回 `2xx`，系统将按指数退避
    （1 分钟、5 分钟、30 分钟、2 小时、6 小时……）重试投递，最长 24 小时，直到收到 `2xx`。
    事件可能重复送达——请根据 `data.id` 进行幂等处理。

    ## 测试模式

    使用 `wsb_test_…` 密钥时，API 的行为与正式环境完全一致，但不会产生真实扣款和采购：
    订单返回测试数据，余额为虚拟余额，Webhook 发送到测试 URL。
    测试数据与正式数据完全隔离。

    ## MCP

    为 AI 代理（Claude、ChatGPT、Cursor 等）提供 MCP 服务器：

    ```
    https://api.wasabi.guru/mcp
    ```

    使用相同的 Bearer 密钥授权。工具列表：

    | 工具 | 功能 |
    |---|---|
    | `search_products` | 按类别筛选搜索目录 |
    | `create_order` | 创建订单并发货 |
    | `get_order` | 订单状态和内容 |
    | `get_balance` | 合作伙伴当前余额 |

    在流程调试完成之前，建议为代理使用测试密钥。
  contact:
    name: Wasabi 支持
    url: https://wasabi.guru
servers:
- url: https://api.wasabi.guru
  description: 正式和测试模式（由密钥决定）
security:
- bearerAuth: []
tags:
- name: 目录
  description: 商品、价格、库存以及下单所需的数据。
- name: 订单
  description: 创建即时发货的订单并查看历史记录。
- name: 余额
  description: 合作伙伴余额和交易记录。
- name: Webhook
  description: Wasabi 发送到您服务器的事件。
paths:
  /v1/products:
    get:
      tags:
      - 目录
      operationId: listProducts
      summary: 商品列表
      description: 返回带有合作伙伴价格的目录商品。支持按类别筛选、搜索和分页。
      parameters:
      - name: category
        in: query
        description: 商品类别。
        schema:
          $ref: '#/components/schemas/Category'
        example: ai
      - name: q
        in: query
        description: 按名称搜索。
        schema:
          type: string
        example: chatgpt
      - name: page
        in: query
        description: 页码，从 1 开始。
        schema:
          type: integer
          minimum: 1
          default: 1
      - name: limit
        in: query
        description: 每页商品数。
        schema:
          type: integer
          minimum: 1
          maximum: 100
          default: 20
      responses:
        '200':
          description: 商品列表。
          content:
            application/json:
              schema:
                type: object
                required:
                - data
                - page
                - limit
                - total
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Product'
                  page:
                    type: integer
                    example: 1
                  limit:
                    type: integer
                    example: 20
                  total:
                    type: integer
                    description: 符合筛选条件的商品总数。
                    example: 3
              example:
                data:
                - id: chatgpt-plus-1m
                  name: ChatGPT Plus — 1 个月
                  category: ai
                  type: account
                  price: 1990
                  currency: RUB
                  in_stock: true
                  fields:
                  - name: email
                    label: 账号邮箱
                    type: email
                    required: true
                - id: claude-pro-1m
                  name: Claude Pro — 1 个月
                  category: ai
                  type: account
                  price: 2190
                  currency: RUB
                  in_stock: true
                  fields:
                  - name: email
                    label: 账号邮箱
                    type: email
                    required: true
                - id: midjourney-basic-1m
                  name: Midjourney Basic — 1 个月
                  category: ai
                  type: code
                  price: 1090
                  currency: RUB
                  in_stock: false
                  fields: []
                page: 1
                limit: 20
                total: 3
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/products/{id}:
    get:
      tags:
      - 目录
      operationId: getProduct
      summary: 按 ID 获取商品
      description: 返回商品详情，包括价格、库存以及下单时需要提交的字段列表。
      parameters:
      - name: id
        in: path
        required: true
        description: 商品 ID。
        schema:
          type: string
        example: steam-topup
      responses:
        '200':
          description: 商品。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'
              examples:
                topup:
                  summary: Steam 充值
                  value:
                    id: steam-topup
                    name: Steam 充值
                    category: topup
                    type: topup
                    price: null
                    currency: RUB
                    min_amount: 100
                    max_amount: 50000
                    fee_percent: 6
                    in_stock: true
                    fields:
                    - name: login
                      label: Steam 登录名
                      type: string
                      required: true
                    - name: region
                      label: 账号地区
                      type: string
                      required: false
                      enum:
                      - RU
                      - KZ
                      - CIS
                account:
                  summary: ChatGPT Plus
                  value:
                    id: chatgpt-plus-1m
                    name: ChatGPT Plus — 1 个月
                    category: ai
                    type: account
                    price: 1990
                    currency: RUB
                    in_stock: true
                    fields:
                    - name: email
                      label: 账号邮箱
                      type: email
                      required: true
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/orders:
    post:
      tags:
      - 订单
      operationId: createOrder
      summary: 创建订单
      description: |
        创建订单，从合作伙伴余额扣款并立即发货。
        大多数商品的响应状态为 `delivered`，并包含已填写的 `item`。
        如果发货需要时间，订单将以 `processing` 状态返回——最终状态会通过 Webhook
        或 `GET /v1/orders/{id}` 获取。如果发货失败，资金将退回余额（`refunded`）。
      parameters:
      - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateOrderRequest'
            examples:
              chatgpt:
                summary: ChatGPT Plus 1 个月
                value:
                  product: chatgpt-plus-1m
                  qty: 1
                  fields:
                    email: anna@example.com
                  external_id: shop-10482
              steam:
                summary: Steam 充值 1000 ₽
                value:
                  product: steam-topup
                  amount: 1000
                  fields:
                    login: player_one
                  external_id: shop-10483
      responses:
        '201':
          description: 订单已创建。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
              examples:
                chatgpt:
                  summary: 已发放 ChatGPT Plus 账号
                  value:
                    id: ord_8KQ2
                    status: delivered
                    product: chatgpt-plus-1m
                    qty: 1
                    external_id: shop-10482
                    charged: 1990
                    currency: RUB
                    balance: 86400
                    item:
                      type: account
                      login: anna@example.com
                      password: Wq7!rP2m-x9L
                      expires_at: '2027-01-03T12:00:00Z'
                      instructions: 请使用这些信息登录 chatgpt.com 并修改密码。
                    created_at: '2026-12-03T12:00:00Z'
                    delivered_at: '2026-12-03T12:00:02Z'
                steam:
                  summary: Steam 充值已完成
                  value:
                    id: ord_8KQ3
                    status: delivered
                    product: steam-topup
                    qty: 1
                    amount: 1000
                    external_id: shop-10483
                    charged: 1060
                    currency: RUB
                    balance: 85340
                    item:
                      type: topup
                      login: player_one
                      amount: 1000
                      currency: RUB
                    created_at: '2026-12-03T12:05:00Z'
                    delivered_at: '2026-12-03T12:05:04Z'
        '200':
          description: 使用相同 `Idempotency-Key` 的重复请求——返回之前创建的订单，不会重复扣款。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '402':
          $ref: '#/components/responses/InsufficientBalance'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          $ref: '#/components/responses/IdempotencyConflict'
        '429':
          $ref: '#/components/responses/RateLimited'
    get:
      tags:
      - 订单
      operationId: listOrders
      summary: 订单列表
      description: 按从新到旧的顺序返回合作伙伴的订单。采用游标分页——将响应中的 `next_cursor` 传入 `cursor` 参数。
      parameters:
      - name: status
        in: query
        description: 按状态筛选。
        schema:
          $ref: '#/components/schemas/OrderStatus'
      - $ref: '#/components/parameters/Cursor'
      - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: 订单分页。
          content:
            application/json:
              schema:
                type: object
                required:
                - data
                - has_more
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Order'
                  has_more:
                    type: boolean
                  next_cursor:
                    type:
                    - string
                    - 'null'
              example:
                data:
                - id: ord_8KQ3
                  status: delivered
                  product: steam-topup
                  qty: 1
                  amount: 1000
                  external_id: shop-10483
                  charged: 1060
                  currency: RUB
                  balance: 85340
                  item:
                    type: topup
                    login: player_one
                    amount: 1000
                    currency: RUB
                  created_at: '2026-12-03T12:05:00Z'
                  delivered_at: '2026-12-03T12:05:04Z'
                - id: ord_8KQ2
                  status: delivered
                  product: chatgpt-plus-1m
                  qty: 1
                  external_id: shop-10482
                  charged: 1990
                  currency: RUB
                  balance: 86400
                  item:
                    type: account
                    login: anna@example.com
                    password: Wq7!rP2m-x9L
                    expires_at: '2027-01-03T12:00:00Z'
                  created_at: '2026-12-03T12:00:00Z'
                  delivered_at: '2026-12-03T12:00:02Z'
                has_more: true
                next_cursor: ord_8KQ1
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/orders/{id}:
    get:
      tags:
      - 订单
      operationId: getOrder
      summary: 按 ID 获取订单
      description: 返回订单及其当前状态和已发放的商品。
      parameters:
      - name: id
        in: path
        required: true
        description: 订单 ID。
        schema:
          type: string
        example: ord_8KQ2
      responses:
        '200':
          description: 订单。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
              examples:
                code:
                  summary: 已发放兑换码
                  value:
                    id: ord_8KR7
                    status: delivered
                    product: spotify-premium-3m
                    qty: 1
                    external_id: shop-10501
                    charged: 1290
                    currency: RUB
                    balance: 84050
                    item:
                      type: code
                      code: SPOT-7QH4-K9XM-2LPA
                      expires_at: '2027-12-31T23:59:59Z'
                      instructions: 请在 spotify.com/redeem 兑换此码。
                    created_at: '2026-12-03T13:10:00Z'
                    delivered_at: '2026-12-03T13:10:01Z'
                processing:
                  summary: 订单处理中
                  value:
                    id: ord_8KR8
                    status: processing
                    product: steam-topup
                    qty: 1
                    amount: 500
                    external_id: shop-10502
                    charged: 530
                    currency: RUB
                    balance: 83520
                    item: null
                    created_at: '2026-12-03T13:12:00Z'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/balance:
    get:
      tags:
      - 余额
      operationId: getBalance
      summary: 当前余额
      description: 返回合作伙伴的可用余额以及为处理中订单冻结的金额。
      responses:
        '200':
          description: 余额。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Balance'
              example:
                balance: 85340
                currency: RUB
                held: 530
                updated_at: '2026-12-03T12:05:04Z'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/balance/transactions:
    get:
      tags:
      - 余额
      operationId: listBalanceTransactions
      summary: 交易记录
      description: 充值、订单扣款和退款，按从新到旧排序。采用游标分页。
      parameters:
      - name: type
        in: query
        description: 按交易类型筛选。
        schema:
          $ref: '#/components/schemas/TransactionType'
      - $ref: '#/components/parameters/Cursor'
      - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: 交易分页。
          content:
            application/json:
              schema:
                type: object
                required:
                - data
                - has_more
                properties:
                  data:
                    type: array
                    items:
                      $ref: '#/components/schemas/Transaction'
                  has_more:
                    type: boolean
                  next_cursor:
                    type:
                    - string
                    - 'null'
              example:
                data:
                - id: txn_5Hd2
                  type: charge
                  amount: -1060
                  currency: RUB
                  balance_after: 85340
                  order_id: ord_8KQ3
                  description: Steam 充值 — 1000 ₽
                  created_at: '2026-12-03T12:05:00Z'
                - id: txn_5Hd1
                  type: charge
                  amount: -1990
                  currency: RUB
                  balance_after: 86400
                  order_id: ord_8KQ2
                  description: ChatGPT Plus — 1 个月
                  created_at: '2026-12-03T12:00:00Z'
                - id: txn_5Hc9
                  type: topup
                  amount: 50000
                  currency: RUB
                  balance_after: 88390
                  order_id: null
                  description: '余额充值，账单 #2471'
                  created_at: '2026-12-02T09:30:00Z'
                has_more: false
                next_cursor: null
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/RateLimited'
webhooks:
  order.delivered:
    post:
      tags:
      - Webhook
      operationId: webhookOrderDelivered
      summary: 订单已发货
      description: 商品已成功发放。即时发货的订单和处理中的订单都会收到此事件。
      parameters:
      - $ref: '#/components/parameters/WebhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrderEvent'
            example:
              event: order.delivered
              created_at: '2026-12-03T12:05:04Z'
              data:
                id: ord_8KQ3
                status: delivered
                product: steam-topup
                qty: 1
                amount: 1000
                external_id: shop-10483
                charged: 1060
                currency: RUB
                balance: 85340
                item:
                  type: topup
                  login: player_one
                  amount: 1000
                  currency: RUB
                created_at: '2026-12-03T12:05:00Z'
                delivered_at: '2026-12-03T12:05:04Z'
      responses:
        '200':
          description: 事件已接收。任何 `2xx` 响应都会停止重试。
  order.processing:
    post:
      tags:
      - Webhook
      operationId: webhookOrderProcessing
      summary: 订单处理中
      description: 发货需要一些时间。金额已冻结，请等待 `order.delivered` 或 `order.failed`。
      parameters:
      - $ref: '#/components/parameters/WebhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrderEvent'
            example:
              event: order.processing
              created_at: '2026-12-03T13:12:00Z'
              data:
                id: ord_8KR8
                status: processing
                product: steam-topup
                qty: 1
                amount: 500
                external_id: shop-10502
                charged: 530
                currency: RUB
                balance: 83520
                item: null
                created_at: '2026-12-03T13:12:00Z'
      responses:
        '200':
          description: 事件已接收。
  order.failed:
    post:
      tags:
      - Webhook
      operationId: webhookOrderFailed
      summary: 订单失败
      description: 商品发放失败。已扣金额退回余额，订单状态变为 `refunded`。
      parameters:
      - $ref: '#/components/parameters/WebhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/OrderEvent'
            example:
              event: order.failed
              created_at: '2026-12-03T13:20:00Z'
              data:
                id: ord_8KR8
                status: refunded
                product: steam-topup
                qty: 1
                amount: 500
                external_id: shop-10502
                charged: 0
                currency: RUB
                balance: 84050
                item: null
                failure:
                  code: account_not_found
                  message: 未找到登录名为 player_two 的 Steam 账号。
                created_at: '2026-12-03T13:12:00Z'
      responses:
        '200':
          description: 事件已接收。
  balance.topup:
    post:
      tags:
      - Webhook
      operationId: webhookBalanceTopup
      summary: 余额已充值
      description: 资金已存入合作伙伴余额。
      parameters:
      - $ref: '#/components/parameters/WebhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/BalanceTopupEvent'
            example:
              event: balance.topup
              created_at: '2026-12-02T09:30:00Z'
              data:
                id: txn_5Hc9
                type: topup
                amount: 50000
                currency: RUB
                balance_after: 88390
                order_id: null
                description: '余额充值，账单 #2471'
                created_at: '2026-12-02T09:30:00Z'
      responses:
        '200':
          description: 事件已接收。
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: wsb_live_… / wsb_test_…
      description: 合作伙伴 API 密钥。`wsb_live_…` 为正式环境，`wsb_test_…` 为测试环境。
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: 请求的唯一键。使用相同键重复请求将返回同一订单，不会重复扣款。
      schema:
        type: string
        maxLength: 64
      example: 3f1c9a52-7d4e-4b8a-9c21-6e0f5b7a8d13
    Cursor:
      name: cursor
      in: query
      description: 下一页的游标（上一次响应中的 `next_cursor`）。
      schema:
        type: string
      example: ord_8KQ1
    Limit:
      name: limit
      in: query
      description: 每页记录数。
      schema:
        type: integer
        minimum: 1
        maximum: 100
        default: 20
    WebhookSignature:
      name: X-Wasabi-Signature
      in: header
      required: true
      description: HMAC-SHA256 签名：`t={unix_time},v1={hex(hmac_sha256(secret, t + "." + raw_body))}`。
      schema:
        type: string
      example: t=1764763504,v1=5f8a3c0d2b9e47a1c6f3e8d0b5a7294c1e6f8a3d0b2c9e47a15f8a3c0d2be91b
  schemas:
    Category:
      type: string
      description: |
        商品类别：
        - `ai` — AI 工具
        - `music` — 音乐
        - `games` — 游戏
        - `topup` — 充值
        - `telegram` — Telegram
        - `study` — 学习
      enum:
      - ai
      - music
      - games
      - topup
      - telegram
      - study
    ProductType:
      type: string
      description: |
        发货方式：
        - `account` — 账号信息（登录名和密码）
        - `code` — 激活码
        - `topup` — 充值到指定登录名
      enum:
      - account
      - code
      - topup
    ProductField:
      type: object
      description: 下单时需要在 `fields` 中提交的字段。
      required:
      - name
      - label
      - type
      - required
      properties:
        name:
          type: string
          description: '`fields` 对象中的键。'
          example: login
        label:
          type: string
          description: 向买家显示的字段名称。
          example: Steam 登录名
        type:
          type: string
          enum:
          - string
          - email
          - phone
          example: string
        required:
          type: boolean
          example: true
        enum:
          type: array
          description: 可选值（如列表受限）。
          items:
            type: string
          example:
          - RU
          - KZ
          - CIS
    Product:
      type: object
      required:
      - id
      - name
      - category
      - type
      - price
      - currency
      - in_stock
      - fields
      properties:
        id:
          type: string
          description: 商品 ID。
          example: chatgpt-plus-1m
        name:
          type: string
          example: ChatGPT Plus — 1 个月
        category:
          $ref: '#/components/schemas/Category'
        type:
          $ref: '#/components/schemas/ProductType'
        price:
          type:
          - number
          - 'null'
          description: 合作伙伴价格（卢布）。充值类商品为 `null`——总价根据 `amount` 并计入 `fee_percent` 计算。
          example: 1990
        currency:
          type: string
          const: RUB
          example: RUB
        min_amount:
          type: number
          description: 仅适用于 `topup`——最低充值金额。
          example: 100
        max_amount:
          type: number
          description: 仅适用于 `topup`——最高充值金额。
          example: 50000
        fee_percent:
          type: number
          description: 仅适用于 `topup`——按金额百分比收取的手续费。
          example: 6
        in_stock:
          type: boolean
          description: 商品当前是否有货。
          example: true
        fields:
          type: array
          description: 下单所需的数据（邮箱、登录名、地区……）。
          items:
            $ref: '#/components/schemas/ProductField'
    CreateOrderRequest:
      type: object
      required:
      - product
      properties:
        product:
          type: string
          description: 商品 ID。
          example: steam-topup
        qty:
          type: integer
          minimum: 1
          maximum: 50
          default: 1
          description: 数量。充值类商品始终为 1。
          example: 1
        amount:
          type: number
          description: 充值金额（卢布）。`topup` 类商品必填。
          example: 1000
        fields:
          type: object
          description: 发货所需数据——键来自 `Product.fields`。
          additionalProperties:
            type: string
          example:
            login: player_one
        external_id:
          type: string
          maxLength: 128
          description: 您的订单编号——会在响应和 Webhook 中返回。
          example: shop-10483
    OrderStatus:
      type: string
      description: |
        - `processing` — 金额已冻结，正在发货
        - `delivered` — 已发货
        - `failed` — 发货失败
        - `refunded` — 资金已退回余额
      enum:
      - processing
      - delivered
      - failed
      - refunded
    OrderItem:
      type: object
      description: 已发放的商品。字段取决于 `type`。
      required:
      - type
      properties:
        type:
          $ref: '#/components/schemas/ProductType'
        code:
          type: string
          description: 激活码（`code`）。
          example: SPOT-7QH4-K9XM-2LPA
        login:
          type: string
          description: 账号登录名（`account`）或已充值的账号（`topup`）。
          example: anna@example.com
        password:
          type: string
          description: 账号密码（`account`）。
          example: Wq7!rP2m-x9L
        amount:
          type: number
          description: 充值金额（`topup`）。
          example: 1000
        currency:
          type: string
          example: RUB
        expires_at:
          type:
          - string
          - 'null'
          format: date-time
          description: 订阅或兑换码的到期时间。
          example: '2027-01-03T12:00:00Z'
        instructions:
          type: string
          description: 给买家的使用说明。
          example: 请使用这些信息登录 chatgpt.com 并修改密码。
    Order:
      type: object
      required:
      - id
      - status
      - product
      - qty
      - charged
      - currency
      - balance
      - created_at
      properties:
        id:
          type: string
          example: ord_8KQ2
        status:
          $ref: '#/components/schemas/OrderStatus'
        product:
          type: string
          example: chatgpt-plus-1m
        qty:
          type: integer
          example: 1
        amount:
          type: number
          description: 充值金额（适用于 `topup`）。
          example: 1000
        external_id:
          type:
          - string
          - 'null'
          example: shop-10482
        charged:
          type: number
          description: 从余额扣除的金额，₽。
          example: 1990
        currency:
          type: string
          example: RUB
        balance:
          type: number
          description: 交易后的合作伙伴余额，₽。
          example: 86400
        item:
          description: 已发放的商品。订单发货前为 `null`。
          oneOf:
          - $ref: '#/components/schemas/OrderItem'
          - type: 'null'
        failure:
          type: object
          description: 失败原因（适用于 `failed` 和 `refunded`）。
          properties:
            code:
              type: string
              example: account_not_found
            message:
              type: string
              example: 未找到登录名为 player_two 的 Steam 账号。
        created_at:
          type: string
          format: date-time
          example: '2026-12-03T12:00:00Z'
        delivered_at:
          type: string
          format: date-time
          example: '2026-12-03T12:00:02Z'
    Balance:
      type: object
      required:
      - balance
      - currency
      - held
      properties:
        balance:
          type: number
          description: 可用于下单的金额，₽。
          example: 85340
        currency:
          type: string
          example: RUB
        held:
          type: number
          description: 为处理中订单冻结的金额，₽。
          example: 530
        updated_at:
          type: string
          format: date-time
          example: '2026-12-03T12:05:04Z'
    TransactionType:
      type: string
      description: |
        - `topup` — 余额充值
        - `charge` — 订单扣款
        - `refund` — 失败订单退款
      enum:
      - topup
      - charge
      - refund
    Transaction:
      type: object
      required:
      - id
      - type
      - amount
      - currency
      - balance_after
      - created_at
      properties:
        id:
          type: string
          example: txn_5Hd2
        type:
          $ref: '#/components/schemas/TransactionType'
        amount:
          type: number
          description: 交易金额，₽。扣款为负数。
          example: -1060
        currency:
          type: string
          example: RUB
        balance_after:
          type: number
          example: 85340
        order_id:
          type:
          - string
          - 'null'
          example: ord_8KQ3
        description:
          type: string
          example: Steam 充值 — 1000 ₽
        created_at:
          type: string
          format: date-time
          example: '2026-12-03T12:05:00Z'
    OrderEvent:
      type: object
      required:
      - event
      - created_at
      - data
      properties:
        event:
          type: string
          enum:
          - order.delivered
          - order.processing
          - order.failed
          example: order.delivered
        created_at:
          type: string
          format: date-time
          example: '2026-12-03T12:05:04Z'
        data:
          $ref: '#/components/schemas/Order'
    BalanceTopupEvent:
      type: object
      required:
      - event
      - created_at
      - data
      properties:
        event:
          type: string
          const: balance.topup
        created_at:
          type: string
          format: date-time
          example: '2026-12-02T09:30:00Z'
        data:
          $ref: '#/components/schemas/Transaction'
    Error:
      type: object
      required:
      - error
      properties:
        error:
          type: object
          required:
          - code
          - message
          properties:
            code:
              type: string
              description: 机器可读的错误代码。
              example: insufficient_balance
            message:
              type: string
              description: 面向开发者的错误说明。
              example: 余额不足。
  responses:
    BadRequest:
      description: 请求无效——缺少字段或字段填写错误。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: invalid_fields
              message: steam-topup 商品必须提供 fields.login 字段。
    Unauthorized:
      description: 未提供密钥，或密钥已吊销或无效。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: unauthorized
              message: API 密钥无效或已吊销。
    InsufficientBalance:
      description: 余额不足，无法下单。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: insufficient_balance
              message: 余额不足：需要 1060 ₽，可用 420 ₽。
    NotFound:
      description: 未找到资源。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: not_found
              message: 未找到商品 netflix-premium-12m。
    IdempotencyConflict:
      description: 该幂等键已用于不同的请求体。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: idempotency_conflict
              message: Idempotency-Key 已用于另一个请求。
    RateLimited:
      description: 超出请求频率限制。请在 `Retry-After` 秒后重试。
      headers:
        Retry-After:
          description: 多少秒后可以重试请求。
          schema:
            type: integer
          example: 2
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: rate_limited
              message: 请求过多。限制为每分钟 60 次。
