Android InApp Billing / Payment

Hi guys,
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.
Going to try and walk you through creating an inapp payment method with the new android market payment system. This isn’t live yet (you can’t publish the app for general use) but you can upload it to the market and do full testing.
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.
First things first.
Create a new project.
This project has a Main Activity XML and Class.

You could now run this project up and it won’t do anything. Fine.
There is an AIDL file that you have to include your project, this gives you access to the Remote Methods of the Android Market service. Don’t worry about this, just know that this is what is under the com.android.vending.billing package in the test project.
InApp Billing in android basically means:
Tell the Android Market what the user is buying In the test project it is “android.test.purchase”
The Android Market compares this against a list you create (on your publisher page) [url=Testing]http://developer.android.com/guide/market/billing/billing_testing.html[/url]
User fills in their details You dont have to worry about this
You receive a message saying purchase complete or failed.
In order to tell the Android Market what you are buying you need to be running the billing service. ( A service that talks between your app and the android market).

Now I have wrapped this BillingService in a BillingHelper, the helper will do all your work for you (call the market with purchases).

When a request to the android market from your BillingHelper is instantiated, the market sends back certain intent’s these need to be caught in a receiver and dealt with appropriately (so the market will say “yes I confirm this user has just bought…” ).

To keep the code a bit tidier all constants are stored in a class called C.

And finally, this code I took from the Android sample project. What it does is decrypt the messages that the Android Market is sending back to you and package them into a purchased item object. YOU NEED TO EDIT ONE LINE, to add your public key from your market account (edit profile):

That is it. Now to get this to work you have to add to your activity class.
In your onCreate you need to start the market service.
startService(new Intent(mContext, BillingService.class));
In the onClick is where we are going to request to make a purchase:
@Override
public void onClick(View v) {
switch (v.getId()) {
 case R.id.main_purchase_yes:
  if(BillingHelper.isBillingSupported()){
    BillingHelper.requestPurchase(mContext,"android.test.purchased");
      
        } else {
              Log.i(TAG,"Can't purchase on this device");
        }                     
             break;
    default:        
             Log.i(TAG,"default. ID: "+v.getId());
               break;
             }                
        }
Now the BillingService won’t do anything on it’s own, so you need to load up the BillingHelper. The billing helper uses a handler to send callbacks on completed purchases:

BillingHelper.setCompletedHandler(mTransactionHandler);

This handler needs to be declared in your activity:
public Handler mTransactionHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
 Log.i(TAG,"Transaction complete");                       
    if(BillingHelper.latestPurchase.isPurchased()){
          showItem();
      } else {
          // Failure
        }
               };    
   };
Once you have all this don’t forget to update your manifest! You need to tell it your using a service, a broadcast reciever and those intents you wish to receive (from the android market).
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.
Caveats:
May have issues with multiple purchases and network delays
I am not responsible if you use this code in a production envrionment.
Please obfuscate your code to ensure people can’t get your purchases for free!


Comments

Popular posts from this blog

How to implement pinch and pan zoom on surface view ?

Zooming and Dragging images using matrix in android