支付、订阅与账单
报价与确认购买、Paywall、订阅变更、试用、退款、发票与原生购买。
API v1更新于
准备 Offering 与 Paywall#
- 在同一 App/环境创建所需 Feature 和 Product,保存价格与权益,并使用保存后返回的 Product 标识和版本。Product 没有独立的发布动作。
- 从这些 Product 创建 Offering 并发布 Offering Version。Paywall 发布时固定一个已发布 Offering Version,选择需要展示的 Product、顺序和默认项。
- 保存 Paywall ID;新的 Offering 发布不会自动切换已有 Paywall 的固定版本。展示 Paywall 不要求创建 Funnel。
- 后端以已验证用户建立 Customer,使用具有 sessions:create、entitlements:read、billing:read、billing:write 的 Key 签发会话;为浏览器配置准确 Origin。需要支付时,为同一 App/环境配置 Stripe 连接,先在 Sandbox 验证。
相关:Stripe 接入:权限、webhook 与激活·Customer 与会话·CLI 资源配置与发布·商户准备报价与执行
Product 版本与已有客户#
Product 保存后直接返回其标识和版本,没有额外的发布步骤。Offering 与 Paywall 仍各自发布独立版本。新建 Product 版本不会自动将已有客户的持有记录迁移到新版本。
版本选项位于 Product 更新写入操作的 query 对象。Console 仅提供“将本次修改保存为新版本”复选框,对应 query.force_version;disable_version 和 all_versions 仅能在写入操作的 query 中设置,没有对应的 Console 开关。
更新方式与影响范围| 改动 | 行为 |
|---|
| 价格、权益、试用或 License | 默认更新时,如果所选版本有 active、past_due、scheduled 或 paused 状态的客户持有记录,变化的价格/权益条目或试用配置会创建新版本;在这些条件下,提交 licenses 也会创建新版本,即使其内容未变。只有 expired 持有记录时不触发这一条件,历史持有记录继续引用原有的价格与权益定义。 |
| 仅计费设置变化 | 只改 billing controls(计费设置)时,只要所选版本有上述状态的客户持有记录、其他比较字段都没变,默认更新也会创建新版本。 |
force_version | 即使提交的字段值未变化,也可强制创建新版本;空更新或仅包含 base_plan_id 的更新不会创建新版本。不能与 disable_version 或 all_versions 同用。Product 信息和 metadata 仍遵守下面的原地更新规则。 |
disable_version | 使用原地更新路径,可能修改已有客户使用的版本。 |
all_versions | 将本次更新产生的变更原地应用到历史版本;不能与 force_version 或 disable_version 同用,也不支持跨所有版本更新 licenses。 |
| Product 信息与 metadata | 修改名称、描述、分组、默认标记、归档状态和 config 等信息时,会原地改写所选版本,部分同时创建新版本的更新也是如此。metadata 按 Product 标识在所有版本间共享。 |
更新方式与影响范围
价格、权益、试用或 License
- 行为
- 默认更新时,如果所选版本有
active、past_due、scheduled 或 paused 状态的客户持有记录,变化的价格/权益条目或试用配置会创建新版本;在这些条件下,提交 licenses 也会创建新版本,即使其内容未变。只有 expired 持有记录时不触发这一条件,历史持有记录继续引用原有的价格与权益定义。
仅计费设置变化
- 行为
- 只改 billing controls(计费设置)时,只要所选版本有上述状态的客户持有记录、其他比较字段都没变,默认更新也会创建新版本。
force_version
- 行为
- 即使提交的字段值未变化,也可强制创建新版本;空更新或仅包含
base_plan_id 的更新不会创建新版本。不能与 disable_version 或 all_versions 同用。Product 信息和 metadata 仍遵守下面的原地更新规则。
disable_version
- 行为
- 使用原地更新路径,可能修改已有客户使用的版本。
all_versions
- 行为
- 将本次更新产生的变更原地应用到历史版本;不能与
force_version 或 disable_version 同用,也不支持跨所有版本更新 licenses。
Product 信息与 metadata
- 行为
- 修改名称、描述、分组、默认标记、归档状态和 config 等信息时,会原地改写所选版本,部分同时创建新版本的更新也是如此。metadata 按 Product 标识在所有版本间共享。
创建客户或访客被识别为客户时,按资格分配默认 Product;默认 Product 可以包含无需绑卡的付费周期试用。已有客户再次登录不会重新分配。修改默认标记不会为已有客户重新初始化 Product。
发起一次购买#
- 调用
previewAttach 生成报价和 operationId,此时不扣款。 - 调用
attach 确认;返回 checkoutUrl 时跳转到 Stripe 付款页。 - 跳转前保存
operationId。用户回来时页面已重新加载,先重新调用 identify,再用 readSubscriptionOperation 读取结果;complete 表示已支付并生效,pending 时稍后再读。
1import { createLongplay } from "@tinkerlandai/longplay-sdk";2 3const longplay = createLongplay({4 baseUrl: "https://api.longplayai.com",5 scope: { projectId: "<APP_UUID>", environment: "sandbox" },6});7 8export async function buy(customerToken: string, requestId: string) {9 longplay.identify(customerToken);10 const { quote } = await longplay.previewAttach({11 requestId, // persisted before the first attempt12 plans: [{ productVersionId: "<PRODUCT_VERSION_UUID>" }],13 provider: "stripe",14 currency: "USD",15 });16 const { operation } = await longplay.attach({ operationId: quote.operationId });17 // Checkout leaves this page, so keep the operation ID for the return page.18 sessionStorage.setItem("indiana-operation", operation.operationId);19 if (operation.checkoutUrl) window.location.assign(operation.checkoutUrl);20 return operation.operationId;21}22 23// The return from checkout is a new page load: identify again before reading.24export async function afterReturn(customerToken: string) {25 const operationId = sessionStorage.getItem("indiana-operation");26 if (!operationId) return null;27 longplay.identify(customerToken);28 const { operation } = await longplay.readSubscriptionOperation({ operationId });29 if (operation.status !== "pending") sessionStorage.removeItem("indiana-operation");30 return operation.status; // "pending" | "complete" | "cancelled" | "failed"31}
- 先在 Console 的支付连接中为该 App 和环境连接 Stripe;Sandbox 使用 Stripe 测试模式。
- 浏览器只能为当前用户购买;后端用 Server Key 调用时传入
customerId。 - 保存
requestId,重试时使用同一个值;不要根据跳转结果判断是否付款成功。 - 商户执行付款(
record_only)时不需要 Stripe:报价返回商户准备的 merchantTerms;条款含 requiredConsents 时,确认 attach、update 或 refund 须提交匹配的 merchantConfirmation;付款由商户完成,操作在你用 recordMerchantEvent 上报结果后完成。见商户执行规范。
展示 Paywall#
在 Console 发布 Paywall 后,用它的 ID 在页面中展示。用户购买后,SDK 自动完成报价与确认。
1import { createPaywallSession, showPaywall } from "@tinkerlandai/longplay-sdk";2 3const options = {4 baseUrl: "https://api.longplayai.com",5 scope: { projectId: "<APP_UUID>", environment: "sandbox" as const },6};7 8export function openPaywall(container: HTMLElement, customerToken: string) {9 return showPaywall(container, {10 start: () =>11 createPaywallSession(options, {12 paywallId: "<PAYWALL_UUID>",13 customerToken,14 commerce: { provider: "stripe", currency: "USD" },15 locale: "en",16 }),17 onCheckoutUrl: (url) => window.location.assign(url),18 onAccessGranted: (check) => console.log("access", check.allowed),19 });20}
- 在宿主页面提供真实容器并调用 openPaywall(container, customerToken)。保留返回的宿主句柄,在卸载或切换 Customer 时按 SDK 生命周期清理。
- 展示成功后核对实际选项来自固定的 Offering Version。购买完成需读取原商业操作及当前权益,不能把展示、关闭或返回页面当作付款成功。
接入排错| 问题 | 检查 |
|---|
| 401 / 403 | 会话有效期、来源、Key 权限及 App/环境是否一致。 |
| Paywall 无法读取或选项不符 | 发布状态、Paywall ID、固定 Offering Version 和显示子集。 |
| 购买 pending 或请求超时 | 保留原 operationId / requestId 并查询原结果;按返回错误恢复,不创建重复购买。 |
接入排错
401 / 403
- 检查
- 会话有效期、来源、Key 权限及 App/环境是否一致。
Paywall 无法读取或选项不符
- 检查
- 发布状态、Paywall ID、固定 Offering Version 和显示子集。
购买 pending 或请求超时
- 检查
- 保留原 operationId / requestId 并查询原结果;按返回错误恢复,不创建重复购买。
相关:产品概览:Paywall
变更订阅、试用与退款#
app/change-plan.tsTypeScript 1import { createLongplay } from "@tinkerlandai/longplay-sdk";2 3const longplay = createLongplay({4 baseUrl: "https://api.longplayai.com",5 scope: { projectId: "<APP_UUID>", environment: "sandbox" },6});7 8export async function changePlan(customerToken: string, requestId: string, productVersionId: string) {9 longplay.identify(customerToken);10 const { holdings } = await longplay.readHoldings();11 const membership = holdings.memberships[0];12 if (!membership) throw new Error("No membership to change");13 14 // 1. Quote the change; nothing is charged yet.15 const { quote } = await longplay.previewUpdate({16 requestId, // persisted before the first attempt17 membershipId: membership.id,18 timing: "immediate",19 change: { type: "plan", productVersionId },20 });21 22 // 2. After the user accepts the quote, confirm it.23 const { operation } = await longplay.update({ operationId: quote.operationId });24 return operation;25}
常见任务| 任务 | 方法 | 说明 |
|---|
| 升级、降级或取消 | previewUpdate → update → readSubscriptionUpdate | timing 选择立即、续订时或在商店中生效。 |
| 保存付款方式 | 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 1import { createLongplay } from "@tinkerlandai/longplay-sdk";2 3const longplay = createLongplay({4 baseUrl: "https://api.longplayai.com",5 scope: { projectId: "<APP_UUID>", environment: "sandbox" },6});7 8type UUID = ReturnType<typeof crypto.randomUUID>;9 10export async function prepareAutoTopup(customerToken: string, requestId: UUID) {11 longplay.identify(customerToken);12 const { prepared } = await longplay.prepareBillingAuthorization({13 requestId,14 kind: "auto_topup",15 productVersionId: "<CREDIT_PACK_VERSION_UUID>",16 internal_feature_id: "<CREDIT_FEATURE_UUID>",17 currency: "USD",18 threshold: "100",19 quantity: "1",20 });21 return prepared; // show prepared.terms to the Customer22}23 24// Call only after the Customer explicitly accepts the displayed terms.25export async function acceptAutoTopup(authorizationId: string) {26 return longplay.confirmBillingAuthorization({ authorizationId, accepted: true });27}
发票与用量上限#
常见任务| 任务 | 方法 | 调用方 |
|---|
| 创建与管理发票 | 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
相关接口#
- 订阅与支付 (20)
- setupPayment, readPaymentSetup, verifySubscriptions, previewAttach, attach, readSubscriptionOperation, previewUpdate, update, readSubscriptionUpdate, listSubscriptions, listPurchases, listSubscriptionConflicts, retrySubscriptionConflict, manageSubscriptionTrial, readSubscriptionTrial, previewSubscriptionBatch, confirmSubscriptionBatch, readSubscriptionBatch, abandonSubscriptionBatch, recordMerchantEvent
- 发票、退款与计费 (20)
- listInvoiceTemplates, previewInvoice, createInvoice, manageInvoice, readInvoiceOperation, listInvoices, getInvoice, previewRefund, refund, readRefund, readBillingControls, prepareBillingAuthorization, confirmBillingAuthorization, setBillingCoupons, revokeBillingAuthorization, listBillingInvoices, manageBillingLimits, readBillingInvoice, prepareBillingAdjustment, confirmBillingAdjustment
相关:路径、凭据与权限见全部接口