10

我正在尝试创建一个恢复购买系统。我想要,用户可以在他/她登录的任何设备上访问其购买的产品。所以我在应用程序启动时使用“queryPurchaseHistoryAsync()”方法。我的问题从这里开始。

随着谷歌的新实现,与文档相反,queryPurchaseHistoryAsync() 参数发生了变化。现在它将 PurchaseHistoryRecord 对象列表作为参数,而不是 Purchase 对象列表。

Android studio 无法解决文档中所述的方法。使用新的 queryPurchaseHistoryAsync() 我无论如何都找不到检查购买状态。(如果它已购买、取消或待处理)。我可以使用“purchase.getPurchaseState()”方法来处理购买对象。

queryPurchaseHistoryAsync() 的文档

billingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
                                         new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(BillingResult billingResult,
                                          List<Purchase> purchasesList) {
        if (billingResult.getResponseCode() == BillingResponse.OK
                && purchasesList != null) {
            for (Purchase purchase : purchasesList) {
                // Process the result.
            }
         }
    }
});

我的实现

implementation 'com.android.billingclient:billing:2.0.3'

我的应用程序中的 queryPurchaseHistoryAsync() 方法

billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.INAPP,
                new PurchaseHistoryResponseListener() {
                    @Override
                    public void onPurchaseHistoryResponse(BillingResult billingResult, List<PurchaseHistoryRecord> purchaseHistoryRecordList) {

                        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                                && purchaseHistoryRecordList != null) {

                            for (PurchaseHistoryRecord purchaseHistoryRecord : purchaseHistoryRecordList) {

                                HandleOldGetting(purchaseHistoryRecord.getSku());
                             }
                        }
                    }

谷歌发布说明(05-2019):

“为了最大程度地减少混淆,queryPurchaseHistoryAsync() 现在返回 PurchaseHistoryRecord 对象而不是 Purchase 对象。PurchaseHistoryRecord 对象与 Purchase 对象相同,只是它仅反映 queryPurchaseHistoryAsync() 返回的值并且不包含 autoRenewing、orderId , 和 packageName 字段。请注意,返回的数据没有任何变化——queryPurchaseHistoryAsync() 返回的数据与以前相同。”

但发行说明和文档都没有说明如何使用 PurchaseHistoryRecord 检查购买状态。

感谢您阅读本文,感谢您的帮助。

4

1 回答 1

3

到目前为止,我一直在使用 queryPurchases() 来自动恢复购买,因为它不需要任何网络。

与帐户相关的 Google Play 应用程序缓存正在为所有设备更新。在许多情况下,您不需要调用 queryPurchaseHistoryAsync 来进行恢复。

正如@bospehre 评论中所述。它有缺点,因为它取决于缓存。所以我们仍然需要检查购买情况并通过网络调用来恢复它们。

对于 queryPurchaseHistory 异步调用,我们可以获得购买 sku 和 token。如果您按照 Google 的建议使用服务器来保存订阅数据。您可以通过您的服务器查看此订阅的情况。

以下是恢复用户最新订阅的示例。

billingManager.billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult, purchaseHistoryRecords ->
      
           if (purchaseHistoryRecords != null) {
                var activePurchaseRecord : PurchaseHistoryRecord? = null
                if (purchaseHistoryRecords.size > 0) {
    
    // Get the latest subscription. It may differ for developer needs.
    
                    for (purchaseHistoryRecord in purchaseHistoryRecords) {
                        Log.d(billingLogs, "Purchase History Record : $purchaseHistoryRecord")
        
                        if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                            if (subSkuListHelper.getSkuList().contains(purchaseHistoryRecord.sku)
                            ) {
        
                                if (activePurchaseRecord == null) {
                                    activePurchaseRecord = purchaseHistoryRecord
                                } else {
                                    if (purchaseHistoryRecord.purchaseTime > activePurchaseRecord.purchaseTime) {
                                        activePurchaseRecord = purchaseHistoryRecord
                                    }
                                }
        
                            }
                        }
        
                    }
                    
        
                        Toast.makeText(
                            this,
                            "Subscription Purchases found, Checking validity...",
                            Toast.LENGTH_SHORT
                        ).show()
        
        
        // Make a network call with sku and purchaseToken to get subscription info
        
        //Subscription Data Fetch is a class that handling the networking
                        activePurchaseRecord?.let { SubscriptionDataFetch(
                            this,
                            billingManager.billingClient
                        )
                            .executeNetWorkCall(
                                getString(R.string.ubscription_check_endpoint),
                                it.sku,
                                it.purchaseToken
                            )
                        }
                    
                }
                else {
                    Log.d(billingLogs, "Purchase History Record not found size 0") }
        
            }
            else {
                Toast.makeText(
                    this,
                    "Purchase not found",
                    Toast.LENGTH_SHORT
                ).show()
        
                Log.d(billingLogs, "Purchase History Record not found null")
            }
}
于 2019-09-21T12:10:55.747 回答