美文网首页
Android集成Google Pay流程以及注意事项

Android集成Google Pay流程以及注意事项

作者: TimberBug | 来源:发表于2021-10-21 18:40 被阅读0次

一、前言

最近在项目开发中,需要集成Google Play支付,也是第一次集成Google的支付功能,在集成过程中也下了一番功夫,故在此记录分享一下,希望能给大家提供一些帮助。

二、准备工作

1.注册Google账号 点此了解注册流程
2.在Google play管理中心创建和配置商品
点此创建和配置一次性商品
点此创建和配置订阅

三、开始集成并实现Google支付

首先附上Google支付官方集成文档

在App module的build.gradle中添加Google支付依赖

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
  ...
  //Google支付
  implementation 'com.android.billingclient:billing:4.0.0'
}

1.初始化BillingClient

BillingClient 为许多常见的结算操作提供了方便的方法,既有同步方法,又有异步方法。强烈建议一次打开一个活跃的 BillingClient 连接,以避免对某一个事件进行多次 PurchasesUpdatedListener 回调

//处理购买监听,后续购买完成后在该监听中处理购买结果
private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
        // To be implemented in a later section.
    }
};

private BillingClient billingClient = BillingClient.newBuilder(activity)
    .setListener(purchasesUpdatedListener)
    .enablePendingPurchases()
    .build();

2.与Google play建立连接

如需连接到 Google Play,调用 startConnection()。连接过程是异步进行的,因此必须实现 BillingClientStateListener,以便在客户端的设置完成后且它准备好发出进一步的请求时接收回调。

此外,还必须实现重试逻辑,以处理与 Google Play 失去连接的问题。如需实现重试逻辑,请替换 onBillingServiceDisconnected() 回调方法,并确保 BillingClient 先调用 startConnection() 方法以重新连接到 Google Play,然后再发出进一步的请求。

billingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(BillingResult billingResult) {
        if (billingResult.getResponseCode() ==  BillingResponseCode.OK) {
            // The BillingClient is ready. You can query purchases here.
        }
    }
    @Override
    public void onBillingServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
        billingClient.startConnection(this);
    }
});

3.根据商品id查询可购买的商品

与 Google Play 建立连接后,向 Google Play 查询应用内商品详情,请调用 querySkuDetailsAsync()

调用 querySkuDetailsAsync() 时,应传递 SkuDetailsParams 的实例,用于指定在 Google Play 管理中心创建的商品 ID 字符串的列表以及 SkuTypeSkuType 可以是 SkuType.INAPP(针对一次性商品),也可以是 SkuType.SUBS(针对订阅)。

须指定实现 SkuDetailsResponseListener 接口的监听器。然后,您可以替换 onSkuDetailsResponse(),该方法会在查询完成时通知监听。

List<String> skuList = new ArrayList<> ();
skuList.add("替换为你的商品id");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(SkuType.INAPP);
billingClient.querySkuDetailsAsync(params.build(),
    new SkuDetailsResponseListener() {
        @Override
        public void onSkuDetailsResponse(BillingResult billingResult,
                List<SkuDetails> skuDetailsList) {
            // Process the result.
        }
    });

4.启动购买流程

如需从应用发起购买请求,请从应用的主线程调用 launchBillingFlow() 方法。

// An activity reference from which the billing flow will be launched.
Activity activity = ...;

// Retrieve a value for "skuDetails" by calling querySkuDetailsAsync().
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
        .setSkuDetails(skuDetails)
        .build();
int responseCode = billingClient.launchBillingFlow(activity, billingFlowParams).getResponseCode();

// Handle the result.

launchBillingFlow() 方法会返回 BillingClient.BillingResponseCode 中列出的几个响应代码之一。BillingResponseCode 为 OK 表示成功启动。

启动成功后Google Play 会调用 onPurchasesUpdated(),以将购买操作的结果传送给实现 PurchasesUpdatedListener 接口的监听器。实现如下:

@Override
void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
    if (billingResult.getResponseCode() == BillingResponseCode.OK
        && purchases != null) {
        for (Purchase purchase : purchases) {
            handlePurchase(purchase);
        }
    } else if (billingResult.getResponseCode() == BillingResponseCode.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}

5.处理购买交易

完成购买后,需要处理该购买交易。应按以下方式处理购买交易:

1.验证购买交易。
2.向用户提供内容,并确认内容已传送给用户。还可以选择性地将商品标记为已消费,以便用户可以再次购买商品。

验证购买交易,请先检查购买交易的状态是否为 PURCHASED。如果购买交易的状态为 PENDING,则您应按照处理待处理的交易中的说明处理购买交易。

对于消耗型商品,请调用 consumeAsync() 并添加 Google Play 应在用户重新购买时提供的购买令牌。示例如下:

void handlePurchase(Purchase purchase) {
    // Purchase retrieved from BillingClient#queryPurchasesAsync or your PurchasesUpdatedListener.
    Purchase purchase = ...;

    // Verify the purchase.
    // Ensure entitlement was not already granted for this purchaseToken.
    // Grant entitlement to the user.

    ConsumeParams consumeParams =
        ConsumeParams.newBuilder()
            .setPurchaseToken(purchase.getPurchaseToken())
            .build();

    ConsumeResponseListener listener = new ConsumeResponseListener() {
        @Override
        public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
            if (billingResult.getResponseCode() == BillingResponseCode.OK) {
                // Handle the success of the consume operation.
            }
        }
    };

    billingClient.consumeAsync(consumeParams, listener);
}

对于消耗型商品,请使用结算库中的 BillingClient.acknowledgePurchase() 或 Google Play Developer API 中的 Product.Purchases.Acknowledge。在确认购买交易之前,您的应用应使用 Google Play 结算库中的 isAcknowledged() 方法或 Google Play Developer API 中的 acknowledgementState 字段检查该购买交易是否已经过确认。示例如下:

BillingClient client = ...
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = ...

void handlePurchase(Purchase purchase) {
    if (purchase.getPurchaseState() == PurchaseState.PURCHASED) {
        if (!purchase.isAcknowledged()) {
            AcknowledgePurchaseParams acknowledgePurchaseParams =
                AcknowledgePurchaseParams.newBuilder()
                    .setPurchaseToken(purchase.getPurchaseToken())
                    .build();
            client.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
        }
    }
}

6.处理完购买交易后,还需调用自己服务端验证接口,验证购买是否真的成功(此步骤非常重要,处理交易中返回的状态不能作为最终处理结果,需再调用服务端接口验证购买结果

相关文章

网友评论

      本文标题:Android集成Google Pay流程以及注意事项

      本文链接:https://www.haomeiwen.com/subject/zxejpktx.html