openapi: 3.1.0
info:
  title: Wasabi API
  version: '1.0'
  description: |
    API đối tác của cửa hàng số **Wasabi**: danh mục gói đăng ký, game và mã nạp tiền.
    Đặt hàng, trừ tiền từ số dư đối tác và giao hàng diễn ra trong **một yêu cầu duy nhất** —
    phản hồi của `POST /v1/orders` trả về ngay tài khoản, mã hoặc xác nhận nạp tiền.

    ## Xác thực

    Mọi yêu cầu đều cần khóa API trong header `Authorization`:

    ```http
    Authorization: Bearer wsb_live_9f2c4a7e1b8d3f6a
    ```

    Có hai loại khóa: `wsb_live_…` dùng cho môi trường thật, `wsb_test_…` dùng để thử nghiệm.
    Khóa được tạo và thu hồi trong trang quản lý đối tác. Không để lộ khóa
    trong mã frontend và không lưu khóa trong kho mã nguồn.

    ## Tính lũy đẳng

    Khi tạo đơn hàng, hãy gửi header `Idempotency-Key` (chuỗi duy nhất bất kỳ, tối đa 64 ký tự, ví dụ UUID).
    Yêu cầu lặp lại với cùng khóa sẽ trả về đơn hàng đã tạo
    và **không trừ tiền lần nữa** — nhờ vậy có thể gửi lại yêu cầu an toàn khi mất kết nối mạng.
    Khóa được lưu trong 24 giờ. Nếu gửi nội dung yêu cầu khác với cùng khóa, API trả về `409 idempotency_conflict`.

    ## Webhook

    Nhập URL trong trang quản lý đối tác — Wasabi sẽ gửi các sự kiện bằng `POST`
    (`order.delivered`, `order.processing`, `order.failed`, `balance.topup`).
    Mỗi yêu cầu đều được ký: header `X-Wasabi-Signature` chứa HMAC-SHA256
    của nội dung yêu cầu, tính bằng khóa bí mật webhook (`whsec_…`), dạng hex.

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

    Chữ ký được tính trên chuỗi `{t}.{raw_body}`. Hãy so sánh chữ ký trong thời gian không đổi
    và từ chối các sự kiện cũ hơn 5 phút. Nếu máy chủ của bạn không trả về `2xx`, việc gửi sẽ được thử lại
    với độ trễ tăng dần (1 phút, 5 phút, 30 phút, 2 giờ, 6 giờ…) trong tối đa 24 giờ cho đến khi nhận được `2xx`.
    Sự kiện có thể đến nhiều lần — hãy xử lý lũy đẳng theo `data.id`.

    ## Chế độ thử nghiệm

    Với khóa `wsb_test_…`, API hoạt động giống hệt môi trường thật nhưng không trừ tiền và không mua hàng thật:
    đơn hàng trả về dữ liệu thử nghiệm, số dư là ảo, webhook được gửi tới URL thử nghiệm.
    Dữ liệu thử nghiệm và dữ liệu thật hoàn toàn tách biệt.

    ## MCP

    Máy chủ MCP dành cho tác tử AI (Claude, ChatGPT, Cursor và các công cụ khác):

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

    Xác thực bằng cùng khóa Bearer. Các công cụ:

    | Công cụ | Chức năng |
    |---|---|
    | `search_products` | Tìm kiếm danh mục, lọc theo loại |
    | `create_order` | Tạo đơn hàng và giao hàng |
    | `get_order` | Trạng thái và nội dung đơn hàng |
    | `get_balance` | Số dư hiện tại của đối tác |

    Nên cấp cho tác tử khóa thử nghiệm cho đến khi quy trình chạy ổn định.
  contact:
    name: Hỗ trợ Wasabi
    url: https://wasabi.guru
servers:
- url: https://api.wasabi.guru
  description: Chế độ thật và thử nghiệm (xác định theo khóa)
security:
- bearerAuth: []
tags:
- name: Danh mục
  description: Sản phẩm, giá, tình trạng còn hàng và dữ liệu cần để đặt hàng.
- name: Đơn hàng
  description: Tạo đơn hàng với giao hàng tức thì và xem lịch sử.
- name: Số dư
  description: Số dư của đối tác và lịch sử giao dịch.
- name: Webhook
  description: Các sự kiện Wasabi gửi tới máy chủ của bạn.
paths:
  /v1/products:
    get:
      tags:
      - Danh mục
      operationId: listProducts
      summary: Danh sách sản phẩm
      description: Trả về sản phẩm trong danh mục với giá dành cho đối tác. Hỗ trợ lọc theo loại, tìm kiếm và phân trang.
      parameters:
      - name: category
        in: query
        description: Loại sản phẩm.
        schema:
          $ref: '#/components/schemas/Category'
        example: ai
      - name: q
        in: query
        description: Tìm theo tên.
        schema:
          type: string
        example: chatgpt
      - name: page
        in: query
        description: Số trang, bắt đầu từ 1.
        schema:
          type: integer
          minimum: 1
          default: 1
      - name: limit
        in: query
        description: Số sản phẩm mỗi trang.
        schema:
          type: integer
          minimum: 1
          maximum: 100
          default: 20
      responses:
        '200':
          description: Danh sách sản phẩm.
          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: Tổng số sản phẩm theo bộ lọc.
                    example: 3
              example:
                data:
                - id: chatgpt-plus-1m
                  name: ChatGPT Plus — 1 tháng
                  category: ai
                  type: account
                  price: 1990
                  currency: RUB
                  in_stock: true
                  fields:
                  - name: email
                    label: Email tài khoản
                    type: email
                    required: true
                - id: claude-pro-1m
                  name: Claude Pro — 1 tháng
                  category: ai
                  type: account
                  price: 2190
                  currency: RUB
                  in_stock: true
                  fields:
                  - name: email
                    label: Email tài khoản
                    type: email
                    required: true
                - id: midjourney-basic-1m
                  name: Midjourney Basic — 1 tháng
                  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:
      - Danh mục
      operationId: getProduct
      summary: Sản phẩm theo ID
      description: Trả về thông tin sản phẩm với giá, tình trạng còn hàng và danh sách trường cần gửi khi đặt hàng.
      parameters:
      - name: id
        in: path
        required: true
        description: ID sản phẩm.
        schema:
          type: string
        example: steam-topup
      responses:
        '200':
          description: Sản phẩm.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Product'
              examples:
                topup:
                  summary: Nạp Steam
                  value:
                    id: steam-topup
                    name: Nạp 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: Tên đăng nhập Steam
                      type: string
                      required: true
                    - name: region
                      label: Khu vực tài khoản
                      type: string
                      required: false
                      enum:
                      - RU
                      - KZ
                      - CIS
                account:
                  summary: ChatGPT Plus
                  value:
                    id: chatgpt-plus-1m
                    name: ChatGPT Plus — 1 tháng
                    category: ai
                    type: account
                    price: 1990
                    currency: RUB
                    in_stock: true
                    fields:
                    - name: email
                      label: Email tài khoản
                      type: email
                      required: true
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '429':
          $ref: '#/components/responses/RateLimited'
  /v1/orders:
    post:
      tags:
      - Đơn hàng
      operationId: createOrder
      summary: Tạo đơn hàng
      description: |
        Tạo đơn hàng, trừ tiền từ số dư đối tác và giao sản phẩm ngay.
        Với hầu hết sản phẩm, phản hồi có trạng thái `delivered` và trường `item` đã được điền.
        Nếu việc giao hàng cần thời gian, đơn hàng sẽ trả về trạng thái `processing` — trạng thái cuối cùng được gửi qua webhook
        hoặc qua `GET /v1/orders/{id}`. Nếu giao hàng thất bại, tiền được hoàn vào số dư (`refunded`).
      parameters:
      - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateOrderRequest'
            examples:
              chatgpt:
                summary: ChatGPT Plus 1 tháng
                value:
                  product: chatgpt-plus-1m
                  qty: 1
                  fields:
                    email: anna@example.com
                  external_id: shop-10482
              steam:
                summary: Nạp Steam 1000 ₽
                value:
                  product: steam-topup
                  amount: 1000
                  fields:
                    login: player_one
                  external_id: shop-10483
      responses:
        '201':
          description: Đã tạo đơn hàng.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
              examples:
                chatgpt:
                  summary: Đã giao tài khoản 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: Đăng nhập chatgpt.com bằng thông tin này và đổi mật khẩu.
                    created_at: '2026-12-03T12:00:00Z'
                    delivered_at: '2026-12-03T12:00:02Z'
                steam:
                  summary: Đã nạp Steam thành công
                  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: Yêu cầu lặp lại với cùng `Idempotency-Key` — trả về đơn hàng đã tạo trước đó, không trừ tiền lần nữa.
          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:
      - Đơn hàng
      operationId: listOrders
      summary: Danh sách đơn hàng
      description: Trả về các đơn hàng của đối tác từ mới đến cũ. Phân trang theo con trỏ — truyền `next_cursor` từ phản hồi vào tham số `cursor`.
      parameters:
      - name: status
        in: query
        description: Lọc theo trạng thái.
        schema:
          $ref: '#/components/schemas/OrderStatus'
      - $ref: '#/components/parameters/Cursor'
      - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: Trang đơn hàng.
          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:
      - Đơn hàng
      operationId: getOrder
      summary: Đơn hàng theo ID
      description: Trả về đơn hàng cùng trạng thái hiện tại và sản phẩm đã giao.
      parameters:
      - name: id
        in: path
        required: true
        description: ID đơn hàng.
        schema:
          type: string
        example: ord_8KQ2
      responses:
        '200':
          description: Đơn hàng.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
              examples:
                code:
                  summary: Đã giao mã
                  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: Kích hoạt mã tại spotify.com/redeem.
                    created_at: '2026-12-03T13:10:00Z'
                    delivered_at: '2026-12-03T13:10:01Z'
                processing:
                  summary: Đơn hàng đang xử lý
                  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:
      - Số dư
      operationId: getBalance
      summary: Số dư hiện tại
      description: Trả về số dư khả dụng của đối tác và số tiền đang tạm giữ cho các đơn hàng đang xử lý.
      responses:
        '200':
          description: Số dư.
          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:
      - Số dư
      operationId: listBalanceTransactions
      summary: Lịch sử giao dịch
      description: Nạp tiền, trừ tiền cho đơn hàng và hoàn tiền, từ mới đến cũ. Phân trang theo con trỏ.
      parameters:
      - name: type
        in: query
        description: Lọc theo loại giao dịch.
        schema:
          $ref: '#/components/schemas/TransactionType'
      - $ref: '#/components/parameters/Cursor'
      - $ref: '#/components/parameters/Limit'
      responses:
        '200':
          description: Trang giao dịch.
          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: Nạp 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 tháng
                  created_at: '2026-12-03T12:00:00Z'
                - id: txn_5Hc9
                  type: topup
                  amount: 50000
                  currency: RUB
                  balance_after: 88390
                  order_id: null
                  description: 'Nạp số dư theo hóa đơn #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: Đơn hàng đã giao
      description: Sản phẩm đã được giao thành công. Gửi cho cả đơn hàng giao ngay và đơn hàng đang xử lý.
      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: Đã nhận sự kiện. Bất kỳ phản hồi `2xx` nào cũng dừng việc gửi lại.
  order.processing:
    post:
      tags:
      - Webhook
      operationId: webhookOrderProcessing
      summary: Đơn hàng đang xử lý
      description: Việc giao hàng cần thêm thời gian. Số tiền đã được tạm giữ; hãy chờ `order.delivered` hoặc `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: Đã nhận sự kiện.
  order.failed:
    post:
      tags:
      - Webhook
      operationId: webhookOrderFailed
      summary: Đơn hàng thất bại
      description: Không thể giao sản phẩm. Số tiền đã trừ được hoàn vào số dư, đơn hàng chuyển sang trạng thái `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: Không tìm thấy tài khoản Steam có tên đăng nhập player_two.
                created_at: '2026-12-03T13:12:00Z'
      responses:
        '200':
          description: Đã nhận sự kiện.
  balance.topup:
    post:
      tags:
      - Webhook
      operationId: webhookBalanceTopup
      summary: Đã nạp tiền vào số dư
      description: Tiền đã được cộng vào số dư của đối tác.
      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: 'Nạp số dư theo hóa đơn #2471'
                created_at: '2026-12-02T09:30:00Z'
      responses:
        '200':
          description: Đã nhận sự kiện.
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: wsb_live_… / wsb_test_…
      description: Khóa API đối tác. `wsb_live_…` — môi trường thật, `wsb_test_…` — thử nghiệm.
  parameters:
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: Khóa yêu cầu duy nhất. Lặp lại với cùng khóa sẽ trả về cùng đơn hàng mà không trừ tiền lần nữa.
      schema:
        type: string
        maxLength: 64
      example: 3f1c9a52-7d4e-4b8a-9c21-6e0f5b7a8d13
    Cursor:
      name: cursor
      in: query
      description: Con trỏ của trang tiếp theo (`next_cursor` từ phản hồi trước).
      schema:
        type: string
      example: ord_8KQ1
    Limit:
      name: limit
      in: query
      description: Số bản ghi mỗi trang.
      schema:
        type: integer
        minimum: 1
        maximum: 100
        default: 20
    WebhookSignature:
      name: X-Wasabi-Signature
      in: header
      required: true
      description: 'Chữ ký 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: |
        Loại sản phẩm:
        - `ai` — công cụ AI
        - `music` — âm nhạc
        - `games` — game
        - `topup` — nạp tiền
        - `telegram` — Telegram
        - `study` — học tập
      enum:
      - ai
      - music
      - games
      - topup
      - telegram
      - study
    ProductType:
      type: string
      description: |
        Cách giao sản phẩm:
        - `account` — thông tin tài khoản (tên đăng nhập và mật khẩu)
        - `code` — mã kích hoạt
        - `topup` — nạp tiền vào tên đăng nhập đã chỉ định
      enum:
      - account
      - code
      - topup
    ProductField:
      type: object
      description: Trường cần gửi trong `fields` khi đặt hàng.
      required:
      - name
      - label
      - type
      - required
      properties:
        name:
          type: string
          description: Khóa trong đối tượng `fields`.
          example: login
        label:
          type: string
          description: Tên trường hiển thị cho người mua.
          example: Tên đăng nhập Steam
        type:
          type: string
          enum:
          - string
          - email
          - phone
          example: string
        required:
          type: boolean
          example: true
        enum:
          type: array
          description: Giá trị hợp lệ, nếu danh sách bị giới hạn.
          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 sản phẩm.
          example: chatgpt-plus-1m
        name:
          type: string
          example: ChatGPT Plus — 1 tháng
        category:
          $ref: '#/components/schemas/Category'
        type:
          $ref: '#/components/schemas/ProductType'
        price:
          type:
          - number
          - 'null'
          description: Giá dành cho đối tác tính bằng rúp. `null` với nạp tiền — tổng tiền được tính từ `amount` cộng `fee_percent`.
          example: 1990
        currency:
          type: string
          const: RUB
          example: RUB
        min_amount:
          type: number
          description: Chỉ với `topup` — số tiền nạp tối thiểu.
          example: 100
        max_amount:
          type: number
          description: Chỉ với `topup` — số tiền nạp tối đa.
          example: 50000
        fee_percent:
          type: number
          description: Chỉ với `topup` — phí tính theo phần trăm số tiền.
          example: 6
        in_stock:
          type: boolean
          description: Sản phẩm hiện có sẵn hay không.
          example: true
        fields:
          type: array
          description: Dữ liệu cần thiết để đặt hàng (email, tên đăng nhập, khu vực…).
          items:
            $ref: '#/components/schemas/ProductField'
    CreateOrderRequest:
      type: object
      required:
      - product
      properties:
        product:
          type: string
          description: ID sản phẩm.
          example: steam-topup
        qty:
          type: integer
          minimum: 1
          maximum: 50
          default: 1
          description: Số lượng. Với nạp tiền luôn là 1.
          example: 1
        amount:
          type: number
          description: Số tiền nạp tính bằng rúp. Bắt buộc với sản phẩm loại `topup`.
          example: 1000
        fields:
          type: object
          description: Dữ liệu giao hàng — các khóa từ `Product.fields`.
          additionalProperties:
            type: string
          example:
            login: player_one
        external_id:
          type: string
          maxLength: 128
          description: Mã đơn hàng của bạn — được trả về trong phản hồi và webhook.
          example: shop-10483
    OrderStatus:
      type: string
      description: |
        - `processing` — đã tạm giữ tiền, đang giao hàng
        - `delivered` — đã giao hàng
        - `failed` — giao hàng thất bại
        - `refunded` — đã hoàn tiền vào số dư
      enum:
      - processing
      - delivered
      - failed
      - refunded
    OrderItem:
      type: object
      description: Sản phẩm đã giao. Các trường phụ thuộc vào `type`.
      required:
      - type
      properties:
        type:
          $ref: '#/components/schemas/ProductType'
        code:
          type: string
          description: Mã kích hoạt (`code`).
          example: SPOT-7QH4-K9XM-2LPA
        login:
          type: string
          description: Tên đăng nhập tài khoản (`account`) hoặc tài khoản đã được nạp tiền (`topup`).
          example: anna@example.com
        password:
          type: string
          description: Mật khẩu tài khoản (`account`).
          example: Wq7!rP2m-x9L
        amount:
          type: number
          description: Số tiền được cộng (`topup`).
          example: 1000
        currency:
          type: string
          example: RUB
        expires_at:
          type:
          - string
          - 'null'
          format: date-time
          description: Thời điểm gói đăng ký hoặc mã hết hạn.
          example: '2027-01-03T12:00:00Z'
        instructions:
          type: string
          description: Hướng dẫn cho người mua.
          example: Đăng nhập chatgpt.com bằng thông tin này và đổi mật khẩu.
    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: Số tiền nạp (với `topup`).
          example: 1000
        external_id:
          type:
          - string
          - 'null'
          example: shop-10482
        charged:
          type: number
          description: Đã trừ từ số dư, ₽.
          example: 1990
        currency:
          type: string
          example: RUB
        balance:
          type: number
          description: Số dư đối tác sau giao dịch, ₽.
          example: 86400
        item:
          description: Sản phẩm đã giao. `null` cho đến khi đơn hàng được giao.
          oneOf:
          - $ref: '#/components/schemas/OrderItem'
          - type: 'null'
        failure:
          type: object
          description: Lý do thất bại (với `failed` và `refunded`).
          properties:
            code:
              type: string
              example: account_not_found
            message:
              type: string
              example: Không tìm thấy tài khoản Steam có tên đăng nhập player_two.
        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: Khả dụng để đặt hàng, ₽.
          example: 85340
        currency:
          type: string
          example: RUB
        held:
          type: number
          description: Tạm giữ cho các đơn hàng đang xử lý, ₽.
          example: 530
        updated_at:
          type: string
          format: date-time
          example: '2026-12-03T12:05:04Z'
    TransactionType:
      type: string
      description: |
        - `topup` — nạp tiền vào số dư
        - `charge` — trừ tiền cho đơn hàng
        - `refund` — hoàn tiền cho đơn hàng không thành công
      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: Số tiền giao dịch, ₽. Khoản trừ có giá trị âm.
          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: Nạp 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: Mã lỗi dạng máy đọc được.
              example: insufficient_balance
            message:
              type: string
              description: Mô tả lỗi dành cho nhà phát triển.
              example: Số dư không đủ.
  responses:
    BadRequest:
      description: Yêu cầu không hợp lệ — thiếu trường hoặc điền sai.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: invalid_fields
              message: Trường fields.login là bắt buộc với sản phẩm steam-topup.
    Unauthorized:
      description: Thiếu khóa, khóa đã bị thu hồi hoặc không hợp lệ.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: unauthorized
              message: Khóa API không hợp lệ hoặc đã bị thu hồi.
    InsufficientBalance:
      description: Số dư không đủ để đặt hàng.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: insufficient_balance
              message: 'Không đủ tiền: cần 1060 ₽, khả dụng 420 ₽.'
    NotFound:
      description: Không tìm thấy tài nguyên.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: not_found
              message: Không tìm thấy sản phẩm netflix-premium-12m.
    IdempotencyConflict:
      description: Khóa lũy đẳng đã được dùng với nội dung yêu cầu khác.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: idempotency_conflict
              message: Idempotency-Key đã được dùng cho một yêu cầu khác.
    RateLimited:
      description: Vượt quá giới hạn yêu cầu. Thử lại sau `Retry-After` giây.
      headers:
        Retry-After:
          description: Số giây cần chờ trước khi thử lại.
          schema:
            type: integer
          example: 2
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            error:
              code: rate_limited
              message: Quá nhiều yêu cầu. Giới hạn là 60 mỗi phút.
