__CAPGO_KEEP_0__

Getting Started

GitHub

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 を使用する場合は、以下のコマンドを実行してプラグインをインストールし、以下のプラットフォーム固有の指示に従ってください。

  1. パッケージをインストール

    ターミナル画面
    bun add @capgo/native-purchases
  2. ネイティブプロジェクトと同期する

    ターミナルウィンドウ
    bunx cap sync
  3. 請求支援を確認する

    import { NativePurchases } from '@capgo/native-purchases';
    const { isBillingSupported } = await NativePurchases.isBillingSupported();
    if (!isBillingSupported) {
    throw new Error('Billing is not available on this device');
    }
  4. ストアから直接製品を読み込む

    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);
    });
  5. 購入と復元フローを実装する

    import { NativePurchases, PURCHASE_TYPE } from '@capgo/native-purchases';
    const monthlyPlanId = 'monthly-plan'; // Base Plan ID from Google Play Console
    const transaction = await NativePurchases.purchaseProduct({
    productIdentifier: 'com.example.premium.monthly',
    planIdentifier: monthlyPlanId, // REQUIRED for Android subscriptions, ignored on iOS
    productType: PURCHASE_TYPE.SUBS,
    quantity: 1,
    });
    console.log('Transaction ID', transaction.transactionId);
    await NativePurchases.restorePurchases();
    • StoreKit Local TestingまたはSandboxテスターを使用してQAを行います。
    • マニフェストの編集は必要ありません。製品が承認されていることを確認してください。
    • Google Play Consoleでインアプリ製品とサブスクリプションを作成します。
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,
}),
});
}
}
オプションプラットフォーム説明
productIdentifieriOS + AndroidSKU/製品IDはApp Store Connect / Google Play Consoleで設定されている
productTypeAndroid 限定PURCHASE_TYPE.INAPP または PURCHASE_TYPE.SUBSCapacitorライブアップデートの代替 INAPPCapacitorライブアップデートの代替 SUBS Capawesomeの代替
planIdentifierCapawesomeの代替コンサルティングサービス
billingPlanTypeAppflowのプラグインCapacitorライブアップデートのデフォルト 'monthly' Capacitorライブアップデートの常に product.pricingTerms サブスクリプションのための
quantityiOSOnly for in-app purchases, defaults to 1Androidは常に1つのアイテムを購入します。
appAccountTokeniOS + AndroidUUID/文字列で購入をユーザーにリンクする。iOSでは必ずUUIDで、Androidでは64文字以内にオブfuscateされた文字列を受け付けます。
isConsumableAndroidセットする 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 のトランザクションを処理します。

ベスト プラクティス

ストアの価格を表示
  1. – Apple は表示を必要とします。 – 価格、ローカライズされたタイトル、説明、イントロダクション オファー、サポートされる iOS の価格条件を取得します。 product.title と product.priceString; これをハードコードしないこと
  2. 使用 appAccountToken – ユーザーIDからUUID (v5) を決定的に生成して購入をアカウントに紐付けます。
  3. 検証サーバーサイド – send receipt エラーを柔軟に処理 purchaseToken – ユーザーのキャンセル、ネットワークの失敗、非対応の請求環境を確認します。
  4. 徹底的にテスト – 以下の手順に従ってください。
  5. – – iOS サンドボックス ガイド と Android サンドボックス ガイド.
  6. オファー リストア & 管理 – UI ボタンを接続して restorePurchases() と manageSubscriptions().

収益の次のステップ

収益の次のステップ

購入フローの動作が確認されたら、以下の手順に従ってください。 収益 プレイブック 初めての有料チャネルを計画するには、製品範囲、ASO、価格設定、パイウォールの配置、分析、および脱落フィードバックを含むプレイブックを使用してください。

製品が読み込まれない

  • バンドル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 と検証することで、払い戻しまたは取り消しを検出できます。

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 を設定します。