Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android In-app purchase: how to consume?

I've implemented Google In-App Billing V3 in my app and i did my first test purchase. Now, as seen that i want it consumable, but if i click the "Purchase" button again i receive an error, i'm wondering how and where to insert "consumePurchase". I've been all day long on my computer searching on every thread, but i'm making confusion with old versions of the same. From what i saw, i need to call consumePurchase after the successfully purchased item AND when the activity is created, but i can't figure out how to do it.

Is this the one and only line of code?

int response = mService.consumePurchase(3, getPackageName(), token);

If so, what is "token"?

P.s. the consumable items are: 50, 150 and 300 coins that the user can buy to take a little advantage in the game.

Aaah, so confusing for me :/

like image 810
Giulio Tedesco Avatar asked May 24 '14 18:05

Giulio Tedesco


2 Answers

As stated in the official documentetion: https://developer.android.com/google/play/billing/billing_reference.html

The response intent to the purchase includes several fields, one of them being:

INAPP_PURCHASE_DATA A String in JSON format that contains details about the purchase order. See table 4 for a description of the JSON fields.

Inside that JSON, you have several fields, also explained in that page, the one you are looking for is:

purchaseToken A token that uniquely identifies a purchase for a given item and user pair.

All these is quite easy to follow from the official sample application, which I recommend you to download and try out, also to check the code.

like image 108
shalafi Avatar answered Oct 20 '22 09:10

shalafi


Ok, i solved. Instead of using:

int response = mService.consumePurchase(3, getPackageName(), token); 

follow this thread:

mService.consumePurchase(3, packageName, purchaseToken) always returns RESULT_DEVELOPER_ERROR = 5 - invalid arguments provided to the API

like image 32
Giulio Tedesco Avatar answered Oct 20 '22 10:10

Giulio Tedesco