IAB not returning un-consumed purchase
Asked Answered
E

2

8

I have an app which has in-app purchases using Google's In-App Billing API. This generally works well, even with internet connection being lost after the purchase has been made, which makes the product show up as an un-consumed purchase. However in some edge case involving turning wifi on & off a few times during the purchase, it sometimes happens that the purchase is processed (i.e. money has been paid), but not yet consumed in the app.

When using the getPurchases() method, this product is not returned as an un-consumed purchase, even though the documentation says it should be. When trying to purchase this product again, however, the API does return the code BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED.
So this product is marked as un-consumed.

Interestingly, after I run the command adb shell pm clear com.android.vending in my terminal, this product does show up in the list returned from getPurchases().

What is happening here that the product only shows up as an un-consumed product after running the terminal command? How can I get this product to show up as it should in the getPurchases call?

(If this issue cannot be fixed, perhaps I might, upon loading the view where purchases can be made, "simulate" the buying process for each item, check whether it returns the BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED code, grant the user the purchase for each item that does and resolve this purchase programmatically. This seems way exaggerated though.)

Evert answered 11/4, 2018 at 9:59 Comment(0)
E
1

Because the google play service will cache the result and return the cache value first.

The purchase is proceeded before the internet is disconnected. As a result, the purchase state is not updated in the cache. When you query getPurchases() first time, it return the cached value, so the product is not shown up in the purchase list.

After you use command to clean the cache, it force google play service update the cache before return the list for getPurchases(). So the product shows up!

So, I think you should handle the already_owned code instead of checking the purchase state by calling getPurchases() in this case.

Entirely answered 19/4, 2018 at 10:22 Comment(1)
By "handle the already_owned_code", you mean the response I get when trying to purchase the item again? That's what I was also thinking as a last resort, but it would require the user to either manually attempt to buy this item again, or require me to "attempt to buy" all items upon opening the relevant screen. Was hoping there'd be a more elegant solution :) Thanks for your input!Evert
I
0

As docs said

Upon a successful purchase, the user’s purchase data is cached locally by Google Play’s In-app Billing service

I run into the same issue, and the only workaround that I found was to advice the user first open the Play Store app (this should update the internal billing cache), and then return to your app and finish purchase (the purchase must be available in inventory)

Ideograph answered 19/4, 2018 at 9:53 Comment(1)
Opening the Play Store app doesn't appear to update the internal billing cache for me :( Even going into the app settings and clearing cache doesn't help. Force stopping the play store app does fix the issue, but I feel like asking the user to do this wouldn't be a great idea haha. Thanks though!Evert

© 2022 - 2024 — McMap. All rights reserved.