指南
原生购买教程
使用 @capgo/native-purchases
让应用内订阅变得简单。
安装
bun add @capgo/native-purchases
bunx cap sync
该插件暴露的内容
restorePurchases- 恢复用户的之前版本并将其与任何使用相同的用户ID的用户关联起来getAppTransaction- 获取应用程序交易信息,提供有关用户最初下载或购买应用程序的详细信息isEntitledToOldBusinessModel- 将应用程序交易中的原始应用程序版本与目标版本进行比较,以确定用户是否有权从早期商业模式中获得特征getProducts- 加载商店产品元数据,包括支持的iOS订阅定价条款purchaseProduct- 为选择的产品和计费计划启动购买流程
示例用途
restorePurchases
恢复用户的之前版本并将其与任何使用相同的用户ID的用户关联起来
import { NativePurchases } from '@capgo/native-purchases';
await NativePurchases.restorePurchases();
getAppTransaction
获取应用程序交易信息,提供有关用户最初下载或购买应用程序的详细信息
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
比较应用程序交易中的原始应用程序版本与目标版本,以确定用户是否有权从早期商业模式中获得特征
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的基本计划ID planIdentifier. For supported iOS subscriptions that offer monthly billing with a 12-month commitment, pass 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 价格中产品工作流 支付系统 支付系统的实现细节 营收手册 营收手册的实现细节