信頼できるウェブ アクティビティで Play 請求サービスを使用する

アンドレ・チプリリアニ・バンダラ
André Cipriani Bandarra

Google Play 請求サービスは、アプリが Play ストアでデジタル商品や定期購入を販売できるようにするだけでなく、カタログ、価格、定期購入を管理するためのツール、便利なレポート、Google Play ストアを利用した購入手続きフローも備えており、使い慣れた操作性を実現します。また、Google Play ストアで公開され、デジタル商品を販売するアプリも要件となります。

Chrome 88 では、Android のオリジン トライアルがリリースされます。Trusted Web ActivityPayment Request API および Digital Goods API と統合し、Google Play 請求サービスによる購入フローを実装できます。このオリジン トライアルは、ChromeOS バージョン 89 でもご利用いただけるようになる予定です。

Android アプリとの統合を容易にするために、Trusted Web Activity チームは、android-browser-helper に拡張機能ライブラリを導入しています。このガイドでは、このライブラリを既存のアプリに統合するために必要な変更について説明します。

注: この記事では、Android アプリの統合について説明します。アプリをビルドするために Bubblewrap を使用している場合は、このツールを使用してアプリを更新できます。Bubblewrap の実装については、この問題で追跡しています。このガイドは、Bubblewrap を使用してアプリを更新していない方を対象としています。

build.gradle

課金拡張機能ライブラリ自体は android-browser-helper のバージョン 2.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 を少しカスタマイズしたバージョンが必要です。

そのためには、元のクラスを拡張して onCreate() をオーバーライドする、独自の DelegationService クラスを作成する必要があります。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>

Billing Library には、Android マニフェストに追加する必要がある 2 つの新しいコンポーネントも導入されています。1 つは、ブラウザが接続してアプリが支払いをサポートしているかどうかを確認することができる Service と、支払いフローそのものを処理する Activity です。

<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 請求サービスの詳細

この記事では、特に Trusted Web Activity を使用する Android アプリで必要な手順を説明しましたが、Google Play Billing API には独自の用語が使用され、クライアント コンポーネントとバックエンド コンポーネントが含まれています。Google Play 請求サービスDigital Goods API のドキュメントをお読みになり、その概念を理解してから、本番環境のアプリに統合することを強くおすすめします。