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:

pezhooman

Member
Licensed User
Longtime User
hi Erel.
Im new.would you send a simple example project with In-app billingv3.?
thanx
 

bluedude

Well-Known Member
Licensed User
Longtime User
Erel,

I'm not sure you explain the "consume" correctly. You say if the user used it this will make it available again. However, below documentation from Google tells me a different story.

"In Version 3, all in-app products are managed. This means that the user's ownership of all in-app item purchases is maintained by Google Play, and your application can query the user's purchase information when needed. When the user successfully purchases an in-app product, that purchase is recorded in Google Play. Once an in-app product is purchased, it is considered to be "owned". In-app products in the "owned" state cannot be purchased from Google Play. You must send a consumption request for the "owned" in-app product before Google Play makes it available for purchase again. Consuming the in-app product reverts it to the "unowned" state, and discards the previous purchase data."

It states that this method sets the product to "unowned" state, and discards the previous purchase data. That means in the end it will not show up again in the owned products list.

Can you explain a little more because you say "consume" means the user used it.
 

Eumel

Active Member
Licensed User
Longtime User
consume something for example in a game.

A User can Buy extra life. InGame if he lost this extra life, he has consumed it.
Now you must send a consuming request to google playstore, and the user can buy the extra life again.
 

sktanmoy

Active Member
Licensed User
Longtime User
In this tutorial, I couldn't find
1. How I allow user to purchase a product
2. Show a message after purchasing
Please help me.
 

LeeM

Member
Licensed User
Longtime User
I've been playing with in-app billing and have successfully purchased items, consumed them (later) and then been able to re-purchase the same items. This was all done with an active internet connection.

Obviously purchasing can only be done with an active internet connection but how can I consume items if I'm offline ?
When I purchase my items should I save information about the items locally and then if I'm offline and I consume any items later should I log this locally too. Then when I go online I can update the console ?
 

Vince

Member
Licensed User
Longtime User
May i know the service will support Malaysia or Singapore? because google merchant didn't support this two country, whether in-app billing can support all? thanks
 

deltacode

Member
Licensed User
Longtime User
Hi all,

I think i am missing something. How can i, on demand, populate a spinner with the list of options to be purchased from my in-app purchase list on the store ?

I know i could manually populate this but that would be duplicating work when they are already setup on the store.

Is this possible ?
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel !

I have a question here: manager is a global object but inside an Activity (Main). If I need to use this object in another activity, the call will be Main.manager.whatever ... but the events will be raised in the current activity or in Main ? (remember that Main activity is paused in this moment). What is the flow? Events Subs will be called imediatly? The Main activity will open and pause the current? Or we need to declare events inside the current activity also and these ones (not others in Main) will be triggered???
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hi @Erel !

I have a question here: manager is a global object but inside an Activity (Main). If I need to use this object in another activity, the call will be Main.manager.whatever ... but the events will be raised in the current activity or in Main ? (remember that Main activity is paused in this moment). What is the flow? Events Subs will be called imediatly? The Main activity will open and pause the current? Or we need to declare events inside the current activity also and these ones (not others in Main) will be triggered???


Ok, I tested - the event returns inside the activity which called main.manager ... thanks!
 
Top