在活跃且稳定的互联网网络上,购买过程没有任何障碍,但是当在不稳定且超慢的互联网网络上(可能断开、丢失网络或其他原因)进行购买时,有时用户会等不及直接关闭应用程序或通过其他方式取消购买。
我有两种类型的数字内容可供出售,第一种是一次性购买,第二种可以多次购买。
问题出在第二种,当购买成功但消费失败时,最终的结果是一次性购买,用户无法再次购买。
我想问的是如果ConsumeParams消费失败如何取消购买?这可能吗?
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
handlePurchases(list);
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
new Handler(Looper.getMainLooper()).post(this::CANCELED);
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
new Handler(Looper.getMainLooper()).post(() -> {
binding.PurchaseTxtMessage.setText(R.string.DPurchase_Canceled);
CANCELED();
});
} else {
new Handler(Looper.getMainLooper()).post(() -> {
binding.PurchaseTxtMessage.setText(R.string.DPurchase_Canceled);
CANCELED();
});
}
}
private void handlePurchases(List<Purchase> purchases) {
for(Purchase purchase:purchases) {
if (SKU.equals(purchase.getProducts().get(0)) && purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
new Handler(Looper.getMainLooper()).post(() -> {
binding.PurchaseTxtMessage.setText(R.string.DPurchase_Not_Valid);
CANCELED();
});
return;
}
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, billingResult -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
//CHECK PURCHASE TYPE
new Handler(Looper.getMainLooper()).post(() -> CHECK_PURCHASE_TYPE(purchase));
} else {
new Handler(Looper.getMainLooper()).post(this::CANCELED);
}
});
} else {
//CHECK PURCHASE TYPE
new Handler(Looper.getMainLooper()).post(() -> CHECK_PURCHASE_TYPE(purchase));
}
} else if(SKU.equals(purchase.getProducts().get(0)) && purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
new Handler(Looper.getMainLooper()).post(() -> {
binding.PurchaseTxtMessage.setText(R.string.DPurchase_Pending);
CANCELED();
});
} else if(SKU.equals(purchase.getProducts().get(0)) && purchase.getPurchaseState() == Purchase.PurchaseState.UNSPECIFIED_STATE) {
new Handler(Looper.getMainLooper()).post(() -> {
binding.PurchaseTxtMessage.setText(R.string.DPurchase_Unknown);
CANCELED();
});
} else {
new Handler(Looper.getMainLooper()).post(this::CANCELED);
}
}
}
private void CHECK_PURCHASE_TYPE(Purchase purchase) {
if (purchase.getProducts().get(0).equals("sku0")) {
ConsumeParams consumeParams = ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
billingClient.consumeAsync(consumeParams, (billingResult, s) -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
SAVE_PREF(purchase);
}
});
} else if (purchase.getProducts().get(0).equals("sku1")) {
SAVE_PREF(purchase);
} else if (purchase.getProducts().get(0).equals("sku2")) {
SAVE_PREF(purchase);
}
}
不,这是不可能的。但是您可以按照
here的建议在您的
BillingClient.queryPurchasesAsync()
方法中调用onResume()
,并尝试确认/使用未处理的内容。
除了消费第二份外,您还必须确认您是一次性购买的,否则 Google 将在 3 天内退款。