android-billing : BillingClient.acknowledgePurchase() twice in short time get DEVELOPER_ERROR

Viewed 1129

I refer to google sample BillingRepository.kt to implement my BillingRepository; As Google advises that developer should call BillingClient.queryPurchases() at onResume() callback;

So when I purchase an item successfully, I got the following code path:

  • onPurchasesUpdated() --> first acknowledgePurchase()
  • BillingRepository.queryPurchasesAsync() ---> second acknowledgePurchase()
    • The play store purchase flow Activity finished and MyActivity get onResume() callback to invoke BillingRepository.queryPurchasesAsync()

Then I found the wired things:

one of the two acknowledgePurchase() will get a DEVELOPER_ERROR(respond code 5), and the debug message is "Server error! Please try again!";

possible cases:

  1. 1st acknowledgePurchase() get DEVELOPER_ERROR, 2th acknowledgePurchase() get successfully
  2. 1st acknowledgePurchase() get successfully, 2th acknowledgePurchase() get DEVELOPER_ERROR [This is the most common case]
  3. both acknowledgePurchase() get successfully

testing with billing client library version is 3.0.0/2.2.0/2.1.0;

Can anyone explain this? Thanks

1 Answers

As far as I remember, only one acknowledge can be given per purchase. If an acknowledge is given to a purchase that has already been acknowledged, an error will occur.

Remember to verify if the purchase is already acknowledged before trying to acknowledge it.

if (purchase.purchaseState == Purchase.PurchaseState.PURCHASED) {
    if (!purchase.isAcknowledged){
        val acknowledgePurchaseParams = AcknowledgePurchaseParams
            .newBuilder()
            .setPurchaseToken(purchase.purchaseToken)
            .build()

        billingClient.acknowledgePurchase(acknowledgePurchaseParams) {
            // result here
        }
    }
}

If your code triggers two calls of acknowledgePurchase to the same purchase at "the same time", there might be the problem.


For further information please refer to https://developer.android.com/google/play/billing/integrate

Related