计费
面向你的终端客户的扣款与退款。
概览
基础路径:
/v1/billingPython 命名空间:
infra.billingtext
# Python
from infrai import infra
# TypeScript
import { InfraiClient } from "@infrai/sdk";
const client = new InfraiClient({ apiKey: process.env.INFRAI_API_KEY! });
// → client.billing方法
billing.charge.create
POST /v1/billing/charges
对客户或支付方式发起一笔扣款。
Python
python
infra.billing.charge.create(amount=..., currency=..., customer_id=None, idempotency_key=None)TypeScript
typescript
client.billing.charge(opts: ChargeOptions): Promise<Charge>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
amount | number | 必填 | 以最小货币单位表示的金额。 |
currency | string | 必填 | ISO 货币代码。 |
customer_id | string | 可选 | 要扣款的客户 id。 |
payment_method_id | string | 可选 | 支付方式 id。 |
idempotency_key | string | 可选 | 可选去重 key;相同重试将返回同一结果。 |
返回
Charge { charge_id, amount, currency, state, customer_id? }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
charge = infra.billing.charge.create(amount=19.99, currency="USD", customer_id="cus_123")
print("charge:", charge.get("charge_id"), "| state:", charge.get("state"))billing.charge.get
GET /v1/billing/charges/{id}
按 id 获取扣款。
Python
python
infra.billing.charge.get(id=charge_id)TypeScript
typescript
client.billing.getCharge(chargeId: string): Promise<Charge>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
id | string | 必填 | 扣款 id。 |
返回
Charge示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
c = infra.billing.charge.get(id="ch_...")
print("state:", c.get("state"), "| amount:", c.get("amount"))billing.refund.create
POST /v1/billing/refunds
对扣款进行全额或部分退款。
Python
python
infra.billing.refund.create(charge_id=..., amount=None, idempotency_key=None)TypeScript
typescript
client.billing.refund(opts): Promise<Refund>参数
| 名称 | 类型 | 必填 | 说明 |
|---|---|---|---|
charge_id | string | 必填 | 扣款 id。 |
amount | number | 可选 | 以最小货币单位表示的金额。 |
idempotency_key | string | 可选 | 可选去重 key;相同重试将返回同一结果。 |
返回
Refund { refund_id, charge_id, amount, state }示例
一次性前置(每个范例都假定已完成):
bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...python
from infrai import infra
r = infra.billing.refund.create(charge_id="ch_...", amount=5.0)
print("refund:", r.get("refund_id"), "| state:", r.get("state"))