Learn How to setup In App purchase in Mobile Application Development with code example.Using this document you can easily setup In App Purchase setup in mobile application development.
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
In App Purchase in an Android App By LetsNurture
1. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
How to setup In App purchase in
Mobile Application Development
2. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
In app purchase Tutorial
Follow Simple Steps to add In app purchase in Project.
Note: To use the In-app Billing Version 3 features, you must add the
IInAppBillingService.aidl file to your Android project. This Android Interface
Definition Language (AIDL) file defines the interface to the Google Play
service.
Step 1:
You can find the IinAppBillingService.aidl file in the provided sample app.
To add the In-app Billing Version 3 library to your existing In-app Billing
project:
1.Copy the IinAppBillingService.aidl file to your Android project.
If you are using Eclipse: Import the IinAppBillingService.aidl file into
your /src directory.
2.Build your application. You should see a generated file named
IinAppBillingService.java in the /gen directory of your project.
3.Add the helper classes from the /util directory of the SampleProject sample
to your project. Remember to change the package name declarations in those
files accordingly so that your project compiles correctly.
Your project should now contain the In-app Billing Version 3 library.
Step 2:
3. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
Set the Billing Permission
--app needs to have permission to communicate request and response messages
to the Google Play’s billing service. To give your app the necessary permission,
add this line in your AndroidManifest.xml manifest file:
<uses-permission android:name="com.android.vending.BILLING" />
Step 3:
Connect with Google Play.
To set up synchronous communication with Google Play, create an IabHelper
instance in your activity's onCreate method. In the constructor, pass in the
Context for the activity, along with a string containing the public license key
that is your apps base64 key retrieved from play store console. When you are
using in testing mode at that time no need to provide base64 key but needs to
define it.
// The helper object in app-billing object
IabHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init view
text = (TextView) findViewById(R.id.text);
btn_purchase = (Button) findViewById(R.id.btn_purchase);
apple = (ImageView) findViewById(R.id.imageView1);
4. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
// init in app purchase object.
Log.e(TAG, "Creating IAB helper.");
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(mSetupListner);
btn_purchase.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// do call purchase
String payload = "";
mHelper.launchPurchaseFlow(MainActivity.this, SKU_GAS,
RC_REQUEST, mPurchaseFinishedListener, payload);
}
});
}
Now bing service by calling mHelper.startSetup(mSetupListner); method on the
IabHelper instance that you created. Pass the method an
OnIabSetupFinishedListener instance, which is called once the IabHelper
completes the asynchronous setup operation. Here we also check if the in-app
billing is supported or not If the API version is not supported, or if an error
occurred while establishing the service binding, the listener is notified and
passed an IabResult object with the error message.
// setup listener for in app.
IabHelper.OnIabSetupFinishedListener mSetupListner = new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
5. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
Log.d(TAG, "Setup finished.");
if (!result.isSuccess()) {
Log.e("Problem setting up in-app billing: ", result + "//");
return;
}
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null)
return;
// IAB is fully set up. Now, let's get an inventory of stuff we own.
Log.e(TAG, "Setup successful. Querying inventory.");
mHelper.queryInventoryAsync(mGotInventoryListener);// Query Items
}
};
Note: Remember to unbind from the In-app Billing service when you are done
with activity. If we don’t unbind, the open service connection could cause your
device’s performance to degrade.
@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null)
mHelper.dispose();
mHelper = null;
}
6. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
Step 4:
Query Items Available for Purchase
Query Google Play programmatically to retrieve details of the in-app products
that are associated with app.
To retrieve the product details, call
queryInventoryAsync(QueryInventoryFinishedListener) on your IabHelper
instance.
This is useful, for example, when you want to display a listing of unowned
items that are still available for purchase to users. And restore owned
items that already purchased.
The following code shows how you can retrieve the details for products with Id
SKU_APPLE that you previously defined in the Developer Console.
SKU_APPLE contains your apps product Ids
// to check whether user already purchased or not to recover product.
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new
IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result,
Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
// Have we been disposed of in the meantime? If so, quit.
if (mHelper == null)
return;
7. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
// Is it a failure?
if (result.isFailure()) {
Log.e("Failed to query inventory: ", result + "//");
return;
}
Log.e(TAG, "Query inventory was successful.");
/*
* Check for items we own. Notice that for each purchase, we check
* the developer payload to see if it's correct! See
* verifyDeveloperPayload().
*/
// Check for Purchase apple -- if we own apple, we should
Purchase gasPurchase = inventory.getPurchase(SKU_APPLE);
if (gasPurchase != null) {
Log.e(TAG, "We own this item invetry ");
isPurchsed = true;
}
updateUi();
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
return;
}
};
check product is purchased or not and update your apps UI according to
purchase.
// Check for Purchase apple -- if we own apple, we should
Purchase gasPurchase = inventory.getPurchase(SKU_APPLE);
if (gasPurchase != null) {
8. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
Log.e(TAG, "We own this item invetry ");
isPurchsed = true;
}
Step 5:
Purchase an Item
To start a purchase request from your app, call launchPurchaseFlow(Activity,
String, int, OnIabPurchaseFinishedListener, String) on your IabHelper instance.
You must make this call from the main thread of your Activity.
The first argument is the calling Activity.
The second argument is the product ID (also called its SKU) of the item
to purchase. Make sure that you are providing the ID and not the product
name. You must have previously defined and activated the item in the
Developer Console, otherwise it won’t be recognized.
The third argument is a request code value. This value can be any positive
integer. Google Play returns this request code to the calling Activity’s
onActivityResult along with the purchase response.
The fourth argument is a listener that is notified when the purchase
operation has completed and handles the purchase response from Google
Play.
The fifth argument contains a ‘developer payload’ string that you can use
to send supplemental information about an order (it can be an empty
string). Typically, this is used to pass in a string token that uniquely
identifies this purchase request. If you specify a string value, Google Play
returns this string along with the purchase response. Subsequently, when
you make queries about this purchase, Google Play returns this string
together with the purchase details.
Check example here
// do call purchase
String payload = "";
mHelper.launchPurchaseFlow(MainActivity.this, SKU_APPLE,
RC_REQUEST, mPurchaseFinishedListener, payload);
9. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
If the purchase order is successful, the response data from Google Play is stored
in an Purchase object that is passed back to the listener
mPurchaseFinishedListener.
The following example shows how you can handle the purchase response in the
listener, depending on whether the purchase order was completed successfully,
check which product is purchased and make change accordingly in app
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new
IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
Log.d(TAG, "Purchase finished: " + result + ", purchase: "
+ purchase);
// if we were disposed of in the meantime, quit.
if (mHelper == null) {
isPurchsed = false;
return;
}
if (result.isFailure()) {
Log.e("Error purchasing: ", result + "//");
isPurchsed = false;
return;
}
Log.e(TAG, "Purchase successful.");
if (purchase.getSku().equals(SKU_APPLE)) {
Log.e(TAG, "Purchase own item purchase lisner");
isPurchsed = true;
}
10. “iOS and Android Mobile application Development Company in India” www.letsnurture.com
updateUi();
}
};
It is good practice to update the UI immediately so that your users can see their
newly purchased items.
We must have to check every time that which product is purchased to make
update.
Test app with test ids android.test.purchased instead of productId.