本指南中介绍的 cordova-plugin-purchase 的替代方案 许多 Capacitor 团队在超出其范围时采用的 CdvPurchase.store 工作流程是 @capgo/native-purchases。它与 iOS 上的 StoreKit 2 和 Android 上的 Google Play Billing 7 通过 Promise 方法(如 getProducts, purchaseProduct,以及 restorePurchases,其中 transactionUpdated 监听器,而非 store.when() 链式调用。本指南将解释在何种情况下这种权衡是合理的,如何安装该插件,以及如何映射常见的 cordova-plugin-purchase 调用。
为什么团队在新 Capacitor code 中弃用 cordova-plugin-purchase
cordova-plugin-purchase 正在积极维护中。npm 显示版本 13.20.0 以最后一个 modified 发布于 2026 年 10 月 27 日。README 指出该插件支持 Capacitor 至 capacitor-plugin-cdv-purchase 以及全局 CdvPurchase.store 带产品注册的 facade, store.initialize(),以及 store.when() 已批准的处理器。
团队仍选择 @capgo/native-purchases 在 Capacitor 迁移中:
- 更简单的 API 结构。 Capgo 为每个商店操作暴露一个方法(
purchaseProduct,getPurchases,acknowledgePurchase) 而非 FSM 风格store.when().productUpdated().approved()pipeline。 - README 中列出的原生计费栈。 iOS 15+ 上的 StoreKit 2 和 Android 上的 Google Play Billing 7.x,以及文档中记录的 iOS 订阅承诺辅助功能
/docs/plugins/native-purchases/. - Capacitor 优先的打包方式。 使用
npm install @capgo/native-purchases和cap sync安装,无需携带 Cordova 全局变量或独立的capacitor-plugin-cdv-purchasebridge,除非你仍需要从 CdvPurchase 发起 Braintree 支付请求。 - 开源维护。 Capgo 的修复通过公共仓库发布,由人工每日审查、测试并合并 50 多个 pull request。
如果你依赖 CdvPurchase 独有的功能,例如 Braintree 支付请求或 Iaptic 验证器快捷方式,请继续使用 cordova-plugin-purchase 或者在迁移结账流程期间暂时同时运行这两个插件。
安装
bun add @capgo/native-purchases
bunx cap sync
Android: 添加账单权限:
<uses-permission android:name="com.android.vending.BILLING" />
在 App Store Connect 和 Google Play Console 中配置产品,然后按照从 插件 README (docs/iOS_TESTING_GUIDE.md 和 docs/ANDROID_TESTING_GUIDE.md).
使用示例
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
const productId = 'com.example.premium.monthly';
export async function buySubscription() {
const { isBillingSupported } = await NativePurchases.isBillingSupported();
if (!isBillingSupported) {
throw new Error('Billing unavailable');
}
await NativePurchases.addListener('transactionUpdated', (event) => {
console.log('Transaction', event.transaction?.transactionId);
});
const { products } = await NativePurchases.getProducts({
productIdentifiers: [productId],
productType: PURCHASE_TYPE.SUBS,
});
await NativePurchases.purchaseProduct({
productIdentifier: productId,
productType: PURCHASE_TYPE.SUBS,
planIdentifier: 'monthly-plan',
quantity: 1,
});
}
调用 restorePurchases() 从您的设置屏幕以及 manageSubscriptions() 当用户需要商店订阅界面时。
从 Cordova 迁移到 Capacitor
卸载 Cordova cordova-plugin-purchase 或仅保留在未移植的屏幕上。导入 NativePurchases 并替换 store 用显式异步流程替代编排。根据各平台规则,使用 finishTransaction() (iOS 端)以及 acknowledgePurchase() 或 consumePurchase() 在 Android 上完成或确认交易(README 要求如此)。
API 映射表
cordova-plugin-purchase (CdvPurchase.store) |
@capgo/native-purchases |
|---|---|
store.register([{ id, type }]) |
getProducts({ productIdentifiers, productType }) |
store.initialize([Platform.APPLE, Platform.GOOGLE]) |
isBillingSupported() 然后 getProducts() |
store.ready() / store.update() |
await getProducts() 在计费检查后 |
store.get(productId) |
getProduct({ productIdentifier, productType }) |
store.order(offer) / product.getOffer().order() |
purchaseProduct({ productIdentifier, productType, planIdentifier, quantity }) |
store.requestPayment(...) (Braintree) |
不在 Capgo 插件中,保留 CdvPurchase 或 Stripe |
store.restore() |
restorePurchases() |
store.owned(productId) |
检查 getPurchases() 结果或服务器授权 |
store.when().approved(handler) |
addListener('transactionUpdated', ...) |
store.validator = url |
在您的服务器上验证收据或购买令牌 |
store.refresh() |
getProducts() 或 getPurchases() |
product.transaction.finish() |
finishTransaction({ transactionIdentifier }) |
| 消耗品 consume | consumePurchase({ purchaseToken }) (Android) |
PURCHASE_TYPE.INAPP 映射到消耗品和非消耗品产品。 PURCHASE_TYPE.SUBS 映射到付费订阅。来自 Play Console 的 Android 基础计划 ID 会直接传递 planIdentifier 开启 purchaseProduct(),如入门指南中所述。
链接与后续步骤
- 插件文档: https://capgo.app/docs/plugins/native-purchases/
- 营销页面: /plugins/capacitor-native-purchases/
- npm: https://www.npmjs.com/package/@capgo/native-purchases
- GitHub: https://github.com/Cap-go/capacitor-native-purchases/
- cordova-plugin-purchase: https://github.com/j3k0/cordova-plugin-purchase
计费规则变更需要原生重新构建。请使用 Capgo 构建 用于签名产物。使用 Live Update 可以更快地发布 Paywall 文案和定价 UI。 Capgo 实时更新 待商店二进制文件更新后。参见 从 Cordova 迁移到 Capacitor 以获取更完整的迁移清单。