在受信任的網路活動中使用 Google Play 帳款服務

本指南適用於封裝為受信任的網路活動的網頁應用程式。如果是以 Kotlin 或 Java 編寫的 Android 應用程式,請參閱 Google Play 帳款服務說明文件。

在 Google Play 銷售數位商品或訂閱項目的應用程式,必須使用 Google Play 帳單系統。在受信任的網路活動中,您可以使用下列網頁 API 進行整合,這些 API 適用於 Android 和 ChromeOS 上的 Chrome 101 以上版本:

需求條件

如要設定 Google Play 帳款服務,您需要:

向使用者收取透過 Play 商店下載應用程式的費用。使用者仍可在網路上存取 PWA,因此付費下載並不會限制存取權。請改為透過應用程式內購或訂閱項目,販售功能或內容。

在 Android 專案中啟用 Play 帳款服務

如果您使用 Bubblewrap 建構應用程式,請在專案根目錄的 twa-manifest.json 中啟用 playBilling 功能:

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

接著,依序執行 bubblewrap update 和 bubblewrap build,然後將新的 Android 套件上傳至 Play 商店。

如果您在 Android Studio 中建構應用程式,請新增 Android Browser Helper Play Billing 程式庫。如需 DelegationService 和 AndroidManifest.xml 的必要變更,請參閱 twa-play-billing 試用版。

確認是否支援 Google Play 帳款服務

使用 Play 商店 ID 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 的標題、說明和價格,確保顯示的價格與 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 中建立 PaymentRequest,並將 https://play.google.com/billing 設為付款方式,產品 ID 設為 sku。Google Play 帳款服務會使用 Play 管理中心的價格,因此 total 為必要欄位,但會遭到忽略。

show() 解析後,請將購買權杖傳送至後端,驗證並確認購買交易。如果您未在三天內確認購買交易,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.
}

測試整合功能

如要在發布至 Play 商店前,先在 Android 裝置上測試,請按照下列步驟操作:

  1. 使用 Android 9 以上版本,並啟用開發人員選項。
  2. 在 Chrome 中前往 chrome://flags,並啟用 #enable-debug-for-store-billing。
  3. 透過 HTTPS 提供網站。Digital Goods API 無法透過 HTTP 使用。

如果應用程式是從 Play 商店安裝,則不需要這個旗標。如要使用測試帳戶和測試產品進行測試,請參閱「測試與 Google Play Billing Library 的整合」。

後續步驟