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.basic4ppc.com/forum/addi.../29998-app-billing-v3-library.html#post174139
 
Last edited:

luke2012

Well-Known Member
Licensed User
I'm using In-App Billing for an Annual subscription item and it works fine.
The problem is that I'm forcing the user to use a Internet connection because I'm checking the subscription payment online using Google API.

Any idea to implement a offline (annual) subscription check ? Any support by Google to do this ?
 

Computersmith64

Well-Known Member
Licensed User
I'm using In-App Billing for an Annual subscription item and it works fine.
The problem is that I'm forcing the user to use a Internet connection because I'm checking the subscription payment online using Google API.

Any idea to implement a offline (annual) subscription check ? Any support by Google to do this ?
What I do is store subscription information in a setting & then check it using the API when there is a connection. Actually, the reason I started doing this is because sometimes there is a delay getting up-to-date subscription info through the API - so this allows subscribers to use the app without being affected by that.

- Colin.
 

luke2012

Well-Known Member
Licensed User
Hi to All,
is there a way to track the In-App Subscriptions activated by app's users ?

Thanks in advance for your reply
 

HAH

Well-Known Member
Licensed User
Hello
Where can i set the price amount for in-app purchase ?
 

HAH

Well-Known Member
Licensed User
Thank you, I got it. All things went well :)
 

Computersmith64

Well-Known Member
Licensed User
any easy way to get all products I created in Google Console?
There is a function in the API that will return details (price, etc...) about your products, but you still have to pass it the product ID's (ie: it won't give you a "dynamic" list of products) - however that function isn't implemented in the BillingManager library.
 

Computersmith64

Well-Known Member
Licensed User
I think the best solution is create your own webservice with products to leave the app dynamic.

Your App -> Your WebService (with products ids, descriptions, prices, etc) to show in your app and then:
Your App -> InBilling3 (to call api)

What you think?
I'm not sure that would work because when you add a new product in the developer console, you have to release a new version of the app to go with it - ie: each version of the app is tied to the products that it was released with. You'd probably want to read Google's documentation on in-app billing before you go down that path.

- Colin.
 

aaronk

Well-Known Member
Licensed User
Few things I have found while setting up my in-app purchases, and thought I would let others know in case they come across the same issues..

- Looks like you need to publish the app on Google Play before the app will allow in-app purchases. (can't have the app saved as a draft)

As per Google Play Developer docs: Previously, you could publish a "draft" version of your app for testing. This functionality is no longer supported.
https://developer.android.com/google/play/billing/billing_testing.html

- Need to make sure you have the billing permission added before you upload the app to Google Play. (or I guess add the BillingManager3 lib)
- Recommend to upload the app as a Beta or Alpha app and not production app while testing it. It will still work if you have it as production app but then the public would see this app while you play around with the in-app billing (especially if the app is a new app you are working on and not yet on Google Play)
 

shashkiranr

Active Member
Licensed User
Hi All,

I have monthly subscriptions set up for my app. But every time it is auto renewed, the recurring subscription order id (GPA.XXXX-XXXX-XXX-XXXXX..0) will not come neither the purchase time change to reflect the recurring subscription payment date. I only get the original order id (without the ..0) and the original purchase date (when the initial subscription was purchased.) Is there a way to get the new details.

Best,
SK
 

DaOel

Member
Licensed User
Is the list of OwnedProducts based on the client's device or the client's google account?

This is for example interesting for the following cases: (3 questions)
1. The user buys a product and loses his smartphone. He buys a new smartphone and configures it with his google account. Will the old purchase be listed in the OwnedProducts of his new smartphone?

2. How is it with several devices at the same time, all linked to the same google account. Is every device listing the purchase under Owned products?

3. Is the purchase-Value on several devices exactly the same, or is there a field that might be different?

Ofc, if the answer to question 1 is 'no' then question 2 and 3 don't need to be answered.
 
Last edited:
Top