在您的 Trusted Web Activity 中使用 Play 结算服务

安德烈·奇普里亚尼·班达拉
André Cipriani Bandarra

除了允许您的应用在 Play 商店中销售数字商品和订阅之外,Google Play 结算服务还提供用于管理目录、价格和订阅的工具、实用的报告,以及由用户熟悉的 Play 商店提供支持的结账流程。对于在 Play 商店中发布且销售数字商品的应用,也必须满足此要求。

Chrome 88 即将在 Android 上推出源试用,支持将 Trusted Web ActivityPayment 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 清单中的新组件:一个是浏览器可以连接到的 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 文档并了解其概念,然后再将其集成到生产环境的应用中。