Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android In-app Billing tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-02-2012, 08:17 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android In-app Billing tutorial

Android market provides an in-app billing service which you can use to accept payments from your own application.

The code required to use this service is quite simple. However the actual process that is done in the background is pretty complicated. To better understand it you should read the official documentation: In-app Billing | Android Developers

This tutorial will cover the main concepts and the steps required to integrate in-app billing in your Basic4android project.

The billing service allows the user to purchase "products". Products are declared in the Android market developer console.
There are two types of products: managed and unmanaged products.

Differences between managed and unmanaged products:
- The user can only purchase a managed products once.
- You can later restore all the managed transactions.

For example, access to premium features should be a managed product.
Coins, potions and other "temporary" assets should be unmanaged products.

Developer console:





Basic4android code
The InAppBilling library includes one type of object which is BillingManager.
The BillingManager should belong to a Service module.

Typical code should look like:
Code:
'Service module
Sub Process_Globals
    
Dim Manager As BillingManager
    
Dim PublicKey As String
    PublicKey = 
"xxxx" 'From the developer console
End Sub

Sub Service_Create
    Manager.Initialize(
"manager", PublicKey)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Manager.HandleIntent(StartingIntent)
End Sub

Sub Manager_BillingSupported (Supported As Boolean)
    
Log("BillingSupported: " & Supported)
End Sub

Sub Manager_PurchaseStateChange (PurchaseState As Int, ProductIdAs String, OrderId As String, PurchaseTime As Long, ExtraData As String)
    
Log("PurchaseStateChange: " & PurchaseState & "" & ProductId& "" & OrderId & "" & DateTime.Time(PurchaseTime) & "" & ExtraData)
    
If PurchaseState = Manager.PURCHASE_STATE_PURCHASED Then
        
'Here you should store the purchase information
        'and then notify the activity about the purchase.
        'In most cases the activity will be running at this state. However it is possible that it will be paused.
        'In that case the following call will be ignored.
        CallSub2(Main, "ThankYouForPurchasing", ProductId)
    
End If
End Sub

Sub Service_Destroy
    Manager.UnbindMarketService
End Sub
PublicKey is your developer public key as appears in the developer console under Edit Profile.
This string should be set in Process_Globals sub to allow it to be obfuscated.

The BillingManager is initialized in Service_Create. BillingManager processes the intents that are sent to the service (through the internal receiver).

BillingManager provides two events:
BillingSupported - This event is raised after initializing the manager. The parameter will tell you whether billing is supported. Note that the emulator doesn't support billing.

PurchaseStateChange - This event is raised when a product is purchased, refunded or cancelled.
PurchaseState will be one of the following values: PURCHASE_STATE_PURCHASED, PURCHASE_STATE_CANCELED, PURCHASE_STATE_REFUNDED or PURCHASE_STATE_NODATA.
ExtraData is an arbitrary string that you can send together with the purchase request.

When the service is destroyed you should call UnbindMarketService to free resources.

Activity code
Code:
Sub Activity_Create(FirstTime As Boolean)
    
If IsPaused(InAppBillingService) Then StartService(InAppBillingService)
    Activity.LoadLayout(
"1")
End Sub

Sub Button1_Click
    InAppBillingService.Manager.RequestPayment(Activity, 
"unmanaged_1""extra data")
End Sub

Sub ThankYouForPurchasing (Product As String)
    
Msgbox("Thank you for purchasing: " & Product, "")
End Sub
When the activity is created we start the service if necessary.
The following code sends a purchase request to the market for a product:
Code:
'InAppBillingService is the name of the Service.
InAppBillingService.Manager.RequestPayment(Activity, "unmanaged_1""extra data")
RestoreTransactions
Managed products are tracked by the market. It is possible to retrieve previous purchases of such products.
For example if the user has uninstalled the application and now installs it again, you can call RestoreTransactions to retrieve the previous products.

According to Google documentation you should not call RestoreTransactions each time your application starts.

When you call RestoreTransactions the PurchaseStateChange event will be raised for each purchase. If there are no previous transaction then PurchaseStateChange will be raised and PurchaseState parameter value will be PURCHASE_STATE_NODATA.

Manifest editor
The following code should be added to the manifest editor code (change InAppBillingService to your service name):
Code:
'InAppBilling declarations - Start
AddReceiverText(InAppBillingService,  <intent-filter>
                <action android:name=
"com.android.vending.billing.IN_APP_NOTIFY" />
                <action android:name=
"com.android.vending.billing.RESPONSE_CODE" />
                <action android:name=
"com.android.vending.billing.PURCHASE_STATE_CHANGED" />
            </
intent-filter>)
'InAppBilling declarations - End
Testing your application

It is not so simple to test the payment process. A device set to the same account as the merchant account cannot be used, as you are not allowed to purchase from yourself.

From my experience this is the easiest way to test your application:
- Upload a draft application to the market. You do not need to publish it.
- Set some low cost ($0.01) products.
- Use a device with an account other than the merchant account.
- Add this account as a test account. This is done under Edit Profile page.
The account must be a gmail.com account.

You will now be able to develop and debug with this device. If you like you can also cancel the orders from the merchant console.

You may need to first publish the application and then unpublish it.
The application must be signed with the same signature and have the same version number as the uploaded one.

Tips
- Track the logs.
- When you cancel an order the purchase state will be PURCHASE_STATE_CANCEL. When you partially refund it the state will be PURCHASE_STATE_REFUND.

The library is available here: http://www.basic4ppc.com/forum/addit...html#post82746
Reply With Quote
  #2 (permalink)  
Old 02-03-2012, 08:21 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

1) It is better to start a new thread for this question as it is not really related to this one.

2) -1 is the value of BillingManager.PURCHASE_STATE_NODATA. As written in the tutorial this means that there are no previous managed transactions.

The error that you see is because you tried to show a Msgbox from a service. This is not possible. Services cannot show any UI elements except of a toast message.
Reply With Quote
  #3 (permalink)  
Old 02-03-2012, 09:03 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
"Extra data" is saved on google clouds?
I think that it is saved for managed transactions. However I'm not sure. You will need to test it.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android FTP tutorial Erel Basic4android Getting started & Tutorials 104 Yesterday 05:51 PM
Android Serial tutorial Erel Basic4android Getting started & Tutorials 78 05-20-2012 05:39 AM
Android Market now allows AppIN billing KashMalaga Bugs & wishlist 12 03-28-2012 04:58 PM
Android FTP Tutorial klaus German Tutorials 1 02-04-2012 04:01 PM
[B4A] [Tutorial] Almacenamiento en Android iliberis Spanish Forum 5 12-06-2011 05:15 PM


All times are GMT. The time now is 10:07 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0