j3k0 / cordova-plugin-purchase

In-App Purchase for Cordova on iOS, Android and Windows
https://purchase.cordova.fovea.cc
1.3k stars 537 forks source link

Finish a product in Android #108

Closed bobibt closed 6 years ago

bobibt commented 9 years ago

After verifying the purchase on my web service i want to finish the product. It is a consumable product so it must be consumed first, before purchasing again. When i try to purchase the product again, the approved event is called two times, the third three and so on. This means that the product isn't consumed so it can't be finished properly. It goes to valid state but every time (when store.refresh() is called) the approved event is called as many times as the product was purchased. I think that the plugin is handling the events properly, but why doesn't it consume the product? It sometimes shows an error that the product is not owned so it can't be consumed, but consumables doesn't go into owned state. Please help, l lost more than a week trying to fix it and i haven't found someone that has my problem.

bobibt commented 9 years ago

I think that Google Play treats my product as a non-consumable and not as a consumable, but don't know why...

agamemnus commented 9 years ago

Same problem here. See this: https://github.com/j3k0/cordova-plugin-purchase/issues/116. Did you fix it? I will spend tomorrow examining the code if not.

agamemnus commented 9 years ago

Okay. I'm just ignoring the errors on the first run to my validation function for now.

Anyway, to answer your question, I think that the product is consumed (not 100% sure) but the refresh function just gets all the products that were bought EVER by the user.

bobibt commented 9 years ago

But if it's consumed it won't go in the approved state again. So that's why i think it isn't consumed properly.

agamemnus commented 9 years ago

https://github.com/j3k0/cordova-plugin-purchase/issues/117#issuecomment-68983009

agamemnus commented 9 years ago

To recap:

There are two issues here basically. First, the error calls don't have a return statement. This causes a number of bugs.

Second, the refresh gets all the purchases: ALL of them! Even consumed purchases. You have to modify the source code in some way (that I don't know yet) to only get unconsumed purchases, or you need to find which purchases were actually consumed and ignore those in your validation function. I added the returns here: https://github.com/agamemnus/cordova-plugin-purchase/blob/fixed_returns/src/android/com/smartmobilesoftware/inappbilling/InAppBillingPlugin.java

As a stopgap measure, don't call refresh() more than once and ignore the errors the first time around.

bobibt commented 9 years ago

But if it takes the consumed purchases the order id would be different and it's not the case. The order id is the same for the same consumable (when the event is triggered continuously). Every order should have different order id. Also if i buy another product for the first time it triggers the event as many ways +1 :), but with the id and alias from the new product so this means that something else is happening.

agamemnus commented 9 years ago

You are either calling refresh() more than once and/or doing something very, very wrong... did you try my modified code?

bobibt commented 9 years ago

I tried without calling refresh and is still the same. I'll try your code now, does it loads all purchases (and the consumed ones) or that is the fix in this code?

agamemnus commented 9 years ago

The only fix is adding the returns. That might help. I didn't fix the part of refresh calling all purchases yet...

bobibt commented 9 years ago

I finally found it!!! :))

The problem was that i was registering the callbacks every time i navigate in the tab where i purchase products. You should register them once or unregister when leaving with store.off(). You should unregister or register all callback from "when", "once", "ready" and "error".

Hope this will help you too... Good luck :)

agamemnus commented 9 years ago

Yes, I knew about this, but indeed the registry-type behavior is not something that I expected at first and is probably a little more brittle (although more efficient in some ways) than just adding single-use callbacks; i.e.: callbacks that remove themselves after use.

What's the use of store.off() when exiting the program? Or is that just to basically turn off the registered callbacks?

After changing your code, are you still experiencing the problem of refreshes calling all the previously bought items? That's "normal", right?

bobibt commented 9 years ago

I'm unregistering the callbacks (calling store.off) everytime i leave the page. i also call it when entering the page because that will help me when the app is force stopped. I'm calling refresh every time i enter the page, but not after every purchase, maybe it will make the same problems maybe not, but like this it works fine, i don't have nerves to try things anymore :D

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.