SlideShare uma empresa Scribd logo
1 de 10
“iOS and Android Mobile application Development Company in India” www.letsnurture.com
How to setup In App purchase in
Mobile Application Development
“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:
“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);
“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) {
“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;
}
“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;
“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) {
“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);
“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;
}
“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.

Mais conteúdo relacionado

Destaque

เรื่องประทับใจให้ข้อคิด 3
เรื่องประทับใจให้ข้อคิด 3เรื่องประทับใจให้ข้อคิด 3
เรื่องประทับใจให้ข้อคิด 3Na Tak
 
Introduction to Advanced Product Options
 Introduction to Advanced Product Options  Introduction to Advanced Product Options
Introduction to Advanced Product Options Ketan Raval
 
Ugg: The Beach Boots
Ugg: The Beach BootsUgg: The Beach Boots
Ugg: The Beach Bootsviegrace
 
Anything’s Possible
Anything’s PossibleAnything’s Possible
Anything’s Possiblecharliecl500
 
Marshall Cassidy-ugly-like-me-lyrics
Marshall Cassidy-ugly-like-me-lyricsMarshall Cassidy-ugly-like-me-lyrics
Marshall Cassidy-ugly-like-me-lyricsVOCAL SPIN
 
Magento store-locator
Magento store-locatorMagento store-locator
Magento store-locatorKetan Raval
 
Band and artists picture analysis
Band and artists picture analysisBand and artists picture analysis
Band and artists picture analysisSalfordmedia
 
Pearson CiTE 2012 OpenClass Presentation
Pearson CiTE 2012 OpenClass PresentationPearson CiTE 2012 OpenClass Presentation
Pearson CiTE 2012 OpenClass PresentationChristopher Rice
 
Introduction to OM
Introduction to OMIntroduction to OM
Introduction to OMkahogan62
 
Few Project Management Tips
Few Project Management TipsFew Project Management Tips
Few Project Management TipsKetan Raval
 
Advanced Social Media Techniques in Higher Education
Advanced Social Media Techniques in Higher EducationAdvanced Social Media Techniques in Higher Education
Advanced Social Media Techniques in Higher EducationChristopher Rice
 
Developing Performance Based Work Statements
Developing Performance Based Work StatementsDeveloping Performance Based Work Statements
Developing Performance Based Work Statementskahogan62
 
มิตรภาพ
มิตรภาพมิตรภาพ
มิตรภาพNa Tak
 

Destaque (16)

เรื่องประทับใจให้ข้อคิด 3
เรื่องประทับใจให้ข้อคิด 3เรื่องประทับใจให้ข้อคิด 3
เรื่องประทับใจให้ข้อคิด 3
 
Introduction to Advanced Product Options
 Introduction to Advanced Product Options  Introduction to Advanced Product Options
Introduction to Advanced Product Options
 
Ugg: The Beach Boots
Ugg: The Beach BootsUgg: The Beach Boots
Ugg: The Beach Boots
 
The universe
The universeThe universe
The universe
 
La llegenda de Sant jordi
La llegenda de Sant jordiLa llegenda de Sant jordi
La llegenda de Sant jordi
 
Arval slide
Arval slideArval slide
Arval slide
 
Anything’s Possible
Anything’s PossibleAnything’s Possible
Anything’s Possible
 
Marshall Cassidy-ugly-like-me-lyrics
Marshall Cassidy-ugly-like-me-lyricsMarshall Cassidy-ugly-like-me-lyrics
Marshall Cassidy-ugly-like-me-lyrics
 
Magento store-locator
Magento store-locatorMagento store-locator
Magento store-locator
 
Band and artists picture analysis
Band and artists picture analysisBand and artists picture analysis
Band and artists picture analysis
 
Pearson CiTE 2012 OpenClass Presentation
Pearson CiTE 2012 OpenClass PresentationPearson CiTE 2012 OpenClass Presentation
Pearson CiTE 2012 OpenClass Presentation
 
Introduction to OM
Introduction to OMIntroduction to OM
Introduction to OM
 
Few Project Management Tips
Few Project Management TipsFew Project Management Tips
Few Project Management Tips
 
Advanced Social Media Techniques in Higher Education
Advanced Social Media Techniques in Higher EducationAdvanced Social Media Techniques in Higher Education
Advanced Social Media Techniques in Higher Education
 
Developing Performance Based Work Statements
Developing Performance Based Work StatementsDeveloping Performance Based Work Statements
Developing Performance Based Work Statements
 
มิตรภาพ
มิตรภาพมิตรภาพ
มิตรภาพ
 

Mais de Ketan Raval

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Ketan Raval
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Ketan Raval
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is hereKetan Raval
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyondKetan Raval
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected worldKetan Raval
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wantedKetan Raval
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKetan Raval
 
Android notifications
Android notificationsAndroid notifications
Android notificationsKetan Raval
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantKetan Raval
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changerKetan Raval
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyKetan Raval
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsKetan Raval
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guideKetan Raval
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integrationKetan Raval
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google WayKetan Raval
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS applicationKetan Raval
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS appKetan Raval
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputingKetan Raval
 
Beta testing guidelines for developer
Beta testing guidelines for developerBeta testing guidelines for developer
Beta testing guidelines for developerKetan Raval
 

Mais de Ketan Raval (20)

Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)Amazon Alexa Auto Software Development Kit (SDK)
Amazon Alexa Auto Software Development Kit (SDK)
 
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
Proximity Marketing Solutions enhancing Businesses leveraging iBeacon SDK Int...
 
Keynote 2016
Keynote 2016Keynote 2016
Keynote 2016
 
Zero ui future is here
Zero ui   future is hereZero ui   future is here
Zero ui future is here
 
Android n and beyond
Android n and beyondAndroid n and beyond
Android n and beyond
 
IoT and Future of Connected world
IoT and Future of Connected worldIoT and Future of Connected world
IoT and Future of Connected world
 
#Instagram API Get visibility you always wanted
#Instagram API   Get visibility you always wanted#Instagram API   Get visibility you always wanted
#Instagram API Get visibility you always wanted
 
Keynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG AhmedabadKeynote - Devfest 2015 organized by GDG Ahmedabad
Keynote - Devfest 2015 organized by GDG Ahmedabad
 
Android notifications
Android notificationsAndroid notifications
Android notifications
 
How to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA CompliantHow to make your Mobile App HIPPA Compliant
How to make your Mobile App HIPPA Compliant
 
3 d touch a true game changer
3 d touch a true game changer3 d touch a true game changer
3 d touch a true game changer
 
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel EconomyOBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
OBD Mobile App - Fault Codes, Driving Behaviour and Fuel Economy
 
Vehicle to vehicle communication using gps
Vehicle to vehicle communication using gpsVehicle to vehicle communication using gps
Vehicle to vehicle communication using gps
 
Obd how to guide
Obd how to guideObd how to guide
Obd how to guide
 
Garmin api integration
Garmin api integrationGarmin api integration
Garmin api integration
 
Beacon The Google Way
Beacon The Google WayBeacon The Google Way
Beacon The Google Way
 
Edge detection iOS application
Edge detection iOS applicationEdge detection iOS application
Edge detection iOS application
 
Google calendar integration in iOS app
Google calendar integration in iOS appGoogle calendar integration in iOS app
Google calendar integration in iOS app
 
Big data cloudcomputing
Big data cloudcomputingBig data cloudcomputing
Big data cloudcomputing
 
Beta testing guidelines for developer
Beta testing guidelines for developerBeta testing guidelines for developer
Beta testing guidelines for developer
 

Último

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 

Último (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
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.