Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sell digital content in Android through Google Play In-app Billing Service Version 3

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 558
    Comment on it

    Products which you can sell in your application falls into these categories :

    1.Standard in-app products(one time billing) eg: Premium Upgrade, Ads free version of the app. 2.Subscription(recurring,automated billing) eg: Game currency, music voucher.

    Preparing your app for the In-app billing services

    Install Google Play Billing library

    1. Open the Android SDK Manager .
    2. Go to Extras section .
    3. Select Google Play Billing Library and download it by click Install packages .

    Create in-app product in Developer console

    1. Go to Google Play Developer Console. You will need to register , if you have not registered yet.
    2. You also need a Google Wallet merchant account.
    3. Add you application in the Google Play Developer console and add details in the Store Listing section.
    4. In the Services and APIs tab, there is public license key that Google Play generated for your application. It will be used later.
    5. Now , go to in-App products tab and create in-app products according to your needs. You'll be asked to create a unique product ID (alse called SKU) which can't be changes later.

    Implementing In-app billing

    1. Add permission in Android.xml manifest file -

      <uses-permission android:name="com.android.vending.BILLING" />
      

    2. Add IInAppBillingService.aidl file to your project -
      a. Open the Android SDK Manager
      b. In the SDK Manager, go to Extras section
      c. Select Google Play Billing Library and click Install packages to complete the download.
      d. If you are using Eclipse: Create a package named com.android.vending.billing. Copy the IinappBillingService.aidl file from /extras/google/play_billing/
      and paste it into the src/com.android.vending.billing/ folder in your workspace.

      If you are using non-Eclipse IDE: a. Create the following directory /src/com/android/vending/billing and copy the
      InAppBillingService.aidl file into this directory.

    3. Build your application so that InAppBillingService.java file gets generated.
    4. Copy files from TrivialDrive sample Application -
      a. Open /extras/google/play_billing/samples
      b. open TrivialDrive folder and copy all the classes into you project.
    5. Now In you activity , add the code shown below

      a. String PURCHASE_PRODUCT_ID is the product unique id (i.e SKU ) which is shown in the in_app products tab in your Google Play Developer console account.
      b. String base64EncodedPublicKey is a public license key which is a base64 string which can be found in the Services and Apis tab in your Google Play developer Console account.

        IabHelper mHelper;
      
           final String PURCHASE_PRODUCT_ID = "YOUR_PRODUCT_UNIQUE_ID";
      
         /*or, you can also use this
        final String PURCHASE_PRODUCT_ID = "android.test.purchased";
         for testing purposes
      
      */
          int PURCHASE_REQUEST_CODE = 1001;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_in_app_billing);
              String base64EncodedPublicKey = "YOUR_PUBLIC_LICENSE_KEY";
      
              // compute your public key and store it in base64EncodedPublicKey
      // 
              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(TAG, "Problem setting up In-app Billing: " + result);                    
                          mHelper=null;
                          // Setup failed !! do what you want to do next.
      
                      }
                      else {
                          // Hooray, IAB is fully set up!
                          Log.d(TAG, "Hooray, IAB is fully set up!");
                          List additionalSkuList = new ArrayList();
                          additionalSkuList.add(PURCHASE_PRODUCT_ID);
      
                          mHelper.queryInventoryAsync(true, additionalSkuList, mQueryFinishedListener);
                      }
                  }
              });
          }
      
          IabHelper.QueryInventoryFinishedListener
                  mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
              public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
      
                  if (result.isFailure()) {
                      // handle error
      
      
                      return;
                  } else {
      
                      String price =
                              inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getPrice();
                      String title =
                              inventory.getSkuDetails(PURCHASE_PRODUCT_ID).getTitle();
                      boolean isPurchased = inventory.hasPurchase(PURCHASE_PRODUCT_ID);
                      // update the UI
                      Log.d(TAG, "product price = " + price + "  title = " + title);
                      if (!isPurchased) {
                          mHelper.launchPurchaseFlow(InAppBillingActivity.this, PURCHASE_PRODUCT_ID, PURCHASE_REQUEST_CODE,
                                  mPurchaseFinishedListener, "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
                      } else {
      
                          //product is already purchased
      
          IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
                  = new IabHelper.OnIabPurchaseFinishedListener() {
              public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
      
                  if (result.isFailure()) {
                      Log.d(TAG, "Error purchasing: " + result);
      
                      return;
                  } else if (purchase.getSku().equals(PURCHASE_PRODUCT_ID)) {
      
      
                      MyUtility.setAdsFreeVersion(InAppBillingActivity.this, true);
                      Log.d(TAG, "user has purchased " + purchase.getSku());
      
                  }
      
              }
          };
      
      
      
      
          @Override
          public void onDestroy() {
              super.onDestroy();
              //unbind from the In-app Billing service when you are done with your activity
              if (mHelper != null) mHelper.dispose();
              mHelper = null;
          }
      
          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
              if (mHelper == null) return;
      
              // Pass on the activity result to the helper for handling
              if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
                  // not handled, so handle it ourselves (here's where you'd
                  // perform any handling of activity results not related to in-app
                  // billing...
                  super.onActivityResult(requestCode, resultCode, data);
              } else {
                  Log.d(TAG, "onActivityResult handled by IABUtil.");
              }
          }
      

      You have successfully implemented In-app billing service.
      To Test In-app billing service click here
      Note: We have not implemented how to consume a purchased product in this blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: