Tuesday, March 26, 2013

How to Inegrate In App Purchase Billing in Android : In App Purchase Demo In Android : Billing Services in Android : Payment Getaway in Android

Hello Friends!
Today I am going to share very useful blog for In App Purchase in Android. Google provide In App Billing faculty in Android. In App Purchase is a very easy and secure way for make payment online.
Please follow my blog step by step-

Screen Shot:



1)Create a new Project in Android.
2)Create MainActivity.java class.
3)Add activity_main.xml in your res/layout folder.
4)Add Billing services and permission in Manifest.xml.

                   Do's

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 store.
5)Copy Key of your Google account and paste it into BillingSecurity.java class Line number 135-
String base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE";
6)Give Billing permissions in Manifest.xml
7)Add IMarketBillingService.java in com.android.vending.billing package.

                   Don't

1)Don't use emulator for testing its does not support Billing Services.
2)Don't use unsigned apk for Billing services.
3)Don't share your key with any one.

My Code-

1)MainActivity.java

package com.manish.inapppurchase;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
Button btn1, btn2, btn3;
private Context mContext=this;
private static final String TAG = "Android BillingService";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);

startService(new Intent(mContext, BillingService.class));
       BillingHelper.setCompletedHandler(mTransactionHandler);
}
public Handler mTransactionHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

if(BillingHelper.latestPurchase.isPurchased()){
showItem();
}
};

};
@Override
public void onClick(View v) {
if (v == btn1) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn1.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "Shirt Button", Toast.LENGTH_SHORT).show();
}
if (v == btn2) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn2.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "TShirt Button", Toast.LENGTH_SHORT).show();
}
if (v == btn3) {
if(BillingHelper.isBillingSupported()){
BillingHelper.requestPurchase(mContext, "android.test.purchased");
       } else {
        Log.i(TAG,"Can't purchase on this device");
        btn3.setEnabled(false); // XXX press button before service started will disable when it shouldnt
       }
Toast.makeText(this, "Denim Button", Toast.LENGTH_SHORT).show();
}

}

private void showItem() {
//purchaseableItem.setVisibility(View.VISIBLE);
}

@Override
protected void onPause() {
Log.i(TAG, "onPause())");
super.onPause();
}

@Override
protected void onDestroy() {
BillingHelper.stopService();
super.onDestroy();
}
}

2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099CC"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:background="#FFFFFF"
        android:text="Shirt for 5.4$" />

    <Button
        android:id="@+id/button2"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:text="Tshirt for 7.4$" />

    <Button
        android:id="@+id/button3"
        android:layout_width="150dp"
        android:layout_height="35dp"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:background="#FFFFFF"
        android:text="Denim for 10.7$" />

</RelativeLayout>



3)manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.inapppurchase"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.inapppurchase.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".BillingService" />

        <receiver android:name=".BillingReceiver" >
            <intent-filter>
                <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
                <action android:name="com.android.vending.billing.RESPONSE_CODE" />
                <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

4)Zip Code


For any help or suggestion please comment on my blog :)-

Thanks,

155 comments :

  1. Do you still have the zip file?

    ReplyDelete
    Replies
    1. Yes Mike I have put a Dropbox shared folder link on click of zip code but I think it have some problem so please give me some time I will upload a new one. you can put your email-id here I will send you the zip code if you needed..

      Thanks,

      Delete
    2. You can use mdcarroll6@gmail.com! Thanks!

      Delete
    3. Hi Mike please check your email I have sent you the zip code.Hope it will help you..

      Delete

  2. Hi,

    Recently I came across some great articles on your site.
    The other day, I was discussing (http://www.androidhub4you.com/2013/03/how-to-inegrate-in-app-purchase-billing.html)with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
    Would it be ok to submit the article? It is free of charge, of course!

    Let me know what you think
    Contact me at anelieivanova@gmail.com

    Regards
    Anele Ivanova

    ReplyDelete
  3. Hi Manish,

    I am Mohit. Can you please send me the code on my email ?

    my email : charadva.mohit@gmail.com

    Thanks.

    ReplyDelete
    Replies
    1. Hi Mohit, Download the zip code from my blog-
      Point 4)Zip Code
      https://www.dropbox.com/s/v068uwfzfhhwoez/InAppPurchaseDemo.zip

      Delete
    2. Thanx bro.. Your demo is really usefull to me.. jay Bharat

      Delete
    3. if you don't mind . .
      can you send full code of this demo.. jay bharat

      Delete
    4. i can't understood code of this ..
      BillingHelper.requestPurchase(mContext, "android.test.purchased");

      In my apps use same package "android.test.purchased" or not . .????
      thanx a lot..

      Delete
  4. show force close error on accept and buy button click

    ReplyDelete
    Replies
    1. I think you have not done Google store part(Google Developer). I mean add item on store and publish them that's why you are getting this crash.
      before run this app please done needed things. you can do googling also and its working for me. Please check your side...

      Delete
  5. This is a good in app billing tutorial. Now I will be able to implement it into my application. Thanks

    ReplyDelete
  6. where to link my product inside your demo

    ReplyDelete
    Replies
    1. You can't add product inside my demo. you should take a android developer account it's cost approx 50$. And into your application you can add product it will update after 10 hour approx.

      Delete
  7. Hi, I am developing hotel and restaurant booking app. I have problem in payment part. Could help me how can i do. Please send the code on megha.sutar05@gmail.com
    Please its my humble request for you.

    ReplyDelete
    Replies
    1. Hi Megha! Please check above code everything is there step by step and check option number 4 for zip code.
      And you can use paypal also for you application.

      Thanks,

      Delete
  8. Ok... Thanks alot...
    1 more thing i have did multidate picker dialog in same activity but i am not able to do multitime picker dialog in same activity... Do you have any sample example of multitime picker dialog and fields validation of registration form. If you have then please send me on megha.sutar05@gmail.com

    ReplyDelete
    Replies
    1. Hi megha!

      I did not get meaning of multidate piker..
      Well you can try some of them-
      http://www.androidhub4you.com/2013/05/date-piker-example-in-android-simple.html

      http://www.androidhub4you.com/2013/03/custom-picker-in-android-number-picker.html

      and I have mailed you validation code..

      Thanks,

      Delete
  9. Please send me zip code
    my gmail id rsbijendra@gmail.com

    ReplyDelete
    Replies
    1. Please download it from above post option number-4(zip-code).

      Delete
  10. Thanks Manish, Brilliant work. Just want to know why it is taking to payment again , even after i got payment successful for a particular product. Isn't this case is handled that if a payment made is managed for products which got product type as managed or is there any setting anywhere in the code that we need to handle

    ReplyDelete
    Replies
    1. It is a demo app and I did not manage like this. So please get successes response of payment and hide that particular product.
      In your case It is Okay but if any one want buy again same item then? so please maintain your product and requirement as per your application need.

      Thanks,

      Delete
  11. HI,
    Can u send this zip to vijaymadhu97@gmail.com please

    ReplyDelete
    Replies
    1. Hey please see code again there a download link also..
      Thanks,

      Delete
  12. Hello Manish! You've put a "showItem();" in the

    public Handler mTransactionHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
    Log.i(TAG, "Transaction complete");
    Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState);
    Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId);

    if(BillingHelper.latestPurchase.isPurchased()){
    showItem();
    }
    };

    };

    But that file doesn't exist, even including that drawable the code never works... transaction is ok and i get a RESULT_OK but the drawable never gets visible, you even COMMENT that line in

    private void showItem() {
    //purchaseableItem.setVisibility(View.VISIBLE);
    }

    Any idea how to know when i get RESULT_OK and show wherever i want?!

    Code is PERFECT except for that little part, never get the showItem(); up and running... "BillingHelper.latestPurchase.isPurchased()" looks like never working.

    Hope you can help me with this. Thanks in advance!

    ReplyDelete
    Replies
    1. No no it is just a demo project provide by android just search on google for more help..

      Delete
  13. hi manish i used your project working well but when i integrate this project in my apps its give me null signature i cont understands please help me .

    ReplyDelete
    Replies
    1. Hi Sujeet, I can't help without code here or without log-cat..
      Have you check your App-Key?
      and have you declare all thing in Manifest? and Internet permission?
      please check your log-cat and googling for it..

      Delete
  14. thanks manish ,
    its working very well with your demo but when i used your packages and classes in our project then its give me signature null and give me arrayindexoutofbound exception

    ReplyDelete
    Replies
    1. have you copy com.android.vending.billing package?
      and one more thing check your manifest-




      In my case my pckage was default so i used ..BillingService only but in your case your parent pacakage is diffrent so call it with full name like-
      android:name="com.manish.inapppurchase.BillingService "

      Hope it will help you..

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Hi Manish,

      I got the same issue. It was working fine for a while, but dont know what went wrong. The Helper class started throwing out of bound exceptions.

      I have rechecked my manifest file and I have the vending package.

      Log:

      07-18 23:30:08.159 17040-17040/? I/BillingService: signedData: {"nonce":-9223021552019373094,"orders":[{"notificationId":"android.test.purchased","orderId":"transactionId.android.test.purchased","packageName":"com.myexample.android","productId":"android.test.purchased","purchaseTime":135169194838,"purchaseState":0}]}
      07-18 23:30:08.189 11959-11959/? D/Finsky: [1] MarketBillingService.sendResponseCode: Sending response RESULT_OK for request 9013055853120068051 to com.myexample.android.
      07-18 23:30:08.199 17040-17040/? D/AndroidRuntime: Shutting down VM
      07-18 23:30:08.199 17040-17040/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
      07-18 23:30:08.219 17040-17040/? E/AndroidRuntime: FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start receiver com.myexample.android.inapppurchase.BillingReceiver: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
      at android.app.ActivityThread.handleReceiver(ActivityThread.java:1826)
      at android.app.ActivityThread.access$2400(ActivityThread.java:121)

      Delete
  15. hi manish

    i have an problem with service in this demo problem is that "BillingHelper not fully instantiated" when back to service and just start service again

    Hope you can help me with this. Thanks in advance!

    ReplyDelete
  16. Hi Manish,

    This helped me a lot.

    Can you please send a sample code of TCP and UDP connection on client side coding.
    If you have plz send me to srivatsava.arjun@gmail.com

    Thanks

    ReplyDelete
  17. Do you have hotel and restaurant booking android app???
    Can you send me that with full coding. Please...

    ReplyDelete
    Replies
    1. No megha sorry no code.. my all projects for client,and i can't leak their code and idea...

      Delete
  18. Hi Manish, Is it compulsory to register items in google play account from which we are going to publish our app or it can be from any account?? Does it really takes at least 6-8 hours to get items updated on account?? Waiting for reply. Thanks

    ReplyDelete
    Replies
    1. No no not compulsory in google doc they have mention it maxmium time.. may be it take only one hour..

      Delete
    2. Your welcome mitesh! And yes same account needed for add item... you can test it using draft sign apk in your account..

      Delete
  19. Dear Manish,

    Thanks for the post. Really appreciate your attitude to share your knowledge to others. It would be great if you could give a brief explanation of the flow using your example. Something like a flow diagram, how different classes communicate and what are their roles in performing an in-app purchase.

    Thanks
    rahul

    All the best..keep rocking

    ReplyDelete
    Replies
    1. Okay sure raauu I will mind it next time..
      Thanks for your nice suggestion..

      Delete
  20. hi Manish can you give me some working code for Restoring functionality too .i am working for that from last 20 days but couldn't get ride of it please help

    ReplyDelete
    Replies
    1. Sorry Ashish I don't have code for Restore purchase..
      Try this-
      mBillingService.restoreTransactions();

      http://developer.android.com/google/play/billing/billing_integrate.html

      install sample code from here it will help you..

      Delete
  21. Hello Manish
    I used your sample code and your guide and i integrate it with my current project.
    but i want to validate on my server not on device so in verify method i call httppost method and pass json which contains signature and publick key and purchase data string..i used recommended method in php to verify transaction.
    but it is always 0 but if you check on device verification is true..
    both having same algorithm technique (sh1withrsa) same parameter but on server it false but on device its true...
    do you have any idea what is wrong in my steps.

    one more thing is that I am using public key which is generated before your code integration and i am using same public key but same public key validates true on device..
    waiting for reply
    thank you

    ReplyDelete
    Replies
    1. How this is possible?
      Have you printed it in log before send it on server? I think problem in setting value HTTP param..
      I think it is pick Boolean default value false, not getting from server. So please check it where you set it in your code..

      Delete
    2. Yes i log the all three string in log cat..and i am getting values also i convert it into json and send it by httppost... and he also getting same value on server public key , data , signature and he call verify_ssl (something like that because i am not php developer) so according to you php developer is making some mistake in http param?
      And One more thing is that i skip your verification code and into that verify method i create json and call http post method to send 3 values to my server than my server response me 400 (which is false) and than my app display Log that "Signature does not match data" And After that i got Unable to start Receiver exception .. Null pointer Exception. can you help me please

      Delete
  22. Great work man,

    I have got a question on this,how will I test If I integrate it within my app,,because for to register in the console,google I have to pay 25$ to register ..Do you know any other way to do it..
    Please reply

    ReplyDelete
    Replies
    1. No there is no any other way to test application.
      You have to pay 25$ to Google or ask to your client for provide their account for configure Google wallet..

      Thanks,

      Delete
    2. Thanks man....
      Can You help me with this,I need to call asynctask in BroadCastReceiver on on Receive.
      But its not allowing me to ...

      Delete
  23. hello Manish.
    I solved that problem..its bacause of php developer made mistake in json at server side..
    I need your help i want to verify with my server and after verification i want to downlaod video file in httpresponse..so i created one asynctask with progress bar..but the issue is i called my async task in verify method of billing security and i pass the context from billing service for progress bar..but i got windows.token null exception..for progress bar...
    Can you tell how can i call my asynctask with progres bar from your service?
    waiting for reply
    thank you

    ReplyDelete
    Replies
    1. I can't say anything with out code front of me but i think we can't create progress bar in service class. So try other way search on stackoverflow or ask question on stack overflow it will help you..

      Thanks,

      Delete
  24. Awesome work dude
    Just want to know How can i restore purchase by using your code.
    Have you written any method in your code for restoration of product?
    do reply
    thanks

    ReplyDelete
    Replies
    1. Sorry Swapnil I don't have code for Restore purchase..
      Try this-
      mBillingService.restoreTransactions();

      http://developer.android.com/google/play/billing/billing_integrate.html

      install sample code from here it will help you..

      Delete
    2. i saw sample docs I just want to know that do i need to add its AIDL file (service) to implement restore into my project?. Do i need to copy all the files into my project?
      Is there any other way to use their restore part into my code?
      thank you

      Delete
    3. Yes if you make payment using your application and want restore purchase then you should add all files because that provided by google including services, receiver, base64 classes for secure payment so we should use all of their code..

      Thanks,

      Delete
    4. Manish thanks for reply i am using your code for purchase and if i add googles all files than there will be some conflict of file. i think so and one more thing is that if i used google code to purchase than i got Null signature and with your code it works fine for purchase...so wondering i will do some research and i will add part of code (restore) into my project..with AIDL and service and helper.

      Delete
    5. manish Issue in your code is that your purchase code is only work on 3g ..on restricted wifi it wont work..but google sample code works for same restricted wifi but ur code won't.. i think problem is in port number you are using for communicating with google play..can you please tell me where should i find this port number code ?

      Delete
    6. No I don't think this is a case..
      wifi and 3g no matter here I have tested on both..

      Delete
    7. Okay may be there is restriction on my wifi..on more thing is you are using "API_VERSION" 1 in makeRequestBundle method?..api version 3 available but still you have used version 1..if i try to change it from 1 to 3 it wont work but it work on version 2 why?

      Delete
    8. Please read Google doc for further detail, May be only changing version does not work you should change code also according version-3.

      Version 2 supports both unmanaged and managed products, as well as supports subscriptions, where Version 3 does not yet offer support for subscriptions. If you want to sell subscriptions in your app, you should implement In-app Billing Version 2, rather than Version 3.

      If you do not need to sell subscriptions, you should implement In-app Billing Version 3 instead.

      Delete
  25. Thanks Manish for this great tut.
    My question is will the item purchased will be removed upon uninstalling the app and then install it agin?
    Should i implement a database to keep track of users item?
    Or the app itself keep track of the item purchased by the user account and his item?
    Thanks in advance

    ReplyDelete
    Replies
    1. Prefer way to keep a database on server for future use,,
      well Google have all record of purchase of your application on their server you can get any time for there but if you want maintain your own database you can...

      Thanks,

      Delete
  26. Hello, Manish , how can i find out my public key for String base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE"; please help me.

    ReplyDelete
    Replies
    1. Please see top of my blog I have wrote all steps there..
      Well when you register for Google developer account with paying $25, you will get your public key..

      Delete
    2. thank you, for your reply. May I use general SDK for payment for all system like paypal, google wallet and any other which available in world? if yes please give me the references.

      Delete
  27. hi Manish how can i get the response for unmanaged products , is this condition( if(BillingHelper.latestPurchase.isPurchased()){
    showItem();
    }) works for unmanaged products

    ReplyDelete
    Replies
    1. No its not work for bulk purchase means cart system..
      All product should be register with Google store...

      Delete
  28. thanks manish for the project that is really helpful me...
    actually i m working with a project that have online billing concept so i don't know how to integrate ur project with that...give me some solution.

    ReplyDelete
    Replies
    1. Hi Just copy pate whole package in your project.
      and add all permission in your manifest..

      Delete
  29. This comment has been removed by the author.

    ReplyDelete
  30. very helpful blog.... thanks a lot manish....

    ReplyDelete
  31. I want to know that your project should be attached with my app or it should be separately done.
    Please reply......thanks in advance..........

    ReplyDelete
    Replies
    1. Just copy paste all package in your project and play with mainactivity.java...

      Delete
    2. Hi manish, I want to show the XLSpreadsheet and edit the Sheet in ralativelayout, can you please help me for that

      Delete
  32. Hey Manish,

    Here I am back with one more query..!! Is it possible to set a password to check the installation of .apk on mobile, i.e when i install my app on to mobile it should ask for a password or key and only if the key is correct it should install the application. Tried googling but couldnot find much info.

    Hope you will help me, thanks in advance....

    Best regards,
    yamini.

    ReplyDelete
    Replies
    1. In my knowdladge there is no way like this but you can do one thing-
      Make an activity Applaunch.java and chek prefrence for first time use before your home or splash page. And add an editbox for match password, if password is ok login into app and save preference true so app did not ask again and again password.

      Delete
    2. That was a great idea can you please help me out how i can do it, check preferenceonly once?!

      Delete
    3. Hi Yamini, I want to show the XLSpreadsheet and edit the Sheet in ralativelayout, can you please help me for that

      Delete
  33. Hi manish can you send zip file to me. actually i am working on a project.....

    Email id: vipin.vishwakarma@countrysideworks.com

    I hope u will send ASAP.

    ReplyDelete
    Replies
    1. Hi Please Check my blog a download link there..

      Thanks!

      Delete
  34. Hai,

    I am using your same code and i have implement my in app package and hash key but it will give me error like "USER IS NOT ELIGIBLE FOR THIS PURCHASE".

    Can you just help to solve this problem?

    ReplyDelete
    Replies
    1. Are you using developer account? I mean have you added item on Google play store?

      Delete
  35. Hello Manish,

    Is it Latest version of in-app purchase V3 or old version of V2.

    and can you please how to implement in-purchase with subscriptions of one month or one year.

    Can you please reply for this. I would like to implement your code in my app.

    Thanks,

    ReplyDelete
    Replies
    1. Hi please see android developer website for more detail..
      Yes subscriptions is possible in android in app purchase, read this-
      http://developer.android.com/google/play/billing/billing_overview.html

      Delete
  36. Hi Manish,

    In MainActivity.java line number 50 the 2nd parameter "android.test.purchased" what dose it do.?
    should I change it when the code goes to live.?
    If it needed a change what should I pass parameter.?

    Can you please tell me.

    Thanks,

    ReplyDelete
    Replies
    1. There is no need to change any thing.. Just see BillingHelper class.
      It is tag only "com.test.purchase" , if you want change it you can no issue but make changes in both places..

      Thanks!

      Delete
  37. Ok thanks Manish,

    One more thing, Is there any testing mode to make sure that the payment transactions are in proper.? If yes how.?

    ReplyDelete
    Replies
    1. Yes sure you can test your payment getway.
      1)create signed apk for your application
      2)add your apk on google store in draft folder. Don't publish it.
      3)now test your application.
      4)for more detail please read google doc. and when you login into google developer console you will got more info there, on merchant account.

      Delete
  38. Hi Manish,
    I have created one android app. Please tell me procedure to create paid app(one time payment).
    Thanks,
    Shrikant.Salunkhe

    ReplyDelete
    Replies
    1. That mean your app is paid, right? means there is no 2 version paid and free?
      This is very easy condition. At the time of uploading your apk on google market use paid merchant account. Here you can set your price for your application. Just setup merchant account on play store.

      Delete
  39. Ok Manish. Thanks. Is any document required to open merchant account?

    ReplyDelete
    Replies
    1. No Document. And no worry, when you going to upload your apk on google store it will provide step by step help.

      Delete
  40. Hi Manish,

    this works perfectly for me however I am having a small issue.

    How can i check whether the item has been purchased and the run a little code snippet?

    Thanks,

    ReplyDelete
    Replies
    1. please check log-cat here you will got successes or failure message...

      Delete
    2. Hi, how can i test the purchase when i am doing it on a test device, as I am required to sign the apk file?
      Also is the following line of code what is used to detect whether the purchase has been completed and if so run some code:
      BillingHelper.latestPurchase.isPurchased()
      Thanks

      Delete
  41. i have one application develop and integration your code and change base64 code key but run the code and get error for 'mservice' get null

    10-17 10:50:51.578: E/AndroidRuntime(13089): FATAL EXCEPTION: main
    10-17 10:50:51.578: E/AndroidRuntime(13089): java.lang.NullPointerException
    10-17 10:50:51.578: E/AndroidRuntime(13089): at com.fortunot.inapppurchase.BillingHelper.isBillingSupported(BillingHelper.java:51)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at com.fortunot.fortunecookie.MainActivity.onClick(MainActivity.java:263)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.view.View.performClick(View.java:2485)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.view.View$PerformClick.run(View.java:9080)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.os.Handler.handleCallback(Handler.java:587)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.os.Handler.dispatchMessage(Handler.java:92)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.os.Looper.loop(Looper.java:130)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at android.app.ActivityThread.main(ActivityThread.java:3687)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at java.lang.reflect.Method.invokeNative(Native Method)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at java.lang.reflect.Method.invoke(Method.java:507)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    10-17 10:50:51.578: E/AndroidRuntime(13089): at dalvik.system.NativeStart.main(Native Method)


    plz I hope u will send ASAP.

    thanks

    ReplyDelete
  42. hi manish,
    i will use your code but i have a problem, how to know transaction is complete or not...?
    i will try to sell virtual coins for my games.please help me,to know transaction process or
    detail tutorial please. thanks

    ReplyDelete
    Replies
    1. Hi please check your log, I have printed there successes message.
      You can check for more android developer web-site and yes there zip code too you can try that. I think that will help you..
      Thanks!

      Delete
    2. See this URL-
      http://developer.android.com/google/play/billing/billing_overview.html

      Delete
    3. This comment has been removed by the author.

      Delete
  43. hi manish,
    i will try and can't understand all things, its give massage "RESULT_OK" , its mean transaction
    is success..? and i will upload my apk also in google play "https://play.google.com/store/apps/details?id=com.mobibit.gpay" for testing. please help me if i will pay the amount its give RESULT_OK else other massage.. i m right or not...?

    ReplyDelete
    Replies
    1. You are right dear and it will give you successes message. And upload your project in google market and you can test it and clear your doubt.

      Thanks!

      Delete
  44. This comment has been removed by the author.

    ReplyDelete
  45. Hi manish there are some confustion pls help me
    1) i had succefyly draf apk file and publish in app purchse product but the question is that i had upload product with name pro verion but when i test from my device it's display me sample title and 0.99$ as price. it there anything wrong or it's display it due to testing mode
    2) when i am going to live that app what thing i have to change in my app that my application can get proper in app purchase?

    ReplyDelete
    Replies
    1. 1)Please read android doc first properly. At the time of testing they allow fix price .99$ if any change in earlier I have no idea.Please read doc.
      2)Sure it will work perfectly and no change you have to make. And if you got any issue you can re-wart to draft any time. so please test your self only then promote your app.

      Thanks!

      Delete
    2. This comment has been removed by the author.

      Delete
  46. Ok and i cannot found any item id in my google play ac then which is item and where i have to replace it?

    ReplyDelete
    Replies
    1. Please don't remove comments and read this url hope it will help you-
      http://developer.android.com/google/play/billing/billing_testing.html

      Delete
  47. Hi! I need some help, maybe you can answer for my question. My problem is the following:
    I have an uploaded app with a package name e.g. com.android.example1. I created another project e.g. com.android.example2. So my question is: Is there any way from example2 to get the purchases which i made in example1? (the google account the same, the base64key is the same, the only one difference is the packagename.)

    ReplyDelete
    Replies
    1. In my knowledge there is no way for this task and google does not allow us for security reason.

      Delete
  48. Hi manish...........This is the best 'greatest' website to learn Android..you are the best author to reply all the questions in time....this is really superb.....Please keep this up....Thank you.....Thanks from my heart

    ReplyDelete
  49. Hi Manish,

    I am using your above code and everything is working fine, I have added my own items into the developer account and the purchase happens correctly.

    The issue I am having is how do i detect that the user has made the purchase? I am trying to use this line of code 'showItem(BillingHelper.latestPurchase.productId)', however this is never going into my function showItem().

    Basically, I have an upgrade managed product, then when the user purchases this I want to make one of my buttons invisible, so I need the app to trigger a function when the purchase is complete, please help.

    Thanks.

    ReplyDelete
    Replies
    1. Please see log it will give you successes message after that you can do what you want with your button..

      Delete
    2. I have seen the log and know it is a success, however the problem is I DO NOT know how to call a function upon success.

      There must be a snippet of code, could you please provide this and where in my code I would need to place this in order for the function to be calle.

      Delete
  50. Is this Version 3 of the inapp Billing library?

    ReplyDelete
  51. Hi, May I know where do I get payment response.? whether the payment is successful of unsuccessful.?
    If successful I need to do a service call there.

    ReplyDelete
    Replies
    1. please check log-cat here you will get successes message. and for more detail please use debugger. please check BillingService.java i think here that log.

      Delete
  52. Hi Manish,

    Im beginner in Android apps.Will u post the code for our expense manager? I hope u will provide ASAP.It will be helpful for me.

    Thanks in advance
    Sudhakar

    ReplyDelete
    Replies
    1. whole app code? i think this is not good to post whole app code. if you have query related to any particular task please tell, i will try to provide you.

      Thanks

      Delete
  53. Hi Manishji,

    May I know how to store and fetch data through datewise transaction using datepicker?

    Thanks in Advance
    Sudhakar
    (sudhakarmsc91@gmail.com)

    ReplyDelete
    Replies
    1. do some thing like that-

      select *
      from TableName
      where mydate >= Datetime('2010-01-01 00:00:00')
      and mydate <= Datetime('2030-01-01 23:00:59');


      AND for one date-

      select *
      from TableName
      where mydate == Datetime('2010-01-01 00:00:00');

      Thanks!

      Delete
  54. Hellol thanks for your share, hot to implement this code in code source of another apps? we have to follow and creat a MainActivity.java or we can create another one with other name? thanks

    ReplyDelete
    Replies
    1. Just copy paste same package in your app just change code of mainActivity.. You can read android developer website too for more help.

      Delete
  55. Hello Manish am trying to implement in app purchase using ur article how can i update my app public key in my app,.And also how to test my app with testers acc pls help me to achieve this,.

    thank you,.

    ReplyDelete
  56. Hi am getting these exceptions,.. please check out

    12-26 16:25:00.345: E/AndroidRuntime(294): FATAL EXCEPTION: main
    12-26 16:25:00.345: E/AndroidRuntime(294): java.lang.RuntimeException: Unable to resume activity {com.vm.vminapppurchase/com.vm.vminapppurchase.MainActivity}: android.app.SuperNotCalledException: Activity {com.vm.vminapppurchase/com.vm.vminapppurchase.MainActivity} did not call through to super.onResume()
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.os.Handler.dispatchMessage(Handler.java:99)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.os.Looper.loop(Looper.java:123)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-26 16:25:00.345: E/AndroidRuntime(294): at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 16:25:00.345: E/AndroidRuntime(294): at java.lang.reflect.Method.invoke(Method.java:521)
    12-26 16:25:00.345: E/AndroidRuntime(294): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-26 16:25:00.345: E/AndroidRuntime(294): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-26 16:25:00.345: E/AndroidRuntime(294): at dalvik.system.NativeStart.main(Native Method)
    12-26 16:25:00.345: E/AndroidRuntime(294): Caused by: android.app.SuperNotCalledException: Activity {com.vm.vminapppurchase/com.vm.vminapppurchase.MainActivity} did not call through to super.onResume()
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.Activity.performResume(Activity.java:3825)
    12-26 16:25:00.345: E/AndroidRuntime(294): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
    12-26 16:25:00.345: E/AndroidRuntime(294): ... 12 more


    12-26 16:25:08.922: I/BillingService(312): Service starting with onCreate
    12-26 16:25:08.932: E/BillingService(312): Market Billing Service could not be bound.
    12-26 16:25:08.943: D/AndroidRuntime(312): Shutting down VM

    ReplyDelete
  57. amgetting this too

    12-26 16:25:08.952: E/AndroidRuntime(312): FATAL EXCEPTION: main
    12-26 16:25:08.952: E/AndroidRuntime(312): java.lang.RuntimeException: Unable to create service com.vm.vminapppurchase.BillingService: java.lang.NullPointerException
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2969)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.app.ActivityThread.access$3300(ActivityThread.java:125)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2087)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.os.Handler.dispatchMessage(Handler.java:99)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.os.Looper.loop(Looper.java:123)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.app.ActivityThread.main(ActivityThread.java:4627)
    12-26 16:25:08.952: E/AndroidRuntime(312): at java.lang.reflect.Method.invokeNative(Native Method)
    12-26 16:25:08.952: E/AndroidRuntime(312): at java.lang.reflect.Method.invoke(Method.java:521)
    12-26 16:25:08.952: E/AndroidRuntime(312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    12-26 16:25:08.952: E/AndroidRuntime(312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    12-26 16:25:08.952: E/AndroidRuntime(312): at dalvik.system.NativeStart.main(Native Method)
    12-26 16:25:08.952: E/AndroidRuntime(312): Caused by: java.lang.NullPointerException
    12-26 16:25:08.952: E/AndroidRuntime(312): at com.vm.vminapppurchase.MainActivity.setStatus(MainActivity.java:132)
    12-26 16:25:08.952: E/AndroidRuntime(312): at com.vm.vminapppurchase.BillingService.onCreate(BillingService.java:32)
    12-26 16:25:08.952: E/AndroidRuntime(312): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2959)
    12-26 16:25:08.952: E/AndroidRuntime(312): ... 10 more

    ReplyDelete
    Replies
    1. Please register on google developer console for in app billing. well you can ask to your client to for access of developer console from where you can create new KEY.
      please check below link and follow step by step-
      https://play.google.com/apps/publish/

      Delete
    2. Thank you, But i already created dev console and i did upload the apk also ,.i.e,. I did all steps in this article,.but am getting this error while clicking button "SecurityException : Binder invocation to incorrect interface "

      Delete
    3. No idea dear. Please check your log-cat in which you are getting this error then try again. you can search on google for your problem too.
      If still you have trouble please go through android developer and download latest zip demo code.

      Delete
  58. Hey my name is aniruidh
    After following this steps how to integrate it to my application
    1)Create a new Project in Android.
    2)Create MainActivity.java class.
    3)Add activity_main.xml in your res/layout folder.
    4)Add Billing services and permission in Manifest.xml.

    Read more: http://www.androidhub4you.com/2013/03/how-to-inegrate-in-app-purchase-billing.html#ixzz2pjKbwhCB

    ReplyDelete
    Replies
    1. Dear all code are there just copy and paste into your project and there some suggestion as well try to follow them..

      Delete
    2. Hi Manish,
      Thanks for ur reply,i have done and i tested in test mode it is working fine but, how to pass the product name and price.please can help me regarding this

      Regards
      Anirudh

      Delete
    3. You have to add item and price on Google developer console. And you have to just pass that specific id for every product.

      Delete
  59. ERROR While retrieving information from server.[RPC:S-7:AEC-0] is coming when saving the credit card details .... sorry for asking u like this what is the version of in app purchase code .

    ReplyDelete
  60. I am not sure about that error but I think it version-3.

    ReplyDelete
  61. hello manish first of all Thanks for giving such a superb tutes, i wanna make demo for InApp purchase i am using your code ,Is'nt there a way to do it for demos. means Can we do it without paying, like sandbox type in paypal.??

    ReplyDelete
    Replies
    1. Yes please check In app Purchase document. And if you go through real development at that time also you can test your product.

      Delete
  62. Manish thanks for u response ,One thing more as per you demo it is showing prices in $ ,in India it is in Rs etc etc according to country, so it is handling by us(means need to code for that) or google play will handel? Waiting for your responce Thanks

    ReplyDelete
  63. hi,I want to implement inapp purchase for some feature of my application .so, can you pls give me idea about it

    ReplyDelete
  64. Hi Manish ! I want to know the benefit and different between In App Purchase and Normal payment. Which one is best?

    ReplyDelete
  65. i am getting this error
    Received action: com.android.vending.billing.IN_APP_NOTIFY
    notify got id: android.test.purchased
    getPurchaseInformation()
    Nonce generateD: -2625802164677832116
    current request is:7910620689484784487
    GET_PURCHASE_INFORMATION Sync Response code: RESULT_OK
    Received action: com.android.vending.billing.PURCHASE_STATE_CHANGED
    purchaseStateChanged got signedData: {"nonce":-2625802164677832116,"orders":[{"notificationId":"android.test.purchased","orderId":"transactionId.android.test.purchased","packageName":"com.futerox.fbvideos","productId":"android.test.purchased","purchaseTime":1395404323710,"purchaseState":0}]}
    purchaseStateChanged got signature:
    signedData: {"nonce":-2625802164677832116,"orders":[{"notificationId":"android.test.purchased","orderId":"transactionId.android.test.purchased","packageName":"com.futerox.fbvideos","productId":"android.test.purchased","purchaseTime":1395404323710,"purchaseState":0}]}
    Shutting down VM
    03-21 17:47:04.356: W/dalvikvm(26432): threadid=1: thread exiting with uncaught exception (group=0x40e3f2a0)
    FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start receiver com.futerox.inappbillinservice.BillingReceiver: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2274)
    at android.app.ActivityThread.access$1500(ActivityThread.java:140)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
    at android.os.Handler.dispatchMessage(Handler.java:99)

    Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
    at java.util.ArrayList.get(ArrayList.java:304)
    at com.futerox.inappbillinservice.BillingHelper.verifyPurchase(BillingHelper.java:300)
    at com.futerox.inappbillinservice.BillingReceiver.purchaseStateChanged(BillingReceiver.java:45)
    at com.futerox.inappbillinservice.BillingReceiver.onReceive(BillingReceiver.java:27)
    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2267)


    give some idea ya demo zip
    on my email
    santoshkumar@futerox.biz
    08130824333

    Thanks
    can you plz give me some suggestion

    ReplyDelete
    Replies
    1. I think you have not generated base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE"; for you application on google play store.


      Delete
    2. i think you don't read my error thats why you told
      I think you have not generated base64EncodedPublicKey = "PUT YOUR PUBLIC KEY HERE"; for you application on google play store.
      K
      Thanks...
      Santosh Kumar

      Delete
  66. Hi Manish.

    I just wanted to know how should I integrate inApp purchase with my project. As I am new to this and never used the Inapp before

    ReplyDelete
  67. very good tutorial. pretty good..thanks manish

    ReplyDelete
  68. Manish sir, Good tutorial.. It works.. And one thing I ask, have you any sample code for In-app subscription. This is in-app purchase but as per requirements, I have to use in-app subscription that means monthly or yearly renewable payment type. So is there in sample or tutorial, please upload here. Or you can contact me on my id: sagar.ucet091@gmail.com
    Thank you sir in advance.

    ReplyDelete
  69. I get "BillingService﹕ BillingHelper not fully instantiated" in LogCat and purchase doesn't start.

    ReplyDelete