Trusted Web Activity で Google Play 請求サービスを使用する

このガイドは、Trusted Web Activity としてパッケージ化されたウェブアプリを対象としています。Kotlin または Java で記述された Android アプリについては、Google Play 請求サービスのドキュメントをご覧ください。

Google Play でデジタル商品や定期購入を販売するアプリは、Google Play 請求サービスを使用する必要があります。Trusted Web Activity では、Android と ChromeOS の Chrome 101 以降で利用可能な次のウェブ API を使用して統合します。

要件

Google Play 請求サービスを設定するには、次のものが必要です。

Google Play ストアからアプリをダウンロードするユーザーに課金する。PWA はウェブでも利用できるため、有料ダウンロードによってアクセスが制限されることはありません。代わりに、アプリ内購入や定期購入で機能やコンテンツを販売します。

Android プロジェクトで Play 請求サービスを有効にする

Bubblewrap を使用してアプリをビルドする場合は、プロジェクトのルートにある twa-manifest.json で playBilling 機能を有効にします。

"features": {
  "playBilling": {
    "enabled": true
  }
}

次に、bubblewrap update、bubblewrap build の順に実行し、新しい Android パッケージを Google Play ストアにアップロードします。

Android Studio でアプリをビルドする場合は、Android Browser Helper Play Billing ライブラリを追加します。必要な DelegationService と AndroidManifest.xml の変更については、twa-play-billing のデモをご覧ください。

Google Play 請求サービスのサポートを確認する

Google Play ストアの識別子 https://play.google.com/billing を指定して getDigitalGoodsService() を呼び出します。メソッドがない場合や呼び出しが失敗した場合は、別の支払いフローを使用します。

if ('getDigitalGoodsService' in window) {
  try {
    const service =
        await window.getDigitalGoodsService('https://play.google.com/billing');
    // Google Play Billing is available.
  } catch (error) {
    // Google Play Billing isn't available. Use another payment flow.
  }
}

商品の詳細情報を表示する

getDetails() を使用して、商品 ID のタイトル、説明、価格を取得し、表示される価格が Google Play ストアと一致するようにします。

const details = await service.getDetails(['shiny_sword', 'gem']);
for (const item of details) {
  const localizedPrice = new Intl.NumberFormat(
      navigator.language,
      {style: 'currency', currency: item.price.currency}
  ).format(item.price.value);

  renderProductDetails(
      item.itemId, item.title, localizedPrice, item.description);
}

Digital Goods API でデジタル商品 ID を一覧表示できません。バックエンドからクエリするには、Google Play Developer API を使用します。

購入する

data で、お支払い方法として https://play.google.com/billing、商品 ID として sku を指定して PaymentRequest を作成します。Google Play 請求サービスは Google Play Console の価格を使用するため、total は必須ですが無視されます。

show() が解決したら、購入トークンをバックエンドに送信して、購入を確認して承認します。3 日以内に購入を承認しない場合、Google Play はユーザーに払い戻しを行い、購入を取り消します。

async function makePurchase(service, sku) {
  const paymentMethods = [{
    supportedMethods: 'https://play.google.com/billing',
    data: {sku},
  }];
  // Required by the Payment Request API, but ignored by Google Play Billing.
  const paymentDetails = {
    total: {label: 'Total', amount: {currency: 'USD', value: '0'}},
  };

  const request = new PaymentRequest(paymentMethods, paymentDetails);
  try {
    const response = await request.show();
    const {purchaseToken} = response.details;

    if (await acknowledgePurchaseOnBackend(purchaseToken, sku)) {
      // Optional: consume the purchase so the user can buy the item again.
      await service.consume(purchaseToken);
      await response.complete('success');
    } else {
      await response.complete('fail');
    }
  } catch (error) {
    // An AbortError usually means the user canceled the purchase.
  }
}

お客様の購入を確認する

listPurchases() を使用して、有効な定期購入、未消費の購入、利用済みのプロモーション コードなど、ユーザーが所有するアイテムを検索します。バックエンドで各購入を確認し、承認していない購入を承認します。

const purchases = await service.listPurchases();
for (const p of purchases) {
  await verifyOrAcknowledgePurchaseOnBackend(p.purchaseToken, p.itemId);
  // Update the UI with items the user is entitled to.
}

統合をテストする

Google Play ストアに公開する前に Android デバイスでテストする手順は次のとおりです。

  1. Android 9 以降を使用し、開発者向けオプションを有効にします。
  2. Chrome で chrome://flags に移動し、#enable-debug-for-store-billing を有効にします。
  3. HTTPS 経由でサイトを配信します。Digital Goods API は HTTP 経由では利用できません。

アプリが Google Play ストアからインストールされている場合は、このフラグは必要ありません。テスト アカウントとテスト アイテムを使用してテストするには、Google Play Billing Library 統合をテストするをご覧ください。

次のステップ