メインコンテンツへスキップ

スタートガイド

GitHub

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.

手動セットアップを希望する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:

  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, // Android base plan id, required unless offerToken is passed; ignored on iOS
    productType: PURCHASE_TYPE.SUBS,
    quantity: 1,
    });
    console.log('Transaction ID', transaction.transactionId);
    await NativePurchases.restorePurchases();
    • App Store ConnectでApp内課金とサブスクリプションを作成します。
    • QAにはStoreKit Local TestingまたはSandboxテスターを使用してください。
    • マニフェストの編集は不要です。商品が承認されていることを確認してください。
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,
}),
});
}
}
オプションプラットフォーム説明
productIdentifieriOS + AndroidApp Store Connect または Google Play Console で設定した SKU/Product ID。
productTypeAndroid のみPURCHASE_TYPE.INAPP または PURCHASE_TYPE.SUBS. デフォルトは INAPP. 常に設定: SUBS サブスクリプション用。
planIdentifierAndroid のサブスクリプションGoogle Play Console の Base Plan ID (Product.identifier サブスクリプションのものではなく Product.planIdentifier)。Android のサブスクリプションでは必須ですが、 offerToken が指定されている場合です。iOS およびアプリ内購入では無視されます。
offerTokenAndroidでは同じ getProducts() / getProduct() 呼び出しで購入する、特定のサブスクリプションまたは一回きりのオファー用のトークンです。サブスクリプションの場合、 planIdentifierよりも優先されます。現在のオファーに一致しないトークンは拒否されます。
billingPlanTypeiOS のサブスクリプション購入する StoreKit の billing plan。使用: 'monthly' がそのオプションを公開している場合、12ヶ月のコミットメント付きの月額課金に使用します。 product.pricingTerms そのオプションを公開します。
quantityiOSとアプリ内購入のみで、デフォルトは 1. Androidでは常に1つのアイテムを購入します。
appAccountTokeniOS + Android購入をユーザーにリンクするためのUUID/文字列。iOSではUUIDが必須です。Androidでは64文字までの任意の難読化文字列を受け入れます。
isConsumableAndroidではに設定します 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.
  • isBillingSupported() – StoreKit / Google Play の利用可能性を確認します。
  • getProduct() / getProducts() – 価格、ローカライズされたタイトル、説明、イントロダクションオファー、およびサポートされている iOS 価格条件を取得します。
  • purchaseProduct() – StoreKit 2 または Billing クライアントの購入フローを開始します(iOS の月額コミットメント課金プランを含む)。
  • restorePurchases() – 過去の購入をリプレイし、現在のデバイスに同期します。
  • getPurchases() – すべての iOS トランザクションまたは Play Billing の購入をリスト表示します。
  • manageSubscriptions() – ネイティブのサブスクリプション管理 UI を開きます。
  • addListener('transactionUpdated') – アプリ起動時に保留中の StoreKit 2 トランザクションを処理します(iOS のみ)。
  1. ストア価格を表示 – Apple は表示を義務付けています product.title と product.priceString; ハードコードしないでください。
  2. 使用する appAccountToken – ユーザー ID から UUID (v5) を決定論的に生成し、購入をアカウントに紐付けます。
  3. サーバーサイドで検証する – receipt (iOS) / purchaseToken (Android) をバックエンドに送信して検証します。
  4. エラーを適切に処理する – ユーザーのキャンセル、ネットワーク障害、サポートされていない課金環境を確認します。
  5. 徹底的なテスト – iOS サンドボックスガイド と Android サンドボックスガイド.
  6. オファーの復元 & 管理 – に接続された UI ボタンを追加 restorePurchases() と manageSubscriptions().

破壊的変更(TypeScript 型定義)

セクション「Breaking (TypeScript typings)」

Product.subscriptionPeriod は任意になりました。Android では、価格のみを持つ前払い(非定期)オファーでは省略されます。読み取り product.subscriptionPeriod.unit チェックなしでは TypeScript エラーになります。使用 product.subscriptionPeriod?.unit。詳細は Androidのサブスクリプションオファー.

購入フローが動作したら、 収益化プレイブック を使って最初の有料ファネルを計画してください:プロダクトのスコープ、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. の使用」のネイティブ機能については