跳转至正文

Capgo Native Purchases Capacitor 插件

通过简单的 API 实现原生应用内购买、订阅以及 iOS StoreKit 承诺计费方案

安装

bun add @capgo/native-purchases bunx cap sync
npm、pnpm 或 yarn
  • npm install @capgo/native-purchases
  • pnpm add @capgo/native-purchases
  • yarn add @capgo/native-purchases

指南

如何在 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);
}

完整参考

继续探索 @capgo/native-purchases 的用法

如果你正在使用 使用 @capgo/native-purchases 要规划付款和购买,请将其与 @capgo/native-purchases 关于 @capgo/native-purchases 中的实现细节, 快速入门 有关“入门指南”中的实现细节, Capgo 定价 关于 Capgo 定价中的产品工作流, 支付系统 关于支付系统中的实现细节,以及 营收手册 关于“Revenue Playbook”中的实现细节。

常见问题

Native Purchases 插件常见问题

如何在 Capacitor 应用中安装 Native Purchases 插件?

运行 &quot;bun add @capgo/native-purchases&quot;(或 &quot;npm install @capgo/native-purchases&quot;),然后运行 &quot;bunx cap sync&quot;,让 iOS 和 Android 项目加载到原生 code。再在应用的 code 中从 &quot;@capgo/native-purchases&quot; 导入。

@capgo/native-purchases 支持 React、Vue 和 Angular 吗?

支持。Native Purchases 是一个 Capacitor 包,因此适用于任何运行在 Capacitor 里的 Web 框架,包括 Ionic、React、Vue、Angular、Svelte 和纯 JavaScript。

@capgo/native-purchases 支持哪个版本的 Capacitor?

Capgo 插件遵循 Capacitor 的主版本号:请使用与 Capacitor 主版本号一致的插件主版本(例如插件 v8 对应 Capacitor 8)。GitHub README 中的兼容性表列出了仍在维护的版本。

@capgo/native-purchases 是免费开源的吗?

是的。code 的源码已在 GitHub 公开,地址为 https://github.com/Cap-go/capacitor-native-purchases/,并且可以通过 npm 免费安装。欢迎提交 Bug 报告和 Pull Request。

我可以在无需经过 App Store 审核的情况下,更新使用了 Native Purchases 的 code 吗?

安装或升级插件会改动原生 code,因此需要重新做一次商店构建。之后,调用该插件的 JavaScript、HTML 和 CSS 改动就能通过 Capgo live updates 立即发布。

无需等待应用审核,即可发布 Native Purchases 的变更

只要插件已包含在你的商店版本中,Capgo live updates 就能在几分钟内把 JavaScript、HTML 和 CSS 的改动推送给用户。

开始使用 Capgo