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 …

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.

[sociallocker id=1372]Android Paypal Integration Tutorial Source Code[/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x