Hi all,
This demo shows how to do in app purchase for managed products in Android using Version 3 in-app Billing API.
Please Go through this before trying out this Demo
These are the important steps in to remember for doing in app purchase.
1. You have to download the Google In App Billing Library from Google.
For that you have to open the Android SDK Manager and Look for “Google Play Billing Library”.
2. You have to make changes in the Android Manifest first.
Add this permission.
<!-- Permission for In App Purchase --> <uses-permission android:name="com.android.vending.BILLING" />
The above permission will detect that your app contains “In App Purchases” in Google Play Store.
3. Now add the .aidl file that you get while downloading the library from Google.
[You can find this library in the Sample Project at the end of this post].
4. Now let’s assume that we have done the coding part by giving a demo in_app item.
5. You can test the application in two ways either using STATIC RESPONSES from Google Play
OR real purchases with users but without using any money.
STATIC TESTING :
READ THIS LINK : http://developer.android.com/google/play/billing/billing_testing.html#billing-testing-static
REAL TESTING – AFTER UPLOADING TO GOOGLE PLAY (IN ALPHA OR BETA MODE)
READ THIS LINK : http://developer.android.com/google/play/billing/billing_testing.html#testing-purchases.
NOTE : If you publish your application through Alpha or Beta mode, the users who are downloading from Google Play will not see it or get it. In that way Google ensures that it is not published before testing.
For REAL TESTING AFTER UPLOADING TO GOOGLE PLAY you must be adding some users as Test Users who can test this app that you have
You can add test users while you upload the apk with in app Billing in Alpha or Beta Mode.
For adding test users you can see a link in the APK page of the particular app in Alpha or Beta Mode.[This is usually a Google community or Groups]
For a user to be a Test account you have to go to Settings > Gmail accounts with testing access > add the Gmail account to test.
This means that these users can buy this in app Item without loosing any money in alpha or Beta Mode.
Important Note :
In Google In App Billing V3, all purchases are managed that means you can even consume managed products.
PRODUCTS ONCE CONSUMED WILL BE A AVAILABLE FOR PURCHASE AGAIN.
THAT MEANS IF YOU PURCHASE A MANAGED PRODUCT AND IF THE USER CONSUMES IT, THEN IT WILL BE AGAIN AVAILABLE FOR PURCHASE.
IF THE USER CONSUMES IT GOOGLE WILL REMOVE THAT PURCHASED ITEM FROM IT’S SEVER AND MARK IT AS ‘UN-OWNED’.
SO WHEN YOU QUERY THE PURCHASED ITEMS YOU WILL NOT BE GETTING THE PURCHASED ITEM.
SO IF YOU WANT THE USER TO PURCHASE THE ITEM ONLY ONCE AND AVAILABLE ALL THE TIME DON’T CONSUME IT
THAT MEANS YOU HAVE TO COMMENT OUT THIS CODE FROM THE SAMPLE CODE PROVIDED.
mHelper.consumeAsync(purchase, mConsumeFinishedListener);
That is you can directly write your code inside this if the purchase is sucessful
if (purchase.getSku().equals(SKU_INAPPITEM)) { }
OK Done.
You can download the sample application from this link
Download In App Billing Source Code
You can also check this post for more simplified example.
Hey,
Is this using latest Google billing library?
I want to implement a one time purchase in android. Is this the current and updated approach to do so ?
Hi Dude,
Yes, this is the latest Google Billing Libary (v3).
This is the approach for implementing a “One Time Purchase”.
Thanks
James
Hey thanks for prompt reply 🙂
Thanks for example 😀
Pingback: IN App Purchase - Android - Simplified source code
What’s up it’s me, I am also visiting this site regularly, this website is in fact
pleasant and the visitors are truly sharing pleasant thoughts.
can you please explain how to do subscription purchase for app
uncomment the purchase consume line from the code, then it becomes a subscription.