5

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 :/

Giulio Tedesco
  • 179
  • 1
  • 3
  • 14

2 Answers2

7

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.

shalafi
  • 3,926
  • 2
  • 23
  • 27
  • Ok, thanks. But, where do i have to put response = mService.consumePurchase(3, getPackageName(), token); ? every time i paste this line inside my activity i need to surround it with try/catch, and it doesn't work. – Giulio Tedesco May 24 '14 at 18:48
  • I've downloaded the app, but it's COMPLETELY different, again. It's not like the official tutorial, the game got codes like "base64" ecc.. – Giulio Tedesco May 24 '14 at 19:01
  • You have to consume a purchase after your app has made whatever it was needed (i.e. virtual currency) If you are not doing any server side validation, the place is probably straight after receiving the notification of the purchase. – shalafi May 26 '14 at 08:12
  • In code, when purchase finished, shall I to consume current item or not ? How much is this critical ? And how can I to see my purchased items ? – Elron Apr 01 '16 at 13:52
4

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

Community
  • 1
  • 1
Giulio Tedesco
  • 179
  • 1
  • 3
  • 14
  • Thank you, this worked for me. Anyway, it should be a best practice store the token in the device or in a server when the user makes a purchase so you don't have to make a request to Google asking for all the purchases and then search for the one you need to consume in a loop – campsjos Feb 12 '15 at 11:34