跳过主要内容

原生购买 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中使用原生购买

使用@capgo/native-purchases

轻松实现内购订阅

安装

bun add @capgo/native-purchases
bunx cap sync

此插件暴露的内容

  • restorePurchases - 恢复用户的之前购买记录并将其与任何使用相同的 appUserIDs 的用户关联。
  • getAppTransaction - 获取 App 交易信息,提供有关用户最初下载或购买应用的详细信息。
  • isEntitledToOldBusinessModel - 比较 App 交易中的原始应用版本与目标版本,以确定用户是否有权从早期商业模式中获得特征。
  • getProducts - 加载商店产品元数据,包括支持的 iOS 订阅定价条款。
  • purchaseProduct - 为选择的产品和计费计划启动购买流程。

- 示例使用

restorePurchases

- 恢复用户的之前购买记录并将其与任何使用相同的 appUserIDs 的用户关联。

import { NativePurchases } from '@capgo/native-purchases';

await NativePurchases.restorePurchases();

getAppTransaction

获取 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

将原始应用程序版本与目标版本进行比较,以确定用户是否有权从较早的商业模式中获得特性。

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对于支持 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 可以通过 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/原生购买 查看 @capgo/native-purchases 查看 Getting Started 查看 __CAPGO_KEEP_0__ Pricing 查看 Capgo Pricing 为产品工作流程中的Capgo定价 查看 Payment system __CAPGO_KEEP_0__ Pricing 营收手册 营收手册的实施细节。

常见问题

本地购买插件常见问题

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

运行"bun add @capgo/native-purchases"(或"npm install @capgo/native-purchases"),然后运行"bunx cap sync",使iOS和Android项目捕获native code。从"@capgo/native-purchases"中导入它到您的应用code。

@capgo/native-purchases与React、Vue和Angular兼容吗?

是的。Native Purchases是一个Capacitor包,因此它与任何在Capacitor中运行的Web框架兼容,包括Ionic、React、Vue、Angular、Svelte和普通JavaScript。

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

Capgo插件遵循Capacitor的主要版本:使用与Capacitor主要版本匹配的插件主要版本(例如插件v8与Capacitor 8)。在GitHub README中列出的维护版本的兼容性表格。

@capgo/native-purchases是否免费且开源?

是的。源code在GitHub上公开,https://github.com/Cap-go/capacitor-native-purchases/,并且包是从npm免费安装的。欢迎bug报告和拉取请求。

是否可以在不经过App Store审查的情况下更新code,该应用使用Native Purchases?

安装或升级插件会更改native code,因此需要新的商店构建。之后,JavaScript、HTML和CSS更改,调用插件的更改,可以立即与Capgo实时更新一起发布。

无需等待应用程序审查即可将 Native Purchases 的更改部署到生产环境

一旦插件在您的商店中构建完成,Capgo 即可推送您的 JavaScript、HTML 和 CSS 更改到用户中,仅需几分钟。

开始使用 Capgo