Tutorial details

[TUT] Simple InApp Billing / Payment V3 | App Code for Sale | Preview

[TUT] Simple InApp Billing / Payment V3 | Android Tutorial

How to work with InApp Billing / Payment V3

Overview PAGE TOP

Google have released a new version of their In App Billing (Version 3). So I’ve followed suit and updated my tutorial!

Eclipse Project Source Code Download

GitHub Source Code Repo Link

There are a fair few changes, too many to fit into one tutorial but the overview is:

- All purchases are now managed purchases

This means that once a user buys your item they own it, you have to ‘consume the purchase’ if you want to allow it to be purchased again. This is used in this tutorial but not explained or coded in a clean way so it is left for my next tut.

The benefits of this are that all purchases will be stored by Google Play and it will be easy to retrieve the status of whether a user has already purchased an item.

- The Android team have released a ‘helper’ class

This is to make in app purchases that much simpler. It has a vague resemblance to my original ‘BillingHelper’ class only it is called ‘IabHelper’ .. very interesting. This helper class makes things a hell of a lot easier.

Let’s get straight into it.

To start with I’d like to say, this isn’t the only way to do this. I myself have followed the developer tutorials on developer.android and managed to create this test project which I can now use as a reference when making other projects. Feel free to comment on the style and ask any questions. I would recommend you download the test project I have attached rather than copy and paste, this will stop minor syntax errors.

Developer.Android Links:

Google Billing Overview

Google Billing Integration

Ok first the outline.

Were going to create and app that has one button, when you click this button it informs the android market you want to make a purchase. On confirmation of this purchase you have bought a picture of a passport and it is shown to you in the app.

Simple setup stuff will be skipped (it is all in the download source).

First you create your project with the IInAppBillingService.aidl file included. You then need to add the Billing Helper classes, look at the source code, I have added these in the android.vending.billing.util package. They are the same as the Android example except the IabHelper which I have modified when I found some slight bugs.

Create 3 Activities, the first is the start up activity. This checks if In App Purchase is available on the device. If it is we load the main activity if it isn’t we don’t let them go any further:

StartUpActivity.java

+ expand source

This StartUpActivity extends the PurchaseActivity. The PurchaseActivity is what talks to the Android helper service. Whenever the activity is created it will check that in app billing is still supported at this time.

PurchaseActivity.java

+ expand source

Then our MainActivity, this just has the button for purchasing a passport. It will also receive a callback saying if the passport was bought or not.

The MainActivity starts the PurchasePassportActivity using startActivityForResult, this is how we inform it of a purchase. As with the StartUpActivity the PurchasePassportActivity extends our PurchaseActivity allowing it to talk to the Google Play Billing Service (and it’s helper class).

When the PurchasePassportActivity is started, it will get a callback saying billing is available and go onto to purchase the passport. The only thing special in this Activity is that is passed the SKU (the items ID from Google Play) to the Helper service. In this tutorial we use the “android.test.purchased” SKU meaning purchases will always be successful.

PurchasePassportActivity

+ expand source

Note- The code below in the PurchaseActivity.java would be removed if you wanted to use this. It is there because the new version of in app billing treats all purchases as “managed items” i.e. when you buy them once you own them until you use them or they are refunded. So this code automatically ‘refunds’ the purchase so the demo doesn’t just work first time!

1   // DEBUG XXX
2   // We consume the item straight away so we can test multiple purchases
3   billingHelper.consumeAsync(info, null);
4   // END DEBUG

That’s it, the rest of the code are just helpers and comments to try and explain what is going on and making it easier to extend.

As I always say This code tutorial isn’t foolproof and I feel I may of swept over a few things. But I really want to just give an alternative to the tutorial that is on the developer.android site. You could read this and understand the smaller concepts then go on to make a better implementation.

If you want to productionise this code you will want to go away and look at some of the security tutorials, obfuscation etc.

Remember to say thanks and enjoy!

Here are the code links again:

Eclipse Project Source Code Download

GitHub Source Code Repo Link

Reference PAGE TOP

http://blog.blundell-apps.com/simple-inapp-billing-payment-v3/

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.