我正在尝试创建一个恢复购买系统。我希望,无论用户登录哪种设备,都可以访问其购买的产品。因此,我在应用启动时使用"queryPurchaseHistoryAsync()“方法。我的问题从这里开始。
在谷歌的新实现中,与文档相反,queryPurchaseHistoryAsync()参数发生了变化。现在它以PurchaseHistoryRecord对象的列表作为参数,而不是以购买对象的列表作为参数。
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());
}
}
}
Google发行说明(05-2019):
“为减少混淆,queryPurchaseHistoryAsync()现在返回PurchaseHistoryRecord对象而不是Purchase对象。PurchaseHistoryRecord对象与Purchase对象相同,不同之处在于它只反映queryPurchaseHistoryAsync()返回的值,并且不包含autoRenewing、orderId和packageName字段。请注意,返回的数据没有任何变化-queryPurchaseHistoryAsync()返回与以前相同的数据。”
但无论是发行说明还是文档都没有说明如何使用PurchaseHistoryRecord检查购买状态。
感谢您阅读这篇文章,感谢您的帮助。
发布于 2019-09-21 20:10:56
到目前为止,我一直在使用queryPurchases()自动恢复购买,因为它不需要任何网络连接。
Google play应用程序与帐户相关的缓存正在为所有设备更新。在许多情况下,您不需要调用queryPurchaseHistoryAsync call来进行恢复。
如@bospehre评论所述。它有缺点,因为它依赖于缓存。因此,我们仍然需要检查购买情况,并通过网络呼叫恢复它们。
对于queryPurchaseHistory异步调用,我们可以获取购买sku和token。如果你像谷歌推荐的那样使用服务器来保存订阅数据。您可以通过您的服务器检查此订阅的情况。
下面是一个恢复用户最新订阅的示例。
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")
}
}
https://stackoverflow.com/questions/58038764
复制相似问题