スタートガイド
インストール手順と、このプラグインの完全なMarkdownガイドを含むセットアッププロンプトをコピーします。
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アシストによるセットアップでプラグインをインストールできます。以下のコマンドを使用して、お使いの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, // Android base plan id, required unless offerToken is passed; ignored on iOSproductType: PURCHASE_TYPE.SUBS,quantity: 1,});console.log('Transaction ID', transaction.transactionId);await NativePurchases.restorePurchases();- App Store ConnectでApp内課金とサブスクリプションを作成します。
- QAにはStoreKit Local TestingまたはSandboxテスターを使用してください。
- マニフェストの編集は不要です。商品が承認されていることを確認してください。
- Google Play Console でアプリ内商品とサブスクリプションを作成します。
- 少なくとも内部テストビルドをアップロードし、ライセンステストユーザーを追加します。
- に billing 権限を追加します
AndroidManifest.xml:
<uses-permission android:name="com.android.vending.BILLING" /> - App Store ConnectでApp内課金とサブスクリプションを作成します。
購入サービスの例
「Purchase service example」セクション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, // Android base plan id, required unless offerToken is passed 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, }), }); }}必要な購入オプション
「Required purchase options」セクション| オプション | プラットフォーム | 説明 |
|---|---|---|
productIdentifier | iOS + Android | App Store Connect または Google Play Console で設定した SKU/Product ID。 |
productType | Android のみ | PURCHASE_TYPE.INAPP または PURCHASE_TYPE.SUBS. デフォルトは INAPP. 常に設定: SUBS サブスクリプション用。 |
planIdentifier | Android のサブスクリプション | Google Play Console の Base Plan ID (Product.identifier サブスクリプションのものではなく Product.planIdentifier)。Android のサブスクリプションでは必須ですが、 offerToken が指定されている場合です。iOS およびアプリ内購入では無視されます。 |
offerToken | Androidでは | 同じ getProducts() / getProduct() 呼び出しで購入する、特定のサブスクリプションまたは一回きりのオファー用のトークンです。サブスクリプションの場合、 planIdentifierよりも優先されます。現在のオファーに一致しないトークンは拒否されます。 |
billingPlanType | iOS のサブスクリプション | 購入する StoreKit の billing plan。使用: 'monthly' がそのオプションを公開している場合、12ヶ月のコミットメント付きの月額課金に使用します。 product.pricingTerms そのオプションを公開します。 |
quantity | iOSと | アプリ内購入のみで、デフォルトは 1. Androidでは常に1つのアイテムを購入します。 |
appAccountToken | iOS + Android | 購入をユーザーにリンクするためのUUID/文字列。iOSではUUIDが必須です。Androidでは64文字までの任意の難読化文字列を受け入れます。 |
isConsumable | Androidでは | に設定します true に設定すると、consumablesの権限付与後にトークンを自動消費します。デフォルトは false. |
Entitlementステータスの確認
「権限ステータスの確認」セクション使用する 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 価格条件を取得します。purchaseProduct()– StoreKit 2 または Billing クライアントの購入フローを開始します(iOS の月額コミットメント課金プランを含む)。restorePurchases()– 過去の購入をリプレイし、現在のデバイスに同期します。getPurchases()– すべての iOS トランザクションまたは Play Billing の購入をリスト表示します。manageSubscriptions()– ネイティブのサブスクリプション管理 UI を開きます。addListener('transactionUpdated')– アプリ起動時に保留中の StoreKit 2 トランザクションを処理します(iOS のみ)。
ベストプラクティス
セクション「ベストプラクティス」- ストア価格を表示 – Apple は表示を義務付けています
product.titleとproduct.priceString; ハードコードしないでください。 - 使用する
appAccountToken– ユーザー ID から UUID (v5) を決定論的に生成し、購入をアカウントに紐付けます。 - サーバーサイドで検証する –
receipt(iOS) /purchaseToken(Android) をバックエンドに送信して検証します。 - エラーを適切に処理する – ユーザーのキャンセル、ネットワーク障害、サポートされていない課金環境を確認します。
- 徹底的なテスト – iOS サンドボックスガイド と Android サンドボックスガイド.
- オファーの復元 & 管理 – に接続された UI ボタンを追加
restorePurchases()とmanageSubscriptions().
破壊的変更(TypeScript 型定義)
セクション「Breaking (TypeScript typings)」Product.subscriptionPeriod は任意になりました。Android では、価格のみを持つ前払い(非定期)オファーでは省略されます。読み取り product.subscriptionPeriod.unit チェックなしでは TypeScript エラーになります。使用 product.subscriptionPeriod?.unit。詳細は Androidのサブスクリプションオファー.
Revenueの次のステップ
セクション「収益化の次のステップ」購入フローが動作したら、 収益化プレイブック を使って最初の有料ファネルを計画してください:プロダクトのスコープ、ASO、価格設定、ペイウォールの配置、アナリティクス、離脱フィードバック。
トラブルシューティング
「トラブルシューティング」セクションプロダクトが読み込まれない
- バンドルID / アプリケーションIDがストアの設定と一致していることを確認してください。
- プロダクトIDが有効かつ承認済み(App Store)またはアクティブ化済み(Google Play)であることを確認してください。
- プロダクト作成後、数時間お待ちください。ストアへの反映には時間がかかります。
購入がキャンセルされた、または停止している
- ユーザーはフロー途中でキャンセルできます。呼び出しを
try/catchし、ユーザーフレンドリーなエラーメッセージを表示します。 - Android の場合、Billing が正常に動作するように、テストアカウントには Play Store(内部トラック)からアプリをインストールしてください。
- デバイス上で実行する際は、logcat/Xcode で Billing エラーを確認してください。
サブスクリプション状態が正しくない
- 使用する
getPurchases()ストアデータとローカルのエンタイトルメントキャッシュを比較するために使用します。 - Android では、常に Google Play Developer API を以下でクエリしてください
purchaseToken有効期限や返金ステータスを取得するために使用します。 - iOS では、
isActive/expirationDateを確認し、レシートを検証して返金や取り消しを検出してください。
Getting Startedの続きを進める
「Getting Started からの続き」セクション使用している場合 スタートガイド ストアの承認と配信を計画するには、これを次と連携させてください: @capgo/native-purchases を使用する 「Using @capgo/native-purchases」のネイティブ機能については、 @capgo/capacitor-in-app-review @capgo/capacitor-in-app-review での実装の詳細については、 @capgo/capacitor-in-app-review の使い方 「@capgo/capacitor-in-app-review の使い方」のネイティブ機能については、 @capgo/capacitor-native-market @capgo/capacitor-native-market の実装の詳細については、 @capgo/capacitor-native-market の使用 「@capgo/capacitor-native-market. の使用」のネイティブ機能については