余额、预算与订单对账
本页内容
查询自己账号的可用余额与两层预算,核对本次读取时的金额上限,再用原回执查订单状态和资金变动。
选择查询内容
核对一笔 API 付款时,从原报价和付款回执开始。展示账户概况时,再按需要查询余额、预算、订单或资金流水。
| 您想知道什么 | 查询接口 | 所需读取动作 |
|---|---|---|
| 目前还有多少站内余额 | 查询账号可用余额 | billing:ReadBalance |
| 账号和当前 API 应用各有多少预算 | 查询费用预算与剩余额度 | billing:ReadBudget |
| 这笔订单购买了什么、现在是什么状态 | 查询账号订单 | billing:ReadOrders |
| 余额在什么时候增加或减少 | 查询账号资金流水 | billing:ReadLedger |
前提条件
在自己的开发者中心确认账号可进行财务查询。为 API 应用授权上表所需动作,并使用该应用的长期 AccessKey。账号级账单须选择全部自有应用,应用排除与包类型限制留空。
以下示例使用已完成身份查询的 Python SDK 客户端。Python SDK 入门。
查询余额与金额单位
从 data.available 读取当前余额,用 data.currency.unit 显示单位,并记录 observed_at。金额为十进制字符串,需要计算时使用十进制定点类型。核对旧交易的金额单位时,使用当次付款或退款回执中记录的单位。
下面的函数同时读取余额和预算,需要授予两项读取动作。仅显示余额时调用 get_balance()。
def read_balance_and_budget(client):
balance = client.get_balance()["data"]
budget = client.get_payment_budget()["data"]
return balance, budget # 金额保留十进制字符串,并显示返回的金额单位。查询付款预算
需要在付款前向使用者展示余额和可用预算时,先读取账号余额,再读取账号与当前 API 应用两层预算。按当前金额单位比较这些值。
- 读取
data.account和data.application。它们分别对应账号和发起请求的 API 应用。 - 两层都检查
enabled、currency_current和single_limit,再看daily.remaining与monthly.remaining。预算日期使用响应中的timezone,下次重置时间看resets_at。 - 计算本次读取时的单笔金额上限。取可用余额、两层单笔上限、两层今日剩余和两层本月剩余中的最小值。
例如可用余额 100.25 点,其余额度均大于 25.00 点,而当前 API 应用今日只剩 25.00 点,则本次读取可显示单笔上限 25.00 点。先用上面的 read_balance_and_budget() 取得两份响应的 data,再传给下面的函数。
from decimal import Decimal
def single_payment_limit(balance, budget):
if budget["financial_health"]["payment_quarantined"]:
raise ValueError("当前账务需要核对,请先查看原报价或订单")
if any(balance["currency"][key] != budget["currency"][key] for key in ("code", "unit")):
raise ValueError("金额单位不一致,请重新查询余额和预算")
amounts = [Decimal(balance["available"])]
for scope in ("account", "application"):
layer = budget[scope]
if not layer["enabled"] or not layer["currency_current"]:
raise ValueError("请核对账号和 API 应用的预算状态与金额单位")
amounts.extend(Decimal(value) for value in (
layer["single_limit"], layer["daily"]["remaining"], layer["monthly"]["remaining"],
))
return format(max(Decimal("0.00"), min(amounts)), ".2f")展示时标明读取时间和金额单位。准备实际购买时,再向使用者展示原报价及有效期并取得确认。最终付款以该次接口的结果为准。
退款到账后重新读取余额和预算。已用 API 付款预算仍计入原付款所在周期,剩余额度以本次查询为准。
查询订单与资金流水
- 订单列表接收
limit、cursor和status。状态可填paid、pending、closed或refunded。返回项中的unknown用于展示。 - 流水使用
limit和cursor。每个列表有独立的next_cursor,继续时沿用原条件和本列表返回的游标。 - 核对某笔订单的当前状态时,省略
status,按付款回执的order_id匹配每页的items[].id。
find_order(client, order_id) 示例每次最多查 5 页。若未匹配而第二个返回值有游标,可传回 cursor 继续查找。游标到末页仍未找到时,继续核对原报价和回执。historical_complete=false 表示历史覆盖范围可能不完整。
def read_order_page(client, status=None, cursor=None):
return client.list_orders(limit=50, cursor=cursor, status=status)["data"]
def read_ledger_page(client, cursor=None):
return client.list_ledger(limit=50, cursor=cursor)["data"]
def find_order_on_page(client, order_id, cursor=None):
page = read_order_page(client, cursor=cursor)
order = next((item for item in page["items"] if item["id"] == order_id), None)
return order, page["next_cursor"]
def find_order(client, order_id, cursor=None, max_pages=5):
if max_pages < 1:
raise ValueError("请设置正数页数上限")
seen = {cursor}
for _ in range(max_pages):
order, next_cursor = find_order_on_page(client, order_id, cursor)
if order is not None or next_cursor is None:
return order, next_cursor
if next_cursor in seen:
raise ValueError("订单游标重复,请重新开始查询并按订单 ID 去重")
seen.add(next_cursor)
cursor = next_cursor
return None, cursor核对一笔已付款订单
- 使用原报价 ID 查询该次购买。已付款时,保存
receipt.order_id、receipt.amount和receipt.currency。还应保留balance_before、balance_after以及付款时间。 - 用回执的订单 ID 匹配订单列表的
id。order_number用于展示和沟通,接口操作使用id。 - 在流水中用
before、change、after展示单条余额变动。例如100.00、-12.00、88.00表示余额减少了12.00。流水的id标识该条流水,关联应用可能为空。根据原付款或退款回执确认对应交易。
订单与流水列表中的 currency 是当前展示单位。历史交易的金额和单位以原付款或退款回执为准。付款或退款响应丢失时,保留现有业务 ID,先按原报价或原订单查询结果。