> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cbpayapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 费用结算与余额不足

> 了解 USD 规范费用、standalone 扣款顺序以及 402 详情

两个环境的 API 完全一致——先在 test 环境构建，再通过切换基础 URL 和密钥上线。 [环境与测试 →](https://docs.cbpayapp.com/zh/environment-testing.md)

本指南说明集成方必须实现的三个合同：

* 百分比和固定费用以 USD 为规范货币；
* 只有 standalone 服务费用可以尝试扣款顺序；
* payout、加密货币提现、转账和银行转账必须从一个余额支付全部金额。

API JSON 始终使用英文。金额是十进制字符串，财务计算不要使用浮点数。

完整的标准错误目录请参阅[错误](https://docs.cbpayapp.com/zh/errors.md)。

```mermaid theme={"theme":{"light":"github-light","dark":"github-dark"}}
sequenceDiagram
  participant App as 集成方
  participant API as CBPay
  participant Ledger as 原子账本
  App->>API: 使用 idempotency_key 请求
  API->>Ledger: 全额扣款
  alt 余额足够
    Ledger-->>API: 扣款成功
    API-->>App: 创建操作
  else 余额不足或竞争失败
    Ledger-->>API: 402 insufficient_funds
    API-->>App: 402 + 可选 details
  end
```

## USD 规范费用

对百分比和固定费用配置 `asset: "USD"`。管理端以 USD 分为单位接收
固定费用，API 返回 USD 十进制金额。对这些服务配置 `USDT` 或 `USDC`
会返回 `400 invalid_asset`。旧数据可在迁移期间继续显示，但有效视图
和新配置均以 USD 为规范。

价差仍保留自己的 `asset` 维度，不会变成费用行。使用以下请求查看
账户的有效费用和结算价格：

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://api.qbank.cl/platform/v1/rates" \
  -H "Authorization: Bearer <token>"
```

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "base": "USD",
  "fees": [
    {
      "service": "payout",
      "country": "CL",
      "asset": "USD",
      "percent": "1.50",
      "fixed_amount": "0.30",
      "status": "active"
    }
  ],
  "settlement": {
    "default_asset": "USD",
    "assets": [
      { "asset": "USD", "available": true, "settlement_rate": "1" },
      { "asset": "USDT", "available": true, "settlement_rate": "1" }
    ]
  }
}
```

USD 100.00 的费用为
`ceil(100.00 × 1.50%) + 0.30 = 1.80 USD`。

## standalone 顺序与 payout

standalone 服务费用按 `default → USD → USDT → USDC` 顺序尝试，并且只
使用一个余额。无效或禁用的 default 会跳过，重复候选会去重，最终扣款
是原子且幂等的。

Payout **不使用**该顺序：必须从选定的 settlement asset 支付 100%，
否则返回 `402 insufficient_funds`。提现、内部转账和 Banking fiat
转账也一样。预检查与原子账本扣款之间发生竞争时仍返回 402；不要创建
第二笔操作。

| 状态/响应 | 操作 |
| - | - |
| `402 insufficient_funds` | 补足余额，并使用相同 `idempotency_key` 重试 |
| `202 pending` 或 `processing` | 读取资源并等待，不要创建替代操作 |
| `completed` 或 `failed` | 对账最终扣款及已记录的退款 |

## fee-aware 402

历史错误外壳保持不变，`details` 是可选的附加字段：

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USDT",
    "need": "103.930000",
    "available": "80.000000",
    "missing": "23.930000",
    "need_amount": "100.000000",
    "need_fee": "3.930000",
    "need_usd": "103.930000",
    "available_usd": "80.000000",
    "missing_usd": "23.930000",
    "need_amount_usd": "100.000000",
    "need_fee_usd": "3.930000"
  }
}
```

基础键是 `asset`、`need`、`available` 和 `missing`。只有能够精确拆分
时才有 `need_amount` 与 `need_fee`。USD 等价字段只适用于 USD、USDT
和 USDC 的 1:1 情况，不适用于 BTC、金属、RWA 或未知资产。若数字不
可靠，响应会省略 `details`。

Payout 示例：

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USD",
    "need": "86.01",
    "available": "80.00",
    "missing": "6.01",
    "need_usd": "86.01",
    "available_usd": "80.00",
    "missing_usd": "6.01"
  }
}
```

Crypto 示例：

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USDT",
    "need": "103.930000",
    "available": "100.000000",
    "missing": "3.930000",
    "need_amount": "100.000000",
    "need_fee": "3.930000",
    "need_usd": "103.930000",
    "available_usd": "100.000000",
    "missing_usd": "3.930000",
    "need_amount_usd": "100.000000",
    "need_fee_usd": "3.930000"
  }
}
```

BTC 转账不会带 USD 等价字段：

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "BTC",
    "need": "0.00150000",
    "available": "0.00100000",
    "missing": "0.00050000"
  }
}
```

## 对账与重试

成功的 standalone 扣款会在 cartola 中显示服务、reference，扣款资产和
执行汇率记录在 movement metadata 中；被跳过的候选只是操作日志，不是
资金流水。Payout 只在选定 settlement asset 中产生一笔扣款。

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://api.qbank.cl/platform/v1/movements?from=2026-11-29&to=2026-11-29&page=1&page_size=50" \
  -H "Authorization: Bearer <token>"
```

收到 402 后补足余额，并使用相同的 `idempotency_key` 重试。遇到 timeout
或 5xx，先读取原操作状态；不要自行重新发送或重新排队。

## FAQ

> [accordion] 可以用 USDT 配置费用吗？
>
> USD 规范服务不可以。请使用 `asset: "USD"`；USDT/USDC 行是旧数据。

> [accordion] USD 不足时 payout 会使用其他余额吗？
>
> 不会。只有 standalone 费用使用 default → USD → USDT → USDC。

> [accordion] 每个 402 都有 details 吗？
>
> 没有。它是可选的，必须始终先处理 `error` 和 `message`。

> [accordion] 在哪里查看有效费用？
>
> 调用 `GET /v1/rates` 查看 `fees`；`asset_prices` 和 `settlement` 分别
> 描述余额价格和价差。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.