Google Play Billing Library 7
This is a pretty old question, but I haven't found an answer anywhere. Anyway, I think I found the solution myself.
The trick is to call billingClient.queryPurchaseHistoryAsync(...) even though it is deprecated, and then call acknowledgePurchase(...) with the purchaseToken from history.
If the purchase is refunded/canceled it will return ITEM_NOT_OWNED. If it is valid it will return OK and acknowledge. From then on, it will appear in queryPurchasesAsync as a regular purchase on that device.
private void checkPurchase() {
billingClient.queryPurchasesAsync(
QueryPurchasesParams.newBuilder().setProductType(
BillingClient.ProductType.INAPP).build(),
(billingResult, purchases) -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
for (Purchase purchase : purchases) {
String product = purchase.getProducts().get(0); // I have only one product
if (product != null && product.equals("myProduct") &&
purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// do whatever you do when the item is purchased
Log.d(TAG, "Found myProduct purchase: " + purchase.getOrderId());
return;
}
}
checkPurchaseHistory();
}
});
}
private void checkPurchaseHistory() {
billingClient.queryPurchaseHistoryAsync(
QueryPurchaseHistoryParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(),
(billingResult, purchases) -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
if (purchases != null && !purchases.isEmpty()) {
for (PurchaseHistoryRecord purchase : purchases) {
String product = purchase.getProducts().get(0); // I have only one product
if (product != null && product.equals("myProduct")) {
Log.d(TAG, "Found purchase history: " + purchase.getPurchaseToken());
// acknowledge will return OK if purchased on another device
// or ITEM_NOT_OWNED if purchase is refunded
acknowledgePurchaseHistory(purchase.getPurchaseToken());
return;
}
}
}
// do whatever you do if the item is not purchased
}
});
}
private void acknowledgePurchaseHistory(String purchaseToken) {
billingClient.acknowledgePurchase(AcknowledgePurchaseParams
.newBuilder()
.setPurchaseToken(purchaseToken)
.build(), billingResult -> {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_NOT_OWNED) {
// do whatever you do if the item is not purchased
Log.d(TAG, "Purchase acknowledge from history: ITEM_NOT_OWNED");
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
// do whatever you do when the item is purchased
Log.d(TAG, "Purchase acknowledged from history");
}
});
}
If there is more than one product, the code can easily be adjusted.