本指南適用於封裝為受信任的網路活動的網頁應用程式。如果是以 Kotlin 或 Java 編寫的 Android 應用程式,請參閱 Google Play 帳款服務說明文件。
在 Google Play 銷售數位商品或訂閱項目的應用程式,必須使用 Google Play 帳單系統。在受信任的網路活動中,您可以使用下列網頁 API 進行整合,這些 API 適用於 Android 和 ChromeOS 上的 Chrome 101 以上版本:
- 數位商品 API:擷取產品詳細資料和使用者的購買交易。
- Payment Request API:啟動購買流程。
需求條件
如要設定 Google Play 帳款服務,您需要:
- Google Play 開發人員帳戶和 Google Payments 商家帳戶,且兩者已連結。
- Play 商店資訊,且在公開、封閉測試或內部測試群組中發布。
- 在 Play 管理中心設定的應用程式產品和訂閱項目。
- 具備有效 Digital Asset Links 設定的受信任的網路活動專案。
向使用者收取透過 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 裝置上測試,請按照下列步驟操作:
- 使用 Android 9 以上版本,並啟用開發人員選項。
- 在 Chrome 中前往
chrome://flags,並啟用#enable-debug-for-store-billing。 - 透過 HTTPS 提供網站。Digital Goods API 無法透過 HTTP 使用。
如果應用程式是從 Play 商店安裝,則不需要這個旗標。如要使用測試帳戶和測試產品進行測試,請參閱「測試與 Google Play Billing Library 的整合」。
後續步驟
- 使用 Google Play Developer API 用戶端程式庫,在後端驗證購買交易。
- 瞭解如何銷售訂閱項目。
- 設定即時開發人員通知,追蹤訂閱項目異動。