Monday 2 March 2015

Integrating Your App with the Google API



Google Play offers great online help to help you implement in-app billing in your application. A good
place to start is with its sample application, which gives you a working model to start from. To get
started with its sample application, go here:

http://developer.android.com/training/in-app-billing/preparing-iab-app.html#GetSample
The old version 2.0 API included a sample app called Dungeons. you can see the
purchase screen from that app, illustrating how you can purchase a potion (“Puts dragons to sleep.”).




If at all possible, you should be integrating with the newer and simpler version 3.0 API, which uses a
different sample app called Trivial Drive




As far as enabling in-app billing in your own application, the following sections spell out the steps
to follow, beginning with downloading the in-app billing library. (We assume you have elected to use
version 3.0 of the API.)


Enabling Your App to Use In-App Billing

To download the in-app billing library, open the Android SDK Manager. Expand the Extras section,
select Google Play Billing Library, and install the Library.

Before you can use in-app billing, you must include a IinAppBillingService.aidl file in your project.
This file is an Android Interface Definition Language file that defines the interface to Google’s billing
service.

First, right-click the src directory of your project and select New ➤ Package. Name your new
package com.android.vending.billing. Move the IinAppBillingService.aidl class (found in
<sdk>/extras/google/play_billing/ as well as the previous sample app provided with the billing
library) into this package.
Add your product to the Google Play Developer Console. To do this, select your application in the
Developer Console and then select the In-app Products tab on the left side. You can then add a
new product. The In-app Product ID must be unique in your application’s namespace. Your product
type can be a Managed per user account, Unmanaged, or Subscription. You also need to add a
description and price.


Initial Setup For In-App Billing In Your App
You can follow the detailed instructions here for the setup process: http://developer.android.com/
training/in-app-billing/preparing-iab-app.html#GetSample
To summarize, you need to give your app permission to interact with the billing service via your
manifest file. You also need to create an Iabhelper, which is the in-app billing helper class. This
class enables a simplified synchronous style of communication that is one of the benefits of version 3.0 of the in-app billing API. The Iabhelper class uses callbacks to communicate with your code. In fact, the setup process uses an onIabSetupFinished callback function to return success or failure.


Using In-App Billing: Requesting a List of Items For Purchase

Again, the process is described here in great detail:
https://developer.android.com/google/play/billing/billing_integrate.html
At a high level, you need to build a list of purchasable items that you are querying. The previous link
uses this example:

List additionalSkuList = new List();
additionalSkuList.add(SKU_APPLE);
additionalSkuList.add(SKU_BANANA);
inAppBillingHelper.queryInventoryAsync(true, additionalSkuList,
mQueryFinishedListener);


As you might expect, the mQueryFinishedListener is a callback function that is called with a list of
available inventory, including their prices.

Using In-App Billing: Making the Purchase

Similarly, making a purchase involves a call the in-app billing helper, and the response is handled
with a callback listener function:

inAppBillingHelper.launchPurchaseFlow(this, SKU_APPLE, REQUEST_CODE_VALUE,
mPurchaseFinishedListener, "developerPayloadString");

SKU_APPLE is the item to be purchased. The REQUEST_CODE_VALUE is a positive integer that will
be returned back to the caller. The developer payload string is a convenience string for use by
developers to send supplemental information. It may be empty.

Using In-App Billing: Determining Which Items Have Already Been Purchased

Continuing the paradigm, you may determine which items have already been purchased with a query
to the in-app billing helper that is captured by a callback listener:

inAppBillingHelper.queryInventoryAsync(mGotInventoryListener);

You need to determine which items have been purchased each time your app restarts so your users
have access to capabilities that they have already purchased.

Using In-App Billing: Consumable Purchases

To consume an item that your user has purchased, you call the in-app billing helper as follows:

inAppBillingHelper.consumeAsync(inventory.getPurchase(SKU_APPLE),
mConsumeFinishedListener);


Integrating Your App with the Amazon API

If you’ve played around with the Google Play in-app billing sample application for version 2.0 of its
API (the Dungeons program), you have a great foundation to start learning how to integrate with the
Amazon API. This link teaches you how to modify the Dungeons application to be compatible
with the Amazon in-app purchasing API:
https://developer.amazon.com/sdk/in-app-purchasing/reference/google-to-iap.html
Amazon claims that its IAP solution requires less work and has a shorter development cycle. Note,
however, that Amazon is not comparing its solution with the newer version 3.0 API.




Enabling Your App to Use In-App Billing

Download the Amazon Mobile App SDK package from here: https://developer.amazon.com/sdk.html
Extract the ZIP file into a directory on your computer. Within this directory, you find an /In-App-
Purchasing/lib folder. This folder contains a JAR file called in-app-purchasing, which is the in-app
purchasing library and must be added to your eclipse project’s library path. You can do this by
accessing the Project Properties menu and going to the Java Build Path. There you can select the
Libraries tab and add the JAR file.
Before you can purchase a product from within an app, you must add your product to Amazon’s
distribution portal by signing into the Distribution Portal and selecting the My Apps tab. Choose the
Manage In-App Items option from the app drop-down menu. Now you must choose whether you
will be creating a Consumable, Entitlement, or Subscription product. You also need to add a price,
description, and thumbnail image.



Initial Setup For In-App Billing In Your App

As you might expect, your application must be given access to the Amazon in-app purchasing
library. The purchasing library is implemented as a broadcast receiver, and you declare it in your
manifest by adding the following to the <application> section:


<receiver android:name = "com.amazon.inapp.purchasing.ResponseReceiver" >
<intent-filter>
<action android:name = "com.amazon.inapp.purchasing.NOTIFY"
android:permission = "com.amazon.inapp.purchasing.Permission.NOTIFY" />
</intent-filter>
</receiver>

This class initiates all in-app billing requests. To capture callbacks from the PurchasingManager, you
must create a PurchasingObserver class and register it with the PurchasingManager. To do this, you
subclass BasePurchasingObserver. (We will discuss each of the methods that need to be subclassed
later in the chapter.) The registration with PurchaseManager occurs in your onStart and looks like this:
PurchasingManager.registerObserver(new MyPurchasingObserver());

You also need to register the user using your app with the Amazon service. 
This also occurs in your
onStart: PurchasingManager.initiateGetUserIdRequest()

You must implement a callback for this request in your PurchasingObserver class:

PurchasingObserver.onGetUserIdResponse(GetUserIdResponse)


Using In-App Billing: Requesting a List of Items for Purchase

Similarly, a call using the PurchasingManager requests the list of items for purchase:

PurchasingManager.initiateItemDataRequest(java.util.Set skus)

You need to implement the following callback in the PurchasingObserver:

PurchasingObserver.onItemDataResponse(ItemDataResponse itemDataResponse)

Using In-App Billing: Making the Purchase

To make a purchase, call the following:
PurchasingManager.initiatePurchaseRequest(java.lang.String sku)

The callback function you need to implement is the following:
PurchasingObserver.onPurchaseResponse(PurchaseResponse purchaseResponse)


Using In-App Billing: Determining Which Items Have Already Been Purchased

The PurchaseResponse field in the callback to the following is set to ALREADY_ENTITLED when an item has already been purchased (of course, the item in question must be an ENTITLEMENT product as set in the distribution portal):
Using In-App Billing: Consumable Purchases
Consumable purchases are simply non-entitled purchases. Once they are bought, the app must
track their use. They can be repurchased at any time.


Supporting In-App Billing with Multiple App Stores

As of this writing, there is no good solution that acts to abstract away the issues of dealing with
multiple app stores for in-app purchasing. However, an open-source project called OpenIAB is in the
works that may change that. OpenIAB is a part of the One Platform Foundation, a global initiative to
help developers submit their apps across multiple alternative app stores.
Although the code is not yet ready for prime time (or even finalized), you should track its
development because once the API is released, it will dramatically simplify the problems of dealing
with multiple in-app purchasing stores. 

You can learn more at the following web sites:

 One Platform Foundation: http://www.onepf.org/
 OpenIAB (Open In-App Billing): http://www.onepf.org/openiab











No comments:

Post a Comment