Android Paypal Integration Tutorial

In this post we will learn how to integrate paypal payment in your android application. In this Android Paypal Integration Tutorial we will be using the official SDK provided by paypal.

Now days a payment option is very much essential for many apps if you are selling something through your apps. And PayPal is one of the major payment method used worldwide.

Thats why I thought about posting this Android PayPal Integration Tutorial. So lets begin.

Android Paypal Integration Tutorial Video Demo

You can checkout this video before moving on to the tutorial to know what actually we will be creating.

Android Paypal Integration Tutorial

Creating Paypal Sandbox Account

Now it is very obvious that you will not be testing your payment with actual money. And that is why paypal has given a sandbox feature. You can create sandbox accounts to test payments. So in the first phase of this Android Paypal Integration Tutorial we will create two sandbox accounts. One personal account from where you pay, and one Business Account where you will receive the paid amount. So lets begin.

  • Go to this link and login with your paypal account.
  • Fill the form to create account. Remember you need to create two accounts. For first account select Account Type to Personal and for Other select Account Type to Business.

android paypal integration tutorial

Creating Paypal REST API App

  • Now go to this link to create a paypal app.

android paypal integration tutorial

  • Put your app name and click on Create App.

android paypal integration tutorial

  • Now you will see your Client ID. Copy it some where. Thats all for the Paypal Side. Now we will move to Android Project.

Creating Android Project

Adding PayPal Client ID

  • Create a new Android Project. I created PayPalIntegration.
  • In your package create a class named PayPalConfig.java and write the following code.

  • In the above code you have to put your paypal client id. Now we need to add PayPal SDK to our project.

Adding PayPal SDK

  • We are using Android Studio so go inside your app level build.gradle file. (Someone said that he doesn’t understand app level build.gradle) So I am posting the screenshot. The highlighted one is app level build.gradle file and above that you have the project level build.gradle file. You need to open the app level build.gradle file.

build.gradle

  • Inside the app level build.gradle file you will see dependencies block. Inside that you need to add your paypal sdk. So add the following code.

  • Now sync your project and your done.

Integrating Paypal Payment

  • As this is a sample demonstrating only paypal payment. I will not give a real world demo. Instead I am creating a simple EditText where you can put the payment amount to pay. So come inside your acitivity_main.xml and write the following code.

  • You will get the following UI with the above given code.

android paypal integration tutorial

  • Now come inside MainActivity.java and declare objects for your views and add listener to the button.

  • Inside onCreate() initialize the views and add listener to the buttons.

  • Now create a method named getPayment() and call it on button click.

  • Now to get Payment from PayPal we need a PayPal Configuration Object and a Request Code.

  • Inside onCreate() method we need to start PayPalService.

  • We should destroy the service when app closes.

  • Now complete the method getPayment() with the following code.

  • The above method will invoke the onActivityResult() method after completion. So override onActivityResult() and write the following code.

  • In the above code you can see the string paymentDetails. It is in json format as follows. It contains the payment detail with a unique payment id.

  • In this post I am not covering the verification process. As you need to verify the payment at your server with the payment id you are seeing in the above json.
    You need to store the unique id at your server after the verification. In the next part we will cover the verification process at the server.
    In this tutorial we will show these details to another activity. 
  • Now we will create another activity where we will show the payment details. So create a new empty activity I created ConfirmationActivity.java. And inside layout file for this activity write the following xml code.

  • Now in the java file which is ConfirmationActivity.java write the following code.

  • Thats it now just run your application and you will get the following output.

android paypal integration tutorial

  • If you are seeing the status as approved you can verity the payment at your sandbox paypal account.

android paypal integration tutorial

  • Bingo! it is working absolutely fine. You can download my source code from GitHub. Just go to the link given below.

So thats all for this Android Paypal Integration Tutorial friends. Feel free to leave your comments if having any troubles regarding this Android PayPal Integration Tutorial. Thank You 🙂

59 thoughts on “Android Paypal Integration Tutorial”

  1. hi belal

    i am having a doubt executed this tutorial

    how is payment done without even using internet permision in manifest file

    Reply
    • You don’t need internet permission because the app is opening the paypal activity on another intent.. so basically its not your activity using the internet.. thats why no internet permission is needed

      Reply
  2. Hi, how can i set the product detail to be sold? and how can i get these after transaction is done? thanks in advance…

    Reply
  3. hey Mr belal : i am just want to sent my amount that i entered from my paypal account to anther paypal account ?? please iam waiting ur reply and thnQ

    Reply
  4. Hi Bilal,
    I need some help from you. I have implemented PayPal in my app and now the size of apk extend to 14 MB.
    Then i have found a solution to reduce PayPal sdk size and it reduce up 4.5 MB by removing io.card group
    compile(‘com.paypal.sdk:paypal-android-sdk:2.14.1’) {
    exclude group: ‘io.card’
    }
    I need to know, Is not there any problem with payment.
    Plz let me know quickly.
    nasiralityagi@gmail.com

    Reply
  5. My login is showing invalid emaid-id/password when i am trying to login in the app.I have made an account with that email id in paypal.

    Reply
  6. {
    “response”: {
    “state”: “approved”,
    “id”: “PAY-2WT82232DX478993NK7DNMLA”,
    “create_time”: “2016-08-31T13:06:09Z”,
    “authorization_id”: “70E40512SC428823W”,
    “intent”: “authorize”
    },
    “client”: {
    “platform”: “Android”,
    “paypal_sdk_version”: “2.14.2”,
    “product_name”: “PayPal-Android-SDK”,
    “environment”: “sandbox”
    },
    “response_type”: “payment”
    }

    My transaction are not show plz help me

    Reply
  7. i have a problem mr. belal khan please help me..

    09-20 15:20:34.503 29662-30725/com.dani.skripsi.paypal_integration E/paypal.sdk: request failure with http statusCode:422,exception:Unprocessable Entity
    09-20 15:20:34.505 29662-30725/com.dani.skripsi.paypal_integration E/paypal.sdk: request failed with server response:{“name”:”PAYMENT_CREATION_ERROR”,”debug_id”:”d8deb29c21fba”,”message”:”checkout-session not created”,”information_link”:”https://developer.paypal.com/docs/api/#PAYMENT_CREATION_ERROR”}
    09-20 15:20:34.507 29662-29662/com.dani.skripsi.paypal_integration E/paypal.sdk: PAYMENT_CREATION_ERROR

    Reply
  8. android {
    dexOptions {
    jumboMode true
    }
    }

    use the above if you got the following error

    Information:Gradle tasks [:app:assembleDebug]
    Error:Error converting bytecode to dex:
    Cause: com.android.dex.DexIndexOverflowException: Cannot merge new index 67062 into a non-jumbo instruction!
    Error:Execution failed for task ‘:app:transformClassesWithDexForDebug’.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/usr/lib/jvm/java-8-oracle/bin/java” finished with non-zero exit value 2
    Information:BUILD FAILED
    Information:Total time: 34.777 secs
    Error:AGPBI: {“kind”:”error”,”text”:”Error converting bytecode to dex:\nCause: com.android.dex.DexIndexOverflowException: Cannot merge new index 67062 into a non-jumbo instruction!”,”sources”:[{}],”original”:”UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexIndexOverflowException: Cannot merge new index 67062 into a non-jumbo instruction!\n\tat com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:111)\n\tat com.android.dx.merge.InstructionTransformer.access$800(InstructionTransformer.java:26)\n\tat com.android.dx.merge.InstructionTransformer$StringVisitor.visit(InstructionTransformer.java:74)\n\tat com.android.dx.io.CodeReader.callVisit(CodeReader.java:114)\n\tat com.android.dx.io.CodeReader.visitAll(CodeReader.java:89)\n\tat com.android.dx.merge.InstructionTransformer.transform(InstructionTransformer.java:50)\n\tat com.android.dx.merge.DexMerger.transformCode(DexMerger.java:837)\n\tat com.android.dx.merge.DexMerger.transformMethods(DexMerger.java:811)\n\tat com.android.dx.merge.DexMerger.transformClassData(DexMerger.java:783)\n\tat com.android.dx.merge.DexMerger.transformClassDef(DexMerger.java:680)\n\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:534)\n\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)\n\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:188)\n\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)\n\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)\n\tat com.android.dx.command.dexer.Main.run(Main.java:277)\n\tat com.android.dx.command.dexer.Main.main(Main.java:245)\n\tat com.android.dx.command.Main.main(Main.java:106)\n”,”tool”:”Dex”}
    FAILURE: Build failed with an exception.

    * What went wrong:
    Execution failed for task ‘:app:transformClassesWithDexForDebug’.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/usr/lib/jvm/java-8-oracle/bin/java” finished with non-zero exit value 2

    * Try:
    Run with –stacktrace option to get the stack trace. Run with –info or –debug option to get more log output.

    Information:3 errors
    Information:0 warnings
    Information:See complete output in console

    Reply
  9. Hi Belal Khan,

    First of all thanks for the tutorial. Can you make another tutorial on verifying the payment using the “payment id” and “status” that we get from the payment confirmation object?

    Reply
  10. hi,
    now explain.can you please explain how to get the values like this”
    mTransactionStatus=success,
    mAddress=Coimbatore,
    mTransactionid=PAY-15144772AT147493HLACGQNA,
    mAmount=85.0,
    mDate=2016-10-17T05: 57: 08Z,
    mCurrencyType=USD,

    thank you.

    Reply
    • You will get most of these details from “PaymentConfirmation” object which is “confirm” in our case. You can find PaymentConfirmation in the onActivityResuly method

      Reply
  11. where is the second part of this tutorial. ” In the next part we will cover the verification process at the server.”

    Reply
  12. Do i need to give my credit card details for creating account in paypal. I have created an account in paypal without giving credit card details. When i login from App, i’m getting the following message.

    “There was a problem setting up this payment – account needs a valid funding source, such as a bank or payment card. Please visit the paypal website to check your account.”

    I thought that i can test the app with sandbox feature without providing credit card details. Can you clarify on this

    Reply
  13. Hey thank u for the amazing code,could u please upload the verification part of this tutorial ?
    Thank u in advance…
    Amazing tutorial…

    Reply
  14. E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.user.paypalintegration, PID: 2521
    java.lang.RuntimeException: Unable to start service com.paypal.android.sdk.payments.PayPalService@79d55d7 with Intent { cmp=com.example.user.paypalintegration/com.paypal.android.sdk.payments.PayPalService (has extras) }: java.lang.RuntimeException: Service extras invalid. Please check the docs.
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314)
    at android.app.ActivityThread.-wrap21(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: java.lang.RuntimeException: Service extras invalid. Please check the docs.
    at com.paypal.android.sdk.payments.PayPalService.a(Unknown Source)
    at com.paypal.android.sdk.payments.PayPalService.onStartCommand(Unknown Source)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297)
    at android.app.ActivityThread.-wrap21(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

    Do u know how to solve it? about java.lang.RuntimeException: Service extras invalid. Please check the docs.

    Reply
  15. my app is working fine but in dashboard pf paytm no transaction history is shown is shown like in yours please help me in this.

    Reply
    • Where should I put the json file? I don’t know where to put this:

      {
      “client”: {
      “environment”: “sandbox”,
      “paypal_sdk_version”: “2.0.0”,
      “platform”: “iOS”,
      “product_name”: “PayPal iOS SDK;”
      },
      “response”: {
      “create_time”: “2014-02-12T22:29:49Z”,
      “id”: “PAY-564191241M8701234KL57LXI”,
      “intent”: “sale”,
      “state”: “approved”
      },
      “response_type”: “payment”
      }

      Thanks in advance!

      Reply
  16. Thank you so much for explaining clearly, Bt can u tell me how to fetch the successful transaction id,and amount and store in the database

    Reply
  17. hi
    first of all , thanks for ur great job 😉
    i have an error on getPayment() fct :

    payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), “USD”, “Simplified Coding Fee”,
    PAYMENT_INTENT_SALE);

    it doesnt work and the compiler give me a mistake. I try to ad if(VERSION.SDK…) or Add @>TargetApi(N)annotation without sucess
    can u help me please…

    Reply
  18. Hi,

    great tutorial thank you it helped me very well to start with. Is it possible to make a tutorial on Paypal Adaptive Payments with multiple payment receiver’s on Android? Would be interesting on how to split payments :)…

    Regards

    Reply
  19. Hello bela sir u r great teacher you should make tutorial on payment getway just like instamojo pay u money ect its realy help us to make great aaps

    Reply
  20. This is the json response I am getting from it:

    {“name”:”UNSUPPORTED_PAYEE_CURRENCY”,”debug_id”:”5236e2cc57b5b”,”message”:”The currency is not accepted by payee.”,”information_link”:”https://developer.paypal.com/docs/api/#UNSUPPORTED_PAYEE_CURRENCY”}

    Reply
  21. Hi Belal,
    Thanks for this. Can you provide any tutorial on Ola/Uber integration API into my own app ? It will be very useful for freshers like me. Thanks in advance to anyone for this help.

    Reply
  22. hey sir my App is working fine……but in business account money is added but the total balance is not increasing….please help me in this

    Reply
  23. after I click pay now button it shows an error “There was a problem setting up this payment. Please visit PayPal website to check your account”. How do I resolve this? please reply. thank you in advance.

    Reply
  24. Hi, I follow your tutorial for paypal integation in my application. It work fine. But there is one query if you can help me. You show tutorial for USD payment. But i want to use INR. If i used USD then it work fine, but if i use INR than it have not respond. Can you please me related this issue. I show the code i used.

    PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(totalPrice)),
    “INR”, title, PayPalPayment.PAYMENT_INTENT_SALE);

    Intent intent = new Intent(this, PaymentActivity.class);
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, configuration);
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
    startActivityForResult(intent, PAYPAL_REQUEST_CODE);

    Reply
  25. Thank you so much Bilal… it was very good tutorial for naive developer like me and very self explanatory. As in the above tutorial you said that we need to verify the payment at our server, can you please provide the link to your next tutorial for this verification .

    Reply

Leave a Comment