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

除了允许您的应用在 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-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

为此,您需要创建自己的 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 的新组件 Manifest:一项 Service,浏览器可连接到该 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 结算服务

本文介绍了使用可信网络的 Android 应用专门需要执行的步骤。 活动,但 Google Play Billing API 有自己的术语,并且包括客户端和后端 组件。我们强烈建议您阅读 Google Play 结算服务Digital Goods API 文档并理解其概念,然后再将其集成到 部署应用。