Android 订阅 offer
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
这些 Android 产品字段和 purchaseProduct() 优惠规则在以下文档中说明 capacitor-native-purchases#245。以下行为与插件实现一致。
购买您展示的优惠
标题为“对已显示的报价进行计费”的章节在 Android 上, offerToken 选择要计费的报价。从同一个 getProducts() 或 getProduct() 结果中读取令牌,然后将该令牌传递给 purchaseProduct()。令牌仅属于一次产品查询,且会过期。
如果插件查询返回的产品详情中缺少令牌,插件将拒绝购买,而不是选择其他报价。其他故障也可能导致购买被拒绝。Google Play 仍可能显示不同的结账页面,例如在查询后报价不再符合条件时,因此最终价格不保证一致。
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
const { product } = await NativePurchases.getProduct({ productIdentifier: 'com.example.premium.monthly', productType: PURCHASE_TYPE.SUBS,});
const transaction = await NativePurchases.purchaseProduct({ productIdentifier: 'com.example.premium.monthly', planIdentifier: product.identifier, // Android base plan id; ignored on iOS offerToken: product.offerToken, // Android: same offer as the price above; ignored on iOS productType: PURCHASE_TYPE.SUBS,});getProducts() 可以为每个 Android 订阅返回多个条目(每个报价或基础计划一个)。请购买您显示价格的那个条目。
选择订阅优惠
标题为“订阅优惠选择”的章节对于订阅(productType: 'subs' 或 PURCHASE_TYPE.SUBS),插件需要 offerToken 或 planIdentifier。如果两者都缺失或为空,购买将被拒绝,并返回: planIdentifier cannot be empty if productType is subs unless offerToken is provided.
planIdentifier 是 基础计划 ID. getProducts() 将该值报告为 Product.identifier,而不是 Product.planIdentifier (该字段存储 Play 产品 ID)。除非提供了 offerToken ,否则 Android 订阅必须提供此字段。对于一次性产品和 iOS,该字段将被忽略。
当两者同时提供时,token 优先,因为它已经指定了某个 offer。
订阅的选择顺序:
- 显式
offerToken,精确匹配。 - 第一个 base plan 匹配的 offer
planIdentifier. - 商店返回的第一个 offer。
这两种未匹配情况处理方式不同:
- 一次
offerToken未匹配任何当前 offer 的 会被拒绝。插件不会回退到 plan 或第一个 offer。 - 一套
planIdentifier即使没有匹配任何基础计划,插件仍会回退到商店返回的第一个 offer,行为与此检查引入前一致。
一个基础计划可以包含多个 offer(例如免费试用、 introductory price)。 planIdentifier 仅指定计划名称,插件随后会购买 Play 为该计划返回的第一个 offer。请传入 offerToken 当需要确保扣费的是你展示的那个特定 offer 时。
一次性产品优惠
标题为“一次性产品 offer”的章节对于一次性产品(productType: 'inapp' 或 PURCHASE_TYPE.INAPP),不存在基础计划,因此 planIdentifier 不起作用。
- 如果你传入
offerToken,插件会选择该特定的一次性 offer。如果 token 不在查询结果中,购买将被拒绝。 - 如果省略
offerToken,当产品仍存在向后兼容的购买选项时,插件不会设置 offer token。此时选择权交给 Play:它会收取标记为向后兼容的购买选项(默认是第一个创建的,或者如果标记了其他选项,则是legacyCompatible)。这未必是第一个getProducts()返回的 offer。 - 只有当产品没有向后兼容选项时,插件才会使用列表中的第一个 offer。
当特定的单次 offer 很重要时,请传递 token。
subscriptionPeriod 是可选的
标题为“subscriptionPeriod 是可选的”的章节Product.subscriptionPeriod 现在为可选参数。这是一处 TypeScript 类型定义的破坏性变更:读取 code 的 product.subscriptionPeriod.unit 现在缺少检查会导致类型检查失败。请使用可选链,例如 product.subscriptionPeriod?.unit.
| 平台 | 当字段被设置时 |
|---|---|
| iOS | 来自 StoreKit subscription.subscriptionPeriod. |
| Android | 当该阶段为无限期循环或分期付款优惠的有限期循环时,来自标准定价阶段的计费周期(InstallmentPlanDetails 存在)。 |
| Android 预付费 | 预付费(非循环)优惠中省略。这些优惠仍包含价格字段(price, priceString,货币),但没有周期。 |
没有分期付款计划详情的有限期循环阶段也会省略 subscriptionPeriod.
const periodLabel = product.subscriptionPeriod ? `${product.subscriptionPeriod.numberOfUnits} ${product.subscriptionPeriod.unitString ?? 'period'}` : 'See price at checkout';subscriptionBillingCycles (仅限 Android)
标题为“subscriptionBillingCycles (Android only)”的章节Product.subscriptionBillingCycles 仅适用于 Android。这是分期基础计划的承诺付款次数,取自 InstallmentPlanDetails.getInstallmentPlanCommitmentPaymentsCount() Play 在优惠中公开该值时。仅当该次数大于零时,插件才会设置此字段。iOS 上不可用。
const cycles = product.subscriptionBillingCycles;if (cycles !== undefined && cycles > 0 && product.subscriptionPeriod?.unitString) { console.log( `${product.priceString} each ${product.subscriptionPeriod.unitString}, ${cycles} payments`, );}