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

安德烈西普里亞尼班達拉
André Cipriani Bandarra

除了允許應用程式在 Play 商店銷售數位商品和訂閱項目,Google Play 帳款服務也提供多項工具,可用來管理目錄、價格和訂閱項目、實用報表,以及讓使用者熟悉的 Play 商店提供的結帳流程。此外,在 Play 商店上發布數位商品販售的應用程式也必須遵守這項規定。

Chrome 第 88 版將在 Android 上推出來源試用,支援將 Trusted Web ActivitiesPayment Request APIDigital Goods API 整合,透過 Google Play 帳款服務實作購買流程。我們預計這項來源試用也能適用於 ChromeOS 89 版本。

為簡化與 Android 應用程式的整合,Trusted Web Activity 團隊推出了 android-browser-helper 的擴充功能程式庫。本指南說明將這個程式庫整合至現有應用程式所需的變更。

注意:本文介紹 Android 應用程式的整合方式。如果你使用 Bubblewrap 建構應用程式,就能使用這項工具更新應用程式。這個問題會追蹤 Bubblewrap 的實作情形。本指南適用於「不」使用 Bubblewrap 更新應用程式的使用者。

build.gradle

結帳擴充功能程式庫本身取決於 android-browser-helper2.1.0 版。請確認您的應用程式使用的版本大於或等於該版本。

此外,您也需要為帳款服務擴充功能程式庫新增實作宣告:

dependencies {
    ...
    implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.1.0'
    implementation 'com.google.androidbrowserhelper:billing:1.0.0-alpha05'
}

DelegationService.java

android-browser-helper 隨附可供應用程式直接使用的預設 DelegationService。使用帳單擴充功能時,您需要稍微自訂 DelegationService 版本。

為此,您必須建立自己的 DelegationService 類別,用來擴充原始類別並覆寫 onCreate()。您必須在 onCreate() 中新增單一方法呼叫,將應用程式註冊為 Digital Goods API 的處理常式:

package com.example.yourapp;

import com.google.androidbrowserhelper.playbilling.digitalgoods.DigitalGoodsRequestHandler;
import com.google.androidbrowserhelper.trusted.DelegationService;

public class DelegationService
        extends com.google.androidbrowserhelper.trusted.DelegationService {
    @Override
    public void onCreate() {
        super.onCreate();
        registerExtraCommandHandler(new DigitalGoodsRequestHandler(getApplicationContext()));
    }
}

AndroidManifest.xml

在 Android 資訊清單中,您必須變更自己實作的委派程式庫參照。在對應的 service 宣告中,將 com.google.androidbrowserhelper.trusted.DelegationService 替換為新建立的類別。

<service
    android:name=".DelegationService"
    android:exported="true">

    <intent-filter>
        <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</service>

帳款服務程式庫也會導入兩個需要加進 Android 資訊清單的新元件:瀏覽器可連線的服務,並檢查應用程式是否支援付款流程,以及處理付款流程本身的活動

<activity
    android:name="com.google.androidbrowserhelper.playbilling.provider.PaymentActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:configChanges="keyboardHidden|keyboard|orientation|screenLayout|screenSize"
    android:exported="true">
    <intent-filter>
        <action android:name="org.chromium.intent.action.PAY" />
    </intent-filter>
    <meta-data
        android:name="org.chromium.default_payment_method_name"
        android:value="https://play.google.com/billing" />
</activity>
<!-- This service checks who calls it at runtime. -->
<service
    android:name="com.google.androidbrowserhelper.playbilling.provider.PaymentService"
    android:exported="true" >
    <intent-filter>
        <action android:name="org.chromium.intent.action.IS_READY_TO_PAY" />
    </intent-filter>
</service>

進一步瞭解 Digital Goods API 和 Google Play 帳款服務

本文特別介紹了使用「受信任網路活動的 Android」應用程式需要執行的步驟,但 Google Play Billing API 有專屬的術語,包括用戶端和後端元件。強烈建議您先閱讀「Google Play 帳款服務」和「Digital Goods API」說明文件瞭解相關概念,再將其整合至實際工作環境中的應用程式。