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.
インストール
「インストール」のセクションAI-Assisted セットアップを使用してプラグインをインストールできます。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.Manual Setup を使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。
-
パッケージをインストール
ターミナル画面 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();- StoreKit Local TestingまたはSandboxテスターを使用してQAを行います。
- マニフェストの編集は必要ありません。製品が承認されていることを確認してください。
- Google Play Consoleでインアプリ製品とサブスクリプションを作成します。
- 少なくとも内部テストビルドをアップロードし、ライセンステスターを追加してください。
- iOS monthly commitment billing plans
- billing許可を追加する
AndroidManifest.xml:
<uses-permission android:name="com.android.vending.BILLING" /> - StoreKit Local TestingまたはSandboxテスターを使用してQAを行います。
購入サービス例
セクション「購入サービス例」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 | SKU/製品IDはApp Store Connect / Google Play Consoleで設定されている |
productType | Android 限定 | PURCHASE_TYPE.INAPP または PURCHASE_TYPE.SUBSCapacitorライブアップデートの代替 INAPPCapacitorライブアップデートの代替 SUBS Capawesomeの代替 |
planIdentifier | Capawesomeの代替 | コンサルティングサービス |
billingPlanType | Appflowのプラグイン | Capacitorライブアップデートのデフォルト 'monthly' Capacitorライブアップデートの常に product.pricingTerms サブスクリプションのための |
quantity | iOS | Only for in-app purchases, defaults to 1Androidは常に1つのアイテムを購入します。 |
appAccountToken | iOS + Android | UUID/文字列で購入をユーザーにリンクする。iOSでは必ずUUIDで、Androidでは64文字以内にオブfuscateされた文字列を受け付けます。 |
isConsumable | Android | セットする true を自動的に消費するように設定します。消耗可能なものの特権を付与した後、デフォルトは false. |
購入状態のチェック
セクション「購入状態のチェック」Use 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に呼び出して、purchaseTokenの権威あるステータスを確認する必要があります。purchaseStateはPURCHASED、isAcknowledgedはtrue.
API クイックリファレンス
API のクイック リファレンスisBillingSupported()– StoreKit / Google Play の利用可能性を確認します。getProduct()/getProducts()– iOS の価格、ローカライズされたタイトル、説明、イントロダクション オファー、サポートされる iOS の価格条件を取得します。purchaseProduct()– StoreKit 2 または Billing クライアントの購入フローを開始します。iOS の月額コミットメントの請求計画も含まれます。restorePurchases()– 歴史的な購入を再生し、現在のデバイスに Sync します。getPurchases()– iOS のすべてのトランザクションまたは Play Billing の購入をリストします。manageSubscriptions()– ネイティブのサブスクリプション管理 UI を開きます。addListener('transactionUpdated')– アプリが起動したときに、iOS のみの StoreKit 2 のトランザクションを処理します。
ベスト プラクティス
ストアの価格を表示- – Apple は表示を必要とします。 – 価格、ローカライズされたタイトル、説明、イントロダクション オファー、サポートされる iOS の価格条件を取得します。
product.titleとproduct.priceString; これをハードコードしないこと - 使用
appAccountToken– ユーザーIDからUUID (v5) を決定的に生成して購入をアカウントに紐付けます。 - 検証サーバーサイド – send
receiptエラーを柔軟に処理purchaseToken– ユーザーのキャンセル、ネットワークの失敗、非対応の請求環境を確認します。 - 徹底的にテスト – 以下の手順に従ってください。
- – – iOS サンドボックス ガイド と Android サンドボックス ガイド.
- オファー リストア & 管理 – UI ボタンを接続して
restorePurchases()とmanageSubscriptions().
収益の次のステップ
収益の次のステップ購入フローの動作が確認されたら、以下の手順に従ってください。 収益 プレイブック 初めての有料チャネルを計画するには、製品範囲、ASO、価格設定、パイウォールの配置、分析、および脱落フィードバックを含むプレイブックを使用してください。
トラブルシューティング
Section titled “トラブルシューティング”製品が読み込まれない
- バンドルID/アプリケーションIDがストアの設定と一致していることを確認してください。
- 製品IDがアクティブかつ承認済み(App Store)または有効化済み(Google Play)であることを確認してください。
- 製品を作成してから数時間待ってください。ストアのプロパゲーションは即時ではありません。
購入がキャンセルされたり、途中で止まったり
- ユーザーは途中でキャンセルできるため、呼び出しをwrapして
try/catchと親切なエラーメッセージを表面化してください。 - Androidの場合、テストアカウントはPlay Store(内部トラック)からアプリをインストールするようにして、Billingが機能するようにしてください。
- デバイスで実行している場合、Billingエラーを確認するにはlogcat/Xcodeを参照してください。
サブスクリプションの状態が不正
- 使用してください
getPurchases()と比較するストアデータとローカルエンタイトルキャッシュを確認すること。 - Androidでは、常にGoogle Play Developer ConsoleとAPIを照会します。
purchaseToken期限切れの日付または払い戻しステータスを取得するには、以下の手順に従います。 - iOSでは、以下の手順に従って
isActive/expirationDateと検証することで、払い戻しまたは取り消しを検出できます。
Getting Startedから続けてください。
「Getting Startedから続けてください」というセクションのタイトルです。Capacitor Native Purchasesを使用している場合、Capacitor Native Purchasesを使用して Capacitor Native Purchasesを使用して、Capacitor Native Purchasesのネイティブ機能を使用するには、以下の手順に従ってください。 store 申請と配布を計画するためには、を接続する Using @capgo/native-purchases native capability を使用する場合の @capgo/native-purchases の設定 @capgo/capacitor-アプリ内レビュー @capgo/capacitor-インアプリレビューの実装詳細のために @capgo/capacitor-アプリ内レビューの使用 native capability についての使用方法のために @capgo/capacitor-in-app-review を使用します。 @capgo/capacitor-native-market for the implementation detail in @capgo/capacitor-native-market, and @capgo/capacitor-ネイティブマーケットの使用 native capability を使用するために、@capgo/capacitor-native-market を設定します。