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

Google Play 請求サービスでは、アプリで Google Play ストアでデジタル商品や定期購入を販売できるだけでなく、カタログ、料金、定期購入を管理するためのツール、便利なレポート、ユーザーに使い慣れた Google Play ストアによる精算フローが提供されます。また、デジタル商品を販売する Google Play ストアで公開されるアプリにも必須です。

Chrome 88 では、Android でオリジン トライアルが開始されます。このトライアルでは、Trusted Web ActivitiesPayment Request APIDigital Goods API を統合して、Google Play 請求サービス経由の購入フローを実装できます。このオリジン トライアルは、ChromeOS バージョン 89 でも利用可能になる予定です。

Android アプリへの統合を容易にするため、信頼できるウェブ アクティビティ チームは 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 のハンドラとしてアプリケーションを登録するメソッド呼び出しを 1 つ追加する必要があります。

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 マニフェストに追加する必要がある 2 つの新しいコンポーネントが導入されています。ブラウザが接続して、アプリケーションがお支払いをサポートしているかどうかを確認できるサービスと、お支払いフローを処理するアクティビティです。

<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 請求サービス API には独自の用語があり、クライアント コンポーネントとバックエンド コンポーネントが含まれています。本番環境のアプリに統合する前に、Google Play 請求サービスDigital Goods API のドキュメントを読み、コンセプトを理解することを強くおすすめします。