I use Google Play in-app in my app based the official sample project.
The Code A is to handle non-consumable products, it works well when I launch it using com.android.billingclient:billing-ktx:3.0.3
.
After I upgrade the project from Google Play Billing Library 3 to 4, I find the code purchase.sku
doesn't work, so I have to replace it with purchase.skus
.
The code of purchase.skus
can be compiled in com.android.billingclient:billing-ktx:4.0.0
, but I can't get the correct order, the test purchase is refunded after 3 minutes, it seems that Google Play doesn't acknowledge the purchase.
How can I fix the Code A when I upgrade Google Play Billing Library 3 to 4 ?
Code A
private fun processPurchases(purchasesResult: Set<Purchase>) {
val validPurchases = HashSet<Purchase>(purchasesResult.size)
purchasesResult.forEach { purchase ->
if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
if (purchase.sku.equals(purchaseItem)) {
//if (purchase.skus.equals(purchaseItem)) { //sku -> skus in 4.0
if (isSignatureValid(purchase)) {
validPurchases.add(purchase)
}
}
} else if (purchase.purchaseState == Purchase.PurchaseState.PENDING) {
Log.d(LOG_TAG, "Received a pending purchase of SKU: ${purchase.sku}")
// handle pending purchases, e.g. confirm with users about the pending
// purchases, prompt them to complete it, etc.
mContext.toast(R.string.msgOrderPending)
} else {
mContext.toast(R.string.msgOrderError)
}
}
acknowledgeNonConsumablePurchasesAsync(validPurchases.toList())
}
I'm not sure what is the exact reason of changing this method but I think that's probably because of the new subscriptions model in Google Play. Moreover this new method gets deprecated in the 5.0.0 version.
However, since the purchase.skus
became a List of Strings, you could just check for your purchaseItem
inside it. I think it depends on your purchases setup. Assuming your purchaseItem
is also a String
, you could either find your purchaseItem
there:
if (purchase.skus.any { it == purchaseItem })
or simply take the first one to compare:
if (purchase.skus[0] == purchaseItem)
Of course, you should debug it to check what's exactly inside the skus
list and then choose the best way to fix that.