in-app purchases for android
By,
Ningan
Contents
- What is In-App Purchase?
- Types of in-app purchases supported in Android
- Setup of In-App Purchases in android application
- Testing of In-App Billing
What is in-app purchase?
- Its a service provided by google, that lets you sell digital content from inside of your application.
Examples for digital contents are: In-game currency,
application upgrade & new content of your application.
- No special account or registration is required other than Google Play Developer Console account & a Google Wallet merchant account.
- In-app Billing can be implemented only in applications that you publish through Google Play.
types of in-app billing
These are of two types:
1) Standard In-App Products
* These are one time billing products.
2) Subscriptions
* These are recurring or automated billing products.
in-app purchase set up
Prerequisites:
- We need to have Google Play Developer Console account.
- Google Wallet Merchant account to create a product list & issue refunds to users if necessary.
- Installing the Google Play Billing Library:
Window> Android SDK Manager > Extras > Google Play Billing Library.
It will be installed in:
<Android SDK path>/extras/google/play_billing
- The IInappBillingService.aidl file which needs to be included in the application which wants In-App Billing service.
Setting up of sample application:
-
Add the billing permission to AndroidManifest:
<uses-permission android:name="com.android.vending.BILLING" />
- Create a package com.android.vending.billing in src folder & then add IInAppBillingServices.aidl file to it.
-
Adding utility classes to the application:
* These are some of the classes which are provided as part of billing library, to implement In-App billing easier.
* Create a package, for Example, <domain>.util & then add all the util classes which are available in TrivialDrive sample application.
- Then create a ServiceConnection & bind it to IInAppBillingService.
IInAppBillingService mService;ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service) {
mService = IInAppBillingService.Stub.asInterface(service);
}
};
This is done by IabHelper class in the sample project in two steps:
IabHelper mHelper;
public void onCreate(Bundle savedInstanceState) {
String base64EncodedPublicKey;
// compute your public key and store it in base64EncodedPublicKey
mHelper = new IabHelper(this, base64EncodedPublicKey);
}
Then perform the service binding by calling startSetup on the IabHelper instance:
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);
}
// Hooray, IAB is fully set up!
}
});
- Send In-App Billing requests(Item purchasing) to IInAppBillingService:
Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
sku, "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
Note: Purchase types > "inapp" or "subs"
- Handle In-App Billing response from Google Play.
-
Querying for purchased items:
Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);
- To unbind & free your system resources, call IabHelper's dispose method when your activity gets destroyed.
@Override
public void onDestroy() {
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
testing in-app billing
Since the google play does not allow to use developer account to directly purchase an in-app product, it provides following two ways to test the in app billing.
1) Test with your own product IDs:
* To create test account, login to developer console & then
enter a valid gmail account.
Developer console > Settings > account details > Licence Testing > test account
* Then build signed apk of your app & then distribute it to the device, where test account is the primary account.
* Finally test all purchases as it wont be charged.
2) Test with static responses:
There are 4 test product IDs, which are provided by google for testing In-App Billing early in development stage.
i) android.test.purchased
ii) android.test.cancelled
iii) android.test.refunded
iv) android.test.item_unavailable
creating new in-app product
Android in-a
By Torry Harris Business Solutions
Android in-a
- 940