Thursday, February 28, 2013

Android Add In-app billing to existing project

Hello,

If you have ever encountered situation, where you need to add In-app billing to your existing project, than this blog is for you. Please note that for this you need Google Play Account and valid Google Merchant Account.

First open Android SDK manager. You can open it from Window - > Android SDK Manager. If it's not available in menu go to Window -> Customize Perspective and Go to third tab  Command Groups Availability.


And check the Android SDK and AVD Manager. Now it should be available in menu. Open it and Go to Extra.


Check the Google Play Billing Library and Click on Install items. This will download necessary files of billing library. Also it will download the sample application. No go to your project src folder in eclipse and Right click on it. Choose Menu New - > Package. Give the name com.android.billing.vending to the package and add file IInAppBillingService.aidl to it. You can find your file in your Android SDK folder at this path extras - > google - > play_billing - > in-app-billing-v03

Now create one more package in in your src and give name com.yourappname. util and add following files to it form sample application. Replace yourappname with your application name.



Now compile your project and it should generate IInAppBillingService.java file in gen folder.


Now add following line to your AndroidManifest.xml file


That's it and now your application is ready for in app billing. For testing use reserved SKUs by google play store. Now create a new activity class and give the suitable name to it. in my case I name it as PurchaseActivity

Add a member to a class 

IabHelper mHelper;

Add following code to onCreate function of activity.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purcahse);
String base64EncodedPublicKey = "yourapppublickey";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
  public void onIabSetupFinished(IabResult result) {
      if (!result.isSuccess()) {
        // Oh noes, there was a problem.
        Log.d("inappp", "Problem setting up In-app Billing: " + result);
      }
      String purchaseToken = "inapp:"+getPackageName()+":android.test.purchased";
     
      startPurchase();
  }
});

Replace yourapppublickey with your application's public key. For this you need create application in google play store and it give you application public key. Here android.test.purchased is the reserved SKU maintained by Google Play Store for the testing purpose. You don't need to spend money to test it.

Add following functions to your class.

public void startPurchase(){
mHelper.launchPurchaseFlow(this, "android.test.purchased", 10001,   
  mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
  = new IabHelper.OnIabPurchaseFinishedListener() {
  public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
  {
      if (result.isFailure()) {
        Log.d("inapp", "Error purchasing: " + result);
        return;
      }      
      else if (purchase.getSku().equals("test")) {
      Log.d("inapp","Purchase successful" + result);
      }
  }
};

Once purchased you have to consume purchase. Please note that this blog does not mention the consumption process. This just shows how to test in app billing in your project. Later you can replace the test SKUs with your own SKUs.







No comments:

Post a Comment