openapi: 3.1.0
info:
  title: UsPay Merchant API
  version: 1.0.0
  description: >-
    UsPay is a single-deployment, single-tenant gateway and the merchant
    protocol documented here. This document covers UsPay transaction creation,
    signing, order queries, and notifications. Internal operations use /admin
    and are outside this merchant API. Supported checkout locales: `en-US`,
    `hi-IN`, `ja-JP`, `ko-KR`, `ru-RU`, `zh-TW`, and `zh-CN`.
servers:
  - url: https://pay.example.com
paths:
  /payments/uspay/v1/order/create-transaction:
    post:
      operationId: createUspayTransaction
      summary: Create a UsPay transaction
      description: Accepts JSON or form data. Exclude signature and empty values, sort field names in ASCII order, join key=value pairs with &, and calculate lowercase HMAC-SHA256 using the API Secret as the HMAC key. Omitting both token and network creates a selectable order; the Merchant API token/network pair table documents the built-in values.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/UspayCreateRequest" }
          application/x-www-form-urlencoded:
            schema: { $ref: "#/components/schemas/UspayCreateRequest" }
      responses:
        "200":
          description: Transaction created
          content:
            application/json:
              schema: { $ref: "#/components/schemas/UspayCreateResponse" }
        "400": { $ref: "#/components/responses/GatewayError" }
        "401": { $ref: "#/components/responses/AuthenticationFailed" }
        "413": { $ref: "#/components/responses/PayloadTooLarge" }
        "429": { $ref: "#/components/responses/RateLimited" }
        "500": { $ref: "#/components/responses/GatewayError" }
        "502": { $ref: "#/components/responses/ProviderUnavailable" }
        "503": { $ref: "#/components/responses/ServiceUnavailable" }
      callbacks:
        orderNotification:
          '{$request.body#/notify_url}':
            post:
              summary: UsPay order notification
              description: The signature is lowercase HMAC-SHA256 over the sorted non-empty callback fields using the same API Secret as the HMAC key. Return plain text ok with HTTP 200. This operation is rendered once in the Webhooks section of the reference.
              x-scalar-ignore: true
              requestBody:
                required: true
                content:
                  application/json:
                    schema: { $ref: "#/components/schemas/UspayNotification" }
              responses:
                "200":
                  description: Plain text ok acknowledgement
                  content:
                    text/plain:
                      schema: { type: string, const: ok }
  /payments/uspay/v1/order/query:
    get:
      operationId: queryUspayTransaction
      summary: Query a UsPay transaction
      description: Query by exactly one trade_id or order_id. Sign all non-empty query fields except signature using the same lowercase HMAC-SHA256 contract as order creation.
      parameters:
        - { name: pid, in: query, required: true, schema: { type: string } }
        - { name: trade_id, in: query, required: false, schema: { type: string, maxLength: 128 } }
        - { name: order_id, in: query, required: false, schema: { type: string, maxLength: 128 } }
        - { name: signature, in: query, required: true, schema: { type: string, pattern: "^[0-9a-f]{64}$" } }
      responses:
        "200":
          description: Transaction status and immutable payment snapshot
          content:
            application/json:
              schema: { $ref: "#/components/schemas/UspayCreateResponse" }
        "400": { $ref: "#/components/responses/GatewayError" }
        "401": { $ref: "#/components/responses/AuthenticationFailed" }
        "404": { $ref: "#/components/responses/OrderNotFound" }
        "429": { $ref: "#/components/responses/RateLimited" }
        "500": { $ref: "#/components/responses/GatewayError" }
webhooks:
  orderNotification:
    post:
      operationId: uspayOrderNotification
      summary: UsPay order notification callback
      description: >-
        UsPay POSTs this JSON body to the order's immutable notify_url after a
        committed order event. The receiving URL is the merchant-supplied
        notify_url, not a fixed gateway path. Verify the signature, persist the
        order/event identity, then return HTTP 200 with the plain-text body ok;
        any other status or body is retried with backoff. The
        x-uspay-event-id and x-uspay-delivery-id headers are informational and
        unsigned. The payload has no currency field: amount is in the currency
        used when the order was created, while actual_amount is the payable
        crypto asset amount in token units.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: "#/components/schemas/UspayNotification" }
      responses:
        "200":
          description: Plain text ok acknowledgement
          content:
            text/plain:
              schema: { type: string, const: ok }
components:
  responses:
    GatewayError:
      description: Gateway error
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    AuthenticationFailed:
      description: "status_code 401: PID, scope, or signature verification failed. An unknown PID and a bad signature are indistinguishable."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    RateLimited:
      description: "status_code 429: the credential exceeded its per-minute request window, or the submitted PID accumulated more than 20 failed authentications within one minute. Retry after the window passes."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    PayloadTooLarge:
      description: "status_code 10009: the request body exceeds 64 KiB."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    OrderNotFound:
      description: "status_code 10001: no order matches trade_id or order_id under the authenticated credential."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    ProviderUnavailable:
      description: "status_code 10003 (provider_unavailable): the hosted payment provider did not return a payment. The order and its external order ID were rolled back, so the same request may be retried."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
    ServiceUnavailable:
      description: "status_code 10003 when the receiving target or provider configuration is unavailable, or 10016 when no usable exchange rate exists for the order currency."
      content:
        application/json:
          schema: { $ref: "#/components/schemas/GatewayEnvelope" }
  schemas:
    OrderStatus:
      type: string
      enum: [pending, confirming, partially_paid, paid, overpaid, expired, cancelled, failed, refunded]
    UspayStatus:
      type: integer
      enum: [1, 2, 3, 4]
      description: 1 waiting for payment, 2 paid, 3 closed, 4 waiting for payment-method selection.
    UspayCreateRequest:
      type: object
      required: [pid, order_id, currency, amount, notify_url, signature]
      properties:
        pid: { type: string, description: API credential PID }
        order_id: { type: string, minLength: 1, maxLength: 128 }
        currency: { type: string, pattern: "^[A-Za-z]{3}$", description: "Active ISO 4217 fiat currency code such as USD; an unsupported code answers status_code 10009." }
        locale:
          type: string
          maxLength: 16
          enum: [en-US, hi-IN, ja-JP, ko-KR, ru-RU, zh-TW, zh-CN]
          description: "Optional BCP 47 checkout locale. Omit to use the payer's saved locale or the site default; unsupported values answer status_code 10009."
        token: { type: string, pattern: "^[A-Za-z0-9_-]{2,20}$", description: "Payment asset symbol such as USDT from the built-in pair table in the Merchant API documentation. Omit together with network to let checkout select a payment method. A listed pair is a capability, not a guarantee of availability." }
        network: { type: string, pattern: "^[A-Za-z0-9-]{2,32}$", description: "Payment network code such as tron from the built-in pair table in the Merchant API documentation. Omit together with token to let checkout select a payment method. An order only succeeds when an enabled, ready receiving method is bound and its connection is healthy." }
        amount:
          description: "Positive decimal with at most 18 integer and 8 fraction digits; anything else answers status_code 10004."
          oneOf:
            - { type: string, pattern: "^(?!0(?:\\.0+)?$)(?:0|[1-9]\\d{0,17})(?:\\.\\d{1,8})?$" }
            - { type: number, exclusiveMinimum: 0 }
        notify_url: { type: string, format: uri, pattern: "^https://" }
        redirect_url: { type: string, format: uri, pattern: "^https://" }
        name: { type: string, maxLength: 500 }
        signature: { type: string, pattern: "^[0-9a-f]{64}$" }
      example:
        pid: "100000000001"
        order_id: invoice-1001
        currency: USD
        amount: "12.50"
        notify_url: https://merchant.example/notify
        token: USDT
        network: tron
        signature: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
    UspayCreateData:
      type: object
      required: [trade_id, order_id, amount, currency, actual_amount, receive_address, token, network, status, status_detail, expiration_time, payment_url]
      properties:
        trade_id: { type: string, pattern: "^[0-9]{20}$", description: "UsPay trade ID." }
        order_id: { type: string, description: "Merchant external order ID supplied at creation." }
        amount: { type: string, description: "Order amount in currency, as a decimal fiat amount." }
        currency: { type: string, description: "ISO 4217 fiat currency of amount." }
        actual_amount: { type: string, description: "Payable amount in token units; 0 until a payment method is selected. This is a crypto asset amount, not fiat." }
        receive_address: { type: string, description: "Receiving address or account target; empty until a payment method is selected." }
        token: { type: string, description: "Payment asset symbol such as USDT; empty until a payment method is selected." }
        network: { type: string, description: "Payment network code such as tron; empty until a payment method is selected." }
        status: { $ref: "#/components/schemas/UspayStatus" }
        status_detail: { $ref: "#/components/schemas/OrderStatus" }
        expiration_time: { type: integer, description: "Unix timestamp in seconds." }
        payment_url: { type: string, format: uri, description: "Public checkout URL for this order. When the order has a checkout locale the URL may carry ?lang=<locale>; merchants must treat the returned URL as opaque and must not construct it manually." }
    UspayCreateResponse:
      allOf:
        - { $ref: "#/components/schemas/GatewayEnvelope" }
        - type: object
          properties:
            data: { $ref: "#/components/schemas/UspayCreateData" }
    UspayNotification:
      type: object
      required: [pid, trade_id, order_id, amount, actual_amount, receive_address, token, block_transaction_id, status, signature]
      properties:
        pid: { type: string, description: "API credential PID." }
        trade_id: { type: string, pattern: "^[0-9]{20}$", description: "UsPay trade ID." }
        order_id: { type: string, description: "Merchant external order ID supplied at creation." }
        amount: { type: string, description: "Order amount in the order's fiat currency, as a decimal string. The payload does not repeat currency; correlate it with order_id or trade_id." }
        actual_amount: { type: string, description: "Payable amount in token units, as a decimal string. This is the expected crypto asset amount, not fiat and not cumulative received." }
        receive_address: { type: string, description: "Receiving address or account target." }
        token: { type: string, description: "Payment asset symbol such as USDT." }
        block_transaction_id: { type: string, description: "On-chain transaction hash; empty when the event has no transaction." }
        status:
          type: integer
          enum: [1, 2, 3]
          description: "1 waiting for payment, 2 paid, 3 closed."
        signature: { type: string, pattern: "^[0-9a-f]{64}$", description: "Lowercase HMAC-SHA256 over the sorted non-empty callback fields using the same API Secret as the HMAC key." }
      example:
        pid: "100000000001"
        trade_id: "26071406211234567890"
        order_id: invoice-1001
        amount: "12.50"
        actual_amount: "12.512345"
        receive_address: TExampleAddress
        token: USDT
        block_transaction_id: transaction-hash
        status: 2
        signature: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
    GatewayEnvelope:
      type: object
      required: [status_code, message, data, request_id]
      properties:
        status_code:
          type: integer
          description: "200 success; 10001 order not found (query, HTTP 404); 10002 external order ID already exists; 10003 receiving method or provider unavailable, including a hosted-provider failure (HTTP 400, 502, or 503); 10004 amount invalid (non-positive, or more than 18 integer / 8 fraction digits); 10009 invalid parameters (unsupported currency, unsupported checkout locale, notify_url not public HTTPS, redirect_url not HTTPS, token without network, or a body above 64 KiB with HTTP 413); 10016 asset, network, or exchange rate unavailable (HTTP 400 or 503); 401 authentication failed; 429 rate limit or authentication-failure limit exceeded; 500 system error."
        message: { type: string }
        data: {}
        request_id: { type: string }
      example:
        status_code: 10009
        message: invalid parameters
        data: null
        request_id: 73e3648b-b257-47e6-a510-12772cac0448
