Thursday 24 November 2022

How can I handle the code purchase.sku when I upgrade Google Play Billing Library 3 to 4?

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())
    }


from How can I handle the code purchase.sku when I upgrade Google Play Billing Library 3 to 4?

No comments:

Post a Comment