Reputation: 2249
I need to query for pending purchases when my Android app launches so they can be processed by our server. I'm currently using BillingClient.queryPurchaseHistoryAsync() to do this and it's working fine.
However, queryPurchaseHistoryAsync has been deprecated and Google recommends using BillingClient.queryPurchasesAsync() instead. Unfortunately that call doesn't return purchases that are in the PENDING state, presumably because it's only using the local cache and PENDING state is only known by the Play Store server.
So my question is how do I retrieve all PENDING purchases now, without relying on a deprecated call?
Upvotes: 0
Views: 43
Reputation: 196
According to google Document Google Doc
val billingClient = BillingClient.newBuilder(context)
.setListener(purchasesUpdatedListener)
.enablePendingPurchases() // Add this line
.build()
Query Pending Purchases on App Launch
Using BillingClient.queryPurchasesAsync()
billingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP) { billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
for (purchase in purchases) {
if (purchase.purchaseState == Purchase.PurchaseState.PENDING) {
// Handle pending purchase
handlePendingPurchase(purchase)
}
}
}
}
Handle Pending Purchases in PurchasesUpdatedListener
Your PurchasesUpdatedListener
will notify you whenever a purchase is updated, including state changes like PENDING to PURCHASED.
val purchasesUpdatedListener = PurchasesUpdatedListener { billingResult, purchases ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && purchases != null) {
for (purchase in purchases) {
when (purchase.purchaseState) {
Purchase.PurchaseState.PENDING -> {
// Handle pending purchase
handlePendingPurchase(purchase)
}
Purchase.PurchaseState.PURCHASED -> {
// Handle completed purchase
if (!purchase.isAcknowledged) {
acknowledgePurchase(purchase)
}
}
Purchase.PurchaseState.UNSPECIFIED_STATE -> {
// Handle error or ignored state
}
}
}
} else if (billingResult.responseCode == BillingClient.BillingResponseCode.USER_CANCELED) {
// Handle user cancellation
} else {
// Handle other errors
}
}
Hope this help you
Upvotes: 0