跳过主要内容
返回到插件
@capgo/native-purchases
教程
@capgo/native-purchases

原生购买

Implement native in-app purchases, subscriptions, and iOS StoreKit commitment billing plans with a simple API

指南

Native Purchases教程

在设备上测试

Download the Capgo app, then scan the QR code.

原生购买插件预览二维码code

使用@capgo/native-purchases

轻松实现内购订阅

安装

bun add @capgo/native-purchases
bunx cap sync

此插件暴露的内容

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

示例使用方法

restorePurchases

恢复用户的历史记录并将其应用用户ID与使用相同应用用户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对于支持的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订阅,月度付款,12个月承诺

在支持的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/native-purchases 查看 @capgo/native-purchases 的实现细节 Getting Started 查看 Getting Started 的实现细节 Capgo Pricing 查看 Capgo Pricing 的产品工作流 支付系统 为支付系统的实现细节, 和 营收手册 为营收手册的实现细节.