指南
如何在 Capacitor 中使用 Native Purchases
使用 @capgo/native-purchases
应用内订阅,轻松搞定
安装
bun add @capgo/native-purchases
bunx cap sync
插件暴露的内容
restorePurchases- 恢复用户之前的记录,并将其 appUserIDs 与任何同样使用这些 ID 的用户关联。getAppTransaction- 获取 App Transaction 信息,其中包含用户最初下载或购买 App 的详细信息。isEntitledToOldBusinessModel- 将 App Transaction 中的原始 App 版本与目标版本进行对比,以确定用户是否有权使用旧版商业模式的功能。getProducts- 加载商店产品元数据,包括支持的 iOS 订阅定价条款。purchaseProduct- 开始所选产品和计费方案的购买流程。
用法示例
restorePurchases
恢复用户之前的记录,并将其 appUserIDs 与任何同样使用这些 ID 的用户关联。
import { NativePurchases } from '@capgo/native-purchases';
await NativePurchases.restorePurchases();
getAppTransaction
获取 App Transaction 信息,其中包含用户最初下载或购买 App 的详细信息。
import { NativePurchases } from '@capgo/native-purchases';
const { appTransaction } = await NativePurchases.getAppTransaction();
// Check if user downloaded before version 2.0.0 (when subscription was added)
if (compareVersions(appTransaction.originalAppVersion, '2.0.0') < 0) {
// User gets free access - they downloaded before subscriptions
grantFreeAccess();
}
isEntitledToOldBusinessModel
将 App Transaction 中的原始 App 版本与目标版本进行对比,以确定用户是否有权使用旧版商业模式的功能。
import { NativePurchases } from '@capgo/native-purchases';
// Check if user downloaded before version 2.0.0/build 42 (when subscription was added)
const result = await NativePurchases.isEntitledToOldBusinessModel({
targetVersion: '2.0.0',
targetBuildNumber: '42'
});
if (result.isOlderVersion) {
console.log(`User downloaded v${result.originalAppVersion}, granting free access`);
grantFreeAccess();
}
purchaseProduct
开始指定产品的购买流程。对于 Android 订阅,请传入 Google Play base plan ID 作为 planIdentifier。对于支持 12 个月合约月付模式的 iOS 订阅,请传入 billingPlanType: 'monthly'.
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
await NativePurchases.purchaseProduct({
productIdentifier: 'com.example.premium.monthly',
planIdentifier: 'monthly-plan',
productType: PURCHASE_TYPE.SUBS,
});
iOS 按月订阅承诺方案
在受支持的 iOS 版本上,StoreKit 可以通过以下方式提供包含 12 个月承诺期的按月计费方案: pricingTerms请在购买前的付费墙上展示这些条款:
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
const { products } = await NativePurchases.getProducts({
productIdentifiers: ['com.example.premium.yearly'],
productType: PURCHASE_TYPE.SUBS,
});
const yearlyProduct = products.find(
(product) => product.identifier === 'com.example.premium.yearly',
);
const monthlyCommitment = yearlyProduct?.pricingTerms?.find(
(term) => term.billingPlanType === 'monthly',
);
if (yearlyProduct && monthlyCommitment) {
console.log('Monthly price:', monthlyCommitment.billingDisplayPrice);
console.log('Total commitment:', monthlyCommitment.commitmentInfo?.priceString);
const transaction = await NativePurchases.purchaseProduct({
productIdentifier: yearlyProduct.identifier,
productType: PURCHASE_TYPE.SUBS,
billingPlanType: 'monthly',
});
console.log('Billing plan:', transaction.billingPlanType);
console.log('Commitment ends:', transaction.commitmentInfo?.expirationDate);
}
完整参考
- GitHub: https://github.com/Cap-go/capacitor-native-purchases/
- 文档:/docs/plugins/native-purchases/
- iOS 按月订阅承诺指南:/docs/plugins/native-purchases/ios-monthly-commitment/
继续探索 @capgo/native-purchases 的用法
如果你正在使用 使用 @capgo/native-purchases 要规划付款和购买,请将其与 @capgo/native-purchases 关于 @capgo/native-purchases 中的实现细节, 快速入门 有关“入门指南”中的实现细节, Capgo 定价 关于 Capgo 定价中的产品工作流, 支付系统 关于支付系统中的实现细节,以及 营收手册 关于“Revenue Playbook”中的实现细节。