简体   繁体   中英

Google Play Billing library developer error code 5 if I want to consume or acknowledge the purchase

I added the latest Google Play Billing library:

implementation 'com.android.billingclient:billing:2.0.1'

If I make a purchase with a "slow testing card, which always be approved after a couple of minutes" (this is a testing card from Google to test in-app purchases):

val params = BillingFlowParams
   .newBuilder()
   .setSkuDetails(skuId)
   .build()

 billingClient.launchBillingFlow(activity, params)

Everything goes fine, until I try to acknowledge or to consume the product:

val consumeParams = ConsumeParams
   .newBuilder()
   .setPurchaseToken(purchase.purchaseToken)
   .build()

billingClient.consumeAsync(consumeParams, this)

I'm getting the following error in the callback:

/**
     * Invalid arguments provided to the API. This error can also indicate that the application was
     * not correctly signed or properly set up for In-app Billing in Google Play, or does not have
     * the necessary permissions in its manifest
     */
    int DEVELOPER_ERROR = 5;

I also get a debug message saying that the purchase has an invalid state (PENDING). Probably I can't consume/acknowledge purchases which doesn't have the "SUCCESS" state.

According to Google, I have 3 days to consume or to acknowledge the purchase, otherwise it will be refunded.

But how am I supposed to consume or acknowledge the purchase if they won't allow me to make it right after the purchase was made?

  1. I don't own a server, the consumption/acknowledge should be made on mobile
  2. I can't keep the user with a loading progress dialog/view in the app while the purchase will be validated (getting the SUCCESS state. With a testing account, within 5 - 6 minutes I'm getting the new " SUCCESS " state via onPurchasesUpdated(billingResult: BillingResult?, purchases: MutableList<Purchase>?) )

I'm using the MVVM architecture ( Activity - ViewModel ) and within the Activity I'm keeping the Billing Client library (because the library needs a Context ) and in the ViewModel the business logic.

It looks like Google doesn't want us to "bind" the billing client library to an activity/viewmodel and instead use it in a Service and instantiate it in the Application class. Probably communicating with Observers or BroadcastReceivers and listen for purchase updates. And once the Purchase is validated or rejected I should update the user's profile. This already can cause a problem, because I can't start a Service without having a non-dismissable sticky notification because this is a foreground service and I should notify the user that the app is running in the background. This will scare the hell out of the users.

This new purchase flow breaks multiple things. This shouldn't be asynchronous. In real life situation when I purchase something, I give the money to the cashier then I receive my product/service not after 5 minutes! How am I supposed to deal with this? The user makes a purchase, then I notify the user he/she will get the product within 5 minutes after confirmation from Google? This should be instantaneous.

How do you deal with in-app purchases?

The answer is simple. Tell the user that the purchase request has occurred but the payment or request has not been confirmed or processed yet.

This situation should and must be asynchronous because it can happen in some scenarios and is exactly what happens in real life, such as when the user uses a payment method that is not instant. Some payment methods can take days to process - not everyone uses credit/debit cards or gift cards for online payments, and not everyone can! For example, in Brazil there is the Boleto payment system which is easy to pay for but takes a long time to process. People pay for their item, print out a receipt and go stand in line at certain places to pay for stuff. It is done this way because getting an international online card is very difficult, especially among the very poor.

So, what you need to do is you get your new purchase. Check its status. If it is SUCCESS then your payment is complete and tell the user. If it is PENDING, then tell the user that the payment request has been completed and awaiting payment confirmation from Google.

If you get a purchase in the PENDING state, when the payment clears you will get a new purchase request through the flow of your app, but this time it will say that the purchase was SUCCESS.

If this doesn't fit your current architecture then you must change your architecture.

I might be late to the party, but i hope it helped. My case is response code 5 invalid token. I got this error when try to consume a subscription product after successfully acknowledge. I made a mistake to differ which one need to be consume and which one are not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM