跳到正文
Indiana
控制台
开发文档
章节目录 / 支付、订阅与账单

支付、订阅与账单

报价与确认购买、Paywall、订阅变更、试用、退款、发票与原生购买。

API v1更新于

发起一次购买

  • 调用 previewAttach 生成报价和 operationId,此时不扣款。
  • 调用 attach 确认;返回 checkoutUrl 时跳转到 Stripe 付款页。
  • 用户回来后调用 readSubscriptionOperation 读取结果,complete 表示已支付并生效。
purchase.tsTypeScript
import { createIndiana } from "@tinkerlandai/indiana-sdk"; const indiana = createIndiana({  baseUrl: "https://api.tinker.cash",  scope: { projectId: "<APP_UUID>", environment: "sandbox" },}); export async function buy(customerToken: string, requestId: string) {  indiana.identify(customerToken);  const { quote } = await indiana.previewAttach({    requestId, // persisted before the first attempt    plans: [{ productVersionId: "<PRODUCT_VERSION_UUID>" }],    provider: "stripe",    currency: "USD",  });  const { operation } = await indiana.attach({ operationId: quote.operationId });  if (operation.checkoutUrl) window.location.assign(operation.checkoutUrl);  return operation.operationId;} export async function afterReturn(operationId: string) {  const { operation } = await indiana.readSubscriptionOperation({ operationId });  return operation.status; // "pending" | "complete" | "cancelled" | "failed"}
  • 先在 Console 的支付连接中为该 App 和环境连接 Stripe;Sandbox 使用 Stripe 测试模式。
  • 浏览器只能为当前用户购买;后端用 Server Key 调用时传入 customerId。
  • 保存 requestId,重试时使用同一个值;不要根据跳转结果判断是否付款成功。

展示 Paywall

在 Console 发布 Paywall 后,用它的 ID 在页面中展示。用户购买后,SDK 自动完成报价与确认。

paywall.tsTypeScript
import { createPaywallSession, showPaywall } from "@tinkerlandai/indiana-sdk"; const options = {  baseUrl: "https://api.tinker.cash",  scope: { projectId: "<APP_UUID>", environment: "sandbox" as const },}; export function openPaywall(container: HTMLElement, customerToken: string) {  return showPaywall(container, {    start: () =>      createPaywallSession(options, {        paywallId: "<PAYWALL_UUID>",        customerToken,        commerce: { provider: "stripe", currency: "USD" },        locale: "en",      }),    onCheckoutUrl: (url) => window.location.assign(url),    onAccessGranted: (check) => console.log("access", check.allowed),  });}

变更订阅、试用与退款

app/change-plan.tsTypeScript
import { createIndiana } from "@tinkerlandai/indiana-sdk"; const indiana = createIndiana({  baseUrl: "https://api.tinker.cash",  scope: { projectId: "<APP_UUID>", environment: "sandbox" },}); export async function changePlan(customerToken: string, requestId: string, productVersionId: string) {  indiana.identify(customerToken);  const { holdings } = await indiana.readHoldings();  const membership = holdings.memberships[0];  if (!membership) throw new Error("No membership to change");   // 1. Quote the change; nothing is charged yet.  const { quote } = await indiana.previewUpdate({    requestId, // persisted before the first attempt    membershipId: membership.id,    timing: "immediate",    change: { type: "plan", productVersionId },  });   // 2. After the user accepts the quote, confirm it.  const { operation } = await indiana.update({ operationId: quote.operationId });  return operation;}
常见任务
任务方法说明
升级、降级或取消previewUpdate → update → readSubscriptionUpdatetiming 选择立即、续订时或在商店中生效。
保存付款方式setupPayment → readPaymentSetup不扣款。
开始、延长或结束试用manageSubscriptionTrial, readSubscriptionTrial后端调用。
退款previewRefund → refund → readRefund全额或部分;可选择是否撤销权益。
查询订阅与购买listSubscriptions, listPurchases, verifySubscriptions后端调用。
处理订阅冲突listSubscriptionConflicts, retrySubscriptionConflict后端调用。

常见任务

  • 升级、降级或取消

    方法
    previewUpdate → update → readSubscriptionUpdate
    说明
    timing 选择立即、续订时或在商店中生效。
  • 保存付款方式

    方法
    setupPayment → readPaymentSetup
    说明
    不扣款。
  • 开始、延长或结束试用

    方法
    manageSubscriptionTrial, readSubscriptionTrial
    说明
    后端调用。
  • 退款

    方法
    previewRefund → refund → readRefund
    说明
    全额或部分;可选择是否撤销权益。
  • 查询订阅与购买

    方法
    listSubscriptions, listPurchases, verifySubscriptions
    说明
    后端调用。
  • 处理订阅冲突

    方法
    listSubscriptionConflicts, retrySubscriptionConflict
    说明
    后端调用。

开启自动充值

余额低于 threshold 时自动购买,使用用户已保存的付款方式。必须先向用户展示 prepared.terms,用户同意后再确认。

auto-topup.tsTypeScript
import { createIndiana } from "@tinkerlandai/indiana-sdk"; const indiana = createIndiana({  baseUrl: "https://api.tinker.cash",  scope: { projectId: "<APP_UUID>", environment: "sandbox" },}); type UUID = ReturnType<typeof crypto.randomUUID>; export async function prepareAutoTopup(customerToken: string, requestId: UUID) {  indiana.identify(customerToken);  const { prepared } = await indiana.prepareBillingAuthorization({    requestId,    kind: "auto_topup",    productVersionId: "<CREDIT_PACK_VERSION_UUID>",    internal_feature_id: "<CREDIT_FEATURE_UUID>",    currency: "USD",    threshold: "100",    quantity: "1",  });  return prepared; // show prepared.terms to the Customer} // Call only after the Customer explicitly accepts the displayed terms.export async function acceptAutoTopup(authorizationId: string) {  return indiana.confirmBillingAuthorization({ authorizationId, accepted: true });}

发票与用量上限

常见任务
任务方法调用方
创建与管理发票previewInvoice, createInvoice, manageInvoice, readInvoiceOperation后端
查询发票listInvoiceTemplates, listInvoices, getInvoice后端
设置用量上限与提醒manageBillingLimits后端
用户查看自己的账单listBillingInvoices, readBillingInvoice, readBillingControls浏览器
用户为会员选择优惠setBillingCoupons浏览器

常见任务

  • 创建与管理发票

    方法
    previewInvoice, createInvoice, manageInvoice, readInvoiceOperation
    调用方
    后端
  • 查询发票

    方法
    listInvoiceTemplates, listInvoices, getInvoice
    调用方
    后端
  • 设置用量上限与提醒

    方法
    manageBillingLimits
    调用方
    后端
  • 用户查看自己的账单

    方法
    listBillingInvoices, readBillingInvoice, readBillingControls
    调用方
    浏览器
  • 用户为会员选择优惠

    方法
    setBillingCoupons
    调用方
    浏览器

原生 App 购买

  • 调用 previewAttach(provider 为 apple 或 google)取得购买意图。
  • 在 App 中完成 App Store 或 Google Play 支付。
  • 把 Apple signedTransaction 或 Google purchaseToken 提交给 restoreNativePurchase,验证后权益生效。
  • 恢复购买时调用同一接口的 read 分支。

相关:iOS 与 Android

相关接口

订阅与支付 (19)
setupPayment, readPaymentSetup, verifySubscriptions, previewAttach, attach, readSubscriptionOperation, previewUpdate, update, readSubscriptionUpdate, listSubscriptions, listPurchases, listSubscriptionConflicts, retrySubscriptionConflict, manageSubscriptionTrial, readSubscriptionTrial, previewSubscriptionBatch, confirmSubscriptionBatch, readSubscriptionBatch, abandonSubscriptionBatch
发票、退款与计费 (19)
listInvoiceTemplates, previewInvoice, createInvoice, manageInvoice, readInvoiceOperation, listInvoices, getInvoice, previewRefund, refund, readRefund, readBillingControls, prepareBillingAuthorization, confirmBillingAuthorization, setBillingCoupons, revokeBillingAuthorization, listBillingInvoices, manageBillingLimits, readBillingInvoice, confirmBillingAdjustment
原生购买 (1)
restoreNativePurchase

相关:路径、凭据与权限见全部接口