Android Tutorial Android In-App Billing v3 Tutorial

New version: GooglePlayBilling - In App Purchases

This tutorial covers the new API for in-app billing provided by Google.

The main differences between v3 and the previous version are:
- (v3) Is easier to implement.
- Supports managed products and subscriptions. Unmanaged products are not supported.
- Includes a method to retrieve all purchased items. This means that you do not need to manage the items yourself.
- Allows you to "consume" managed products. For example if the user has bought a game add-on and then used it, the app consumes the product allowing the user to purchase the add-on again.

The official documentation is available here: In-app Billing Version 3 | Android Developers

Implementing in-app billing in your application

The first step is to upload a draft APK to Google developer console. Note that you should use a private signing key to sign the app.

Under Services & APIs you will find your license key. This key is also required for in-app billing:

SS-2013-06-06_17.21.31.png


You should also add at least one item to the "In-app Products" list. The item's type should be Managed Product or Subscription.

Basic4android code

The first step is to initialize a BillingManager3 object:
B4X:
Sub Process_Globals
   Dim manager As BillingManager3
   Private key As String = "MIIBIjANBgkqhkiG9w0BAQEFAA..."
End Sub

Sub Globals


End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      manager.Initialize("manager", key)
      manager.DebugLogging = True
   End If
   ...
End Sub

Sub Manager_BillingSupported (Supported As Boolean, Message As String)
   Log(Supported & ", " & Message)
   Log("Subscriptions supported: " & manager.SubscriptionsSupported)
End Sub

The BillingSupported event will be raised with the result. Note that all of the billing related actions happen in the background and raise events when the action is completed.

Calling manager.GetOwnedProducts will raise the OwnedProducts event. The OwnedProducts event includes a Map that holds the user current purchases. The keys in the map are the products ids (or Skus) and the values are objects of type Purchase. These objects include more information about the purchase and are required for other actions, such as consuming a purchase.

B4X:
Sub manager_OwnedProducts (Success As Boolean, purchases As Map)
   Log(Success)
   If Success Then
      Log(purchases)
      For Each p As Purchase In purchases.Values
         Log(p.ProductId & ", Purchased? " & (p.PurchaseState = p.STATE_PURCHASED))
      Next
   End If
End Sub

Purchasing a product is done by calling: manager.RequestPayment. The user will be asked to approve the payment. The PurchaseCompleted event will be raised when the operation completes.

Note that managed products can only be purchased once. Only after you call ConsumeProduct will the user be able to purchase the same item again.

Consuming purchased products is done by calling manager.ConsumeProduct.
For example:
B4X:
If ownedProducts.ContainsKey("test2") Then
   manager.ConsumeProduct(ownedProducts.Get("test2"))
End If

The ProductConsumed event will be raised.

Tips
- See this tutorial for more information about the possible testing options: Testing In-app Billing | Android Developers
- If you get a "signature verification error" when trying to make a purchase then you should make sure that the licensing key is correct. If it is correct then try to upload a new APK.
- It is recommended to use a process global variable to hold the key. This way it will be obfuscated when you compile in obfuscated mode.
- Only one background request can run at a time.

The library is available here:
http://www.b4x.com/forum/additional.../29998-app-billing-v3-library.html#post174139
 
Last edited:

deltacode

Member
Licensed User
Longtime User
Hi, added this and put my list of purchases in the app.

when a user goes to buy it comes back

Purchase currency not supported

User is English and its on the market in the beta channel, anybody seen this message before ? Have i missed a setting ?

B4X:
dim strPayment as string = "payment_100"
manager.RequestPayment(strPayment, "inapp", "myApp v" & Vars.APP_VERSION)

payment_100 is one of the payments i have setup on the market.
 

deltacode

Member
Licensed User
Longtime User
This is a preconfigured setup as im replacing an old java project with a new sparkling B4A project. It was working against the billing v1 originally and the currency was GBP.
 

JOANORSKY

Member
Licensed User
Longtime User
Hi there... i have received this email from google a few days ago... can anyone inform me how this affects the current inapp Billing and how can i upgrade it?


Hello,
If you previously used the In-app billing sample code to build your in-app billing system, please use the recently-updated sample code as it addresses an exploitable flaw we recently discovered (note that this only affects the helper sample code; the core system and in-app billing service itself was not affected).

The affected applications are those that use the in-app billing sample library (specifically, the IabHelper and the Security classes in the util directory of the in-app billing V3 sample) and do not perform server-side verification.

An update to the sample and library that fixes this vulnerability is now available at code.google.com/p/marketbilling and also through the Android SDK Manager.

To apply the security update:
1. Download the updated source code for the in-app billing sample and library from the Android SDK Manager, which is part of the Android SDK. The in-app billing package is located under Extras -> Google Play Billing Library. Make sure to update to Revision 5. (or, alternatively, download the updated source code from the public repository at code.google.com/p/marketbilling).

2. Merge the new code for IabHelper.java and Security.java into your application, replacing the existing code.

If you prefer to apply the code changes manually, you can browse the diff at

https://code.google.com/p/marketbilling/source/detail?r=7bc191a004483a1034b758e1df0bda062088d840

and merge the modifications into the appropriate parts of your code.

Thank you for your continued support of Google Play.

Regards,

The Google Play Team


©2013 Google Inc.
1600 Amphitheatre Parkway
Mountain View, CA 94043


The fact is .. that somehow.. someguys... are now becoming to be able to order some of my products without the payment have has been cleared (i have a implemented system that a email is sent ordering the item when the payment clears, but, these last weeks.. i've been receiving emails ordering.. but when i check it.. the payment is nowhere to be found.. probably because of this exploit. Anyone wants to comment?
 
Last edited:

touchsquid

Active Member
Licensed User
Longtime User
Hi Erel,

Just testing with an app here, but it seems I get the same Purchase State back whether the app is purchased, canceled, or refunded.

Sub Manager_OwnedProducts(Success As Boolean, Purchases As Map)
If Success Then
If Purchases.Size > 0 Then
For Each p As Purchase In Purchases.Values
If p.PurchaseState <> p.STATE_PURCHASED Then Activity.Finish
Next
Else
Activity.Finish
End If
End If
End Sub

I should also mention that there is only one possible product to purchase.
 
Last edited:

touchsquid

Active Member
Licensed User
Longtime User
How are you checking the state?
Add Log(p.PurchaseState). 0 - purchased, 1 - canceled, 2 - refunded.

Note that once you purchased a product it will appear as a purchased product until you consume it.

It can take awhile until canceled purchases are processed. This seems like an issue in Google service.

If we consume a purchased product, will it still appear as a purchased product (if it hasn't been canceled or refunded)?
 

tufanv

Expert
Licensed User
Longtime User
Hello Erel in the first page a mamber asked that he cant use ownedproducts . ( I got an error as ownedproducts is not declared)
If ownedProducts.ContainsKey("test2") Then
manager.ConsumeProduct(ownedProducts.Get("test2"))
EndIf

I cant use it too . You had asked him if he knows autocomplete function but i couldnt understand how to do it. ownedproducts is coming in red in my code section as it is not declared. Can you help
 

tufanv

Expert
Licensed User
Longtime User
I call Manager.GetOwnedProducts after purchase completed in purchasecomplete event . And then in owned products event sub i can access what user bought but cant consume it because it still doesnt recognize ownedproducts as not declared (in red) . sorry for noob questions i had no problem with 1 single managed item i was directly going to purchasecomplete event and do what i want but now mecause i have multiple items i need to consume it and now which item was bought
 

tsteward

Well-Known Member
Licensed User
Longtime User
From my experience with this service the best way to test the app is by adding a low cost item ($0.01) and then upload the app without publishing it, install it and test it with real purchases.

You might also need to publish it and then unpublish it after a few minutes.
"The publisher can not purchase this item"
This really sucks. I want to run in debug mode and see what the map actually contains, watch how my app reacts etc.
Also the minimum price you can set is $0.99 so it could cost a dollar. Just set a trial period I guess and uninstall. But still can't debug can I?

Have I missed something?
 
Top