Getting Started
インストールステップとこのプラグインのフルマークダウンガイドのセットアッププロンプトをコピーしてください。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/native-purchases`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/native-purchases/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
インストール
「インストール」のセクションCapgoのAI-Assisted Setupを使用してプラグインをインストールできます。AIツールにCapgoスキルを追加するには、以下のコマンドを使用してください。
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins次のプロンプトを使用してください:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/native-purchases` plugin in my project.手動設定を選択する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム固有の手順に従ってください。
-
パッケージをインストール
ターミナル画面 bun add @capgo/native-purchases -
ネイティブプロジェクトと同期
ターミナル画面 bunx cap sync -
請求サポートを確認
import { NativePurchases } from '@capgo/native-purchases';const { isBillingSupported } = await NativePurchases.isBillingSupported();if (!isBillingSupported) {throw new Error('Billing is not available on this device');} -
ストアから直接製品を読み込む
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';const { products } = await NativePurchases.getProducts({productIdentifiers: ['com.example.premium.monthly','com.example.premium.yearly','com.example.one_time_unlock'],productType: PURCHASE_TYPE.SUBS, // Use PURCHASE_TYPE.INAPP for one‑time products});products.forEach((product) => {console.log(product.title, product.priceString);}); -
購入と復元フローを実装する
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';const monthlyPlanId = 'monthly-plan'; // Base Plan ID from Google Play Consoleconst transaction = await NativePurchases.purchaseProduct({productIdentifier: 'com.example.premium.monthly',planIdentifier: monthlyPlanId, // REQUIRED for Android subscriptions, ignored on iOSproductType: PURCHASE_TYPE.SUBS,quantity: 1,});console.log('Transaction ID', transaction.transactionId);await NativePurchases.restorePurchases();- App Store Connectでインアプリ製品とサブスクリプションを作成します。
- StoreKit Local TestingまたはSandboxテスターを使用してQAを行います。
- マニフェストの編集は必要ありません。製品が承認されていることを確認してください。
- Google Play Consoleでインアプリ製品とサブスクリプションを作成します。
- 少なくとも内部テストビルドをアップロードし、ライセンステスターを追加してください。
- 課金許可を追加してください。
AndroidManifest.xml:
<uses-permission android:name="com.android.vending.BILLING" /> - App Store Connectでインアプリ製品とサブスクリプションを作成します。
購入サービス例
「購入サービス例」のセクションimport { NativePurchases, PURCHASE_TYPE, Transaction } from '@capgo/native-purchases';import { Capacitor } from '@capacitor/core';
class PurchaseService { private premiumProduct = 'com.example.premium.unlock'; private monthlySubId = 'com.example.premium.monthly'; private monthlyPlanId = 'monthly-plan'; // Base Plan ID (Android only)
async initialize() { const { isBillingSupported } = await NativePurchases.isBillingSupported(); if (!isBillingSupported) throw new Error('Billing unavailable');
const { products } = await NativePurchases.getProducts({ productIdentifiers: [this.premiumProduct, this.monthlySubId], productType: PURCHASE_TYPE.SUBS, });
console.log('Loaded products', products);
if (Capacitor.getPlatform() === 'ios') { NativePurchases.addListener('transactionUpdated', (transaction) => { this.handleTransaction(transaction); }); } }
async buyPremium(appAccountToken?: string) { const transaction = await NativePurchases.purchaseProduct({ productIdentifier: this.premiumProduct, productType: PURCHASE_TYPE.INAPP, appAccountToken, });
await this.processTransaction(transaction); }
async buyMonthly(appAccountToken?: string) { const transaction = await NativePurchases.purchaseProduct({ productIdentifier: this.monthlySubId, planIdentifier: this.monthlyPlanId, // REQUIRED for Android subscriptions productType: PURCHASE_TYPE.SUBS, appAccountToken, });
await this.processTransaction(transaction); }
async restore() { await NativePurchases.restorePurchases(); await this.refreshEntitlements(); }
async openManageSubscriptions() { await NativePurchases.manageSubscriptions(); }
private async processTransaction(transaction: Transaction) { this.unlockContent(transaction.productIdentifier); this.validateOnServer(transaction).catch(console.error); }
private unlockContent(productIdentifier: string) { // persist entitlement locally console.log('Unlocked', productIdentifier); }
private async refreshEntitlements() { const { purchases } = await NativePurchases.getPurchases({ productType: PURCHASE_TYPE.SUBS, }); console.log('Current purchases', purchases); }
private async handleTransaction(transaction: Transaction) { console.log('StoreKit transaction update:', transaction); await this.processTransaction(transaction); }
private async validateOnServer(transaction: Transaction) { await fetch('/api/validate-purchase', { method: 'POST', body: JSON.stringify({ transactionId: transaction.transactionId, receipt: transaction.receipt, purchaseToken: transaction.purchaseToken, }), }); }}必要な購入オプション
「必要な購入オプション」のセクション| オプション | プラットフォーム | 説明 |
|---|---|---|
productIdentifier | iOS + Android | App Store Connect / Google Play Console で設定した SKU / Product ID |
productType | Android 限定 | PURCHASE_TYPE.INAPP または PURCHASE_TYPE.SUBSまたは INAPPデフォルト SUBS 常に |
planIdentifier | サブスクリプション用 | Android サブスクリプション用 |
billingPlanType | iOS サブスクリプション | StoreKit の請求計画を購入に使用します。 'monthly' 月額請求に 12 か月のコミットメントを設定する場合に使用します。 product.pricingTerms iOS |
quantity | iOS のみのインアプリ購入に使用します。デフォルトは | 。Android は常に 1 つのアイテムを購入します。 1iOS + Android |
appAccountToken | iOS | 購入をユーザーにリンクするための UUID/文字列。iOS では必須の UUID になりますが、Android では 64 文字以内にオブフスコードされた文字列を許可します。 |
isConsumable | Android | を設定すると、消耗可能なアイテムの特権を付与した後で自動的にトークンを消費します。デフォルトは true です。 false. |
チェックする権限の状態
セクション: チェックする権限の状態使用 getPurchases() すべての取引を表示するクロスプラットフォームのビューのために:
import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
const { purchases } = await NativePurchases.getPurchases({ productType: PURCHASE_TYPE.SUBS,});
purchases.forEach((purchase) => { if (purchase.isActive && purchase.expirationDate) { console.log('iOS sub active until', purchase.expirationDate); }
const isAndroidIapValid = ['PURCHASED', '1'].includes(purchase.purchaseState ?? '') && purchase.isAcknowledged;
if (isAndroidIapValid) { console.log('Grant in-app entitlement for', purchase.productIdentifier); }});プラットフォームの動作
セクション: プラットフォームの動作- iOS: サブスクリプションには
isActive,expirationDate,willCancel, および StoreKit 2 リスナーのサポートが含まれます。アプリ内課金にはサーバー受領の検証が必要です。 - Android:
isActive/expirationDateは埋められていません。Google Play Developer API に、purchaseTokenfor authoritative status.purchaseStatemust bePURCHASEDとisAcknowledgedmust betrue.
API quick reference
セクション「API quick reference」isBillingSupported()– StoreKit / Google Play の利用可能性を確認します。getProduct()/getProducts()– 価格、ローカライズされたタイトル、説明、イントロダクション オファー、サポートされている iOS の価格条件を取得します。purchaseProduct()– StoreKit 2 または Billing クライアント購入フローの開始、iOS の月額コミットメント請求計画を含みます。restorePurchases()– 歴史的な購入を再生し、現在のデバイスに Sync します。getPurchases()– iOS のすべての取引または Play Billing の購入をリストします。manageSubscriptions()– ネイティブのサブスクリプション管理 UI を開きます。addListener('transactionUpdated')– iOS アプリが起動したときに、StoreKit 2 の保留中のトランザクションを処理する (iOS 限定).
ベスト プラクティス
「ベスト プラクティス」セクション- ショップの価格を表示 – Apple は表示することを要求しています
product.titleそしてproduct.priceString;ハードコードしないこと。 - 使用
appAccountToken– ユーザー ID から UUID (v5) を決定的に生成して、購入をアカウントにリンクする。 - サーバー側で検証 – (iOS) /
receipt–purchaseTokenバックエンドに送信して検証 - エラーを柔軟に処理 – ユーザーのキャンセル、ネットワークの失敗、非対応の請求環境を確認
- 徹底的にテスト – iOS サンドボックス ガイド と Android サンドボックス ガイド.
- 請求の復元と管理を提供 – UI ボタンに接続
restorePurchases()とmanageSubscriptions().
収益の次のステップ
収益の次のステップ購入フローの正常動作後、 Revenue Playbook を使用して、最初の有料チャネルを計画する: 製品範囲、ASO、価格設定、壁の配置、分析、脱落フィードバック。
トラブルシューティング
- 製品が読み込まれない
- バンドルID / アプリケーションIDがストアの設定と一致していることを確認してください。
- 製品IDが有効かつ承認済み (App Store) または有効化済み (Google Play) であることを確認してください。
製品を作成してから数時間待ってください。ストアのプロパゲーションは即時ではありません。
- 購入がキャンセルされたり、途中で止まったりする
try/catchそして表面に親切なエラーメッセージを表示します。 - Androidの場合、テストアカウントはPlay Store (内部トラック)からアプリをインストールして、Billingが正常に動作するようにしてください。
- デバイス上で実行している場合、Billingエラーを確認するにはlogcat/Xcodeを参照してください。
サブスクリプションの状態が不正です。
- を使用して
getPurchases()ストアデータとローカルエンタイトルメントキャッシュを比較するために - Androidの場合、常にGoogle Play Developer APIに
purchaseTokenを使用して、期限切れ日または払い戻しステータスを取得します。 - iOSの場合、を確認し、受け取りを検証して払い戻しまたは取り消しを検出します。
isActive/expirationDateGetting Startedから続けてください。
「Getting Startedから続けてください」というセクションのタイトル
itemsCapgoを使用している場合 はじめに 店舗承認と配布の計画に役立つため、 Using @capgo/native-purchases for the native capability in Using @capgo/native-purchases, @capgo/capacitor-in-app-review for the implementation detail in @capgo/capacitor-in-app-review, Using @capgo/capacitor-in-app-review for the native capability in Using @capgo/capacitor-in-app-review, @capgo/capacitor-native-market for the implementation detail in @capgo/capacitor-native-market, and Using @capgo/capacitor-native-market for the native capability in Using @capgo/capacitor-native-market.