Intro:
In-app Billing is a Google Play service that lets you sell digital content from inside your applications. You can use the service to sell a wide range of content, including downloadable content such as media files or photos, virtual content such as game levels or potions, premium services and features, and more.
Steps to integrate into an Android Application:
1. Create sign apk for your application.
2. Upload your apk on Google play store.
3. Create product for your application.
4. Wait for 6-12 hour for update item’s on Play store.
5. Copy Key of your Google account and paste it into BillingSecurity
String base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE";
6. Give Billing permissions in Manifest.xml
<uses-permission android:name="com.android.vending.BILLING" />
7. Install the Google Play Billing Library of Extrafolder in Sdk.
8. Adding the InAppBillingService.aidl File to the Project.
9. Adding the Utility Classes to the Project
Eg: <sdk path>/extras/google/play_billing/samples/TrivialDrive
10. Google Play Developer Console and Google Wallet Accounts
Each application developer making use of Google Play billing must be identified by a unique public license key. The only way to obtain a public license key is to register an application within the Google Play Developer Console. If you do not already have a Google Play Developer Console account, go to http://play.google.com/apps/publish and follow the steps to register as outlined in the chapter entitled Signing and Preparing an Android Application for Release.
11. Obtaining the Public License Key for the Application
From the home page of the Google Play Developer Console, click on the Add new application button, specifying the default language and a title of InAppBilling. Once this information has been entered, click on the Upload APK button
12. Setting Up Google Play Billing in the Application
String base64EncodedPublicKey = "<your license key here>";(Already mentioned above )
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " +
result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
13. Initiating a Google Play In-app Billing Purchase
eg:InAppBillingActivity.java file and adding the code for the buyClick method so that it reads as follows
public void buyClick(View view) {
mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001,
mPurchaseFinishedListener, "mypurchasetoken");
}
14. Implementing the onActivityResult Method
When the purchasing process returns, it will call a method on the calling activity named onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data)
{
if (!mHelper.handleActivityResult(requestCode,
resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
15. Implementing the Purchase Finished Listener
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener
= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,
Purchase purchase)
{
if (result.isFailure()) {
// Handle error
return;
}
else if (purchase.getSku().equals(ITEM_SKU)) {
consumeItem();
buyButton.setEnabled(false);
}
}
};
16. Consuming the Purchased Item
public void consumeItem() {
mHelper.queryInventoryAsync(mReceivedInventoryListener);
}
IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener
= new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
if (result.isFailure()) {
// Handle failure
} else {
mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
mConsumeFinishedListener);
}
}
};
As with the query, the consumption task is also performed asynchronously and, in this case, is configured to call a listener named mConsumeFinishedListener when completed. This listener now needs to be implemented such that it enables the “Click Me!” button after the item has been consumed in the billing system:
IabHelper.OnConsumeFinishedListener mConsumeFinishedListener =
new IabHelper.OnConsumeFinishedListener() {
public void onConsumeFinished(Purchase purchase,
IabResult result) {
if (result.isSuccess()) {
clickButton.setEnabled(true);
} else {
// handle error
}
}
};
17. Releasing the IabHelper Instance
Remaining in the InAppBillingActivity.java file, override the onDestroy() activity lifecycle method as follows:
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}
18. Modifying the Security.java File
public static boolean verifyPurchase(String base64PublicKey,
String signedData, String signature) {
if (TextUtils.isEmpty(signedData) ||
TextUtils.isEmpty(base64PublicKey) ||
TextUtils.isEmpty(signature)) {
Log.e(TAG, "Purchase verification failed: missing data.");
if (BuildConfig.DEBUG) {
return true;
}
return false;
}
PublicKey key = Security.generatePublicKey(base64PublicKey);
return Security.verify(key, signedData, signature);
}
Reference:
Techotopia - Google Play In-app Billing
Javacodegeeks
0 comments:
Post a Comment