Pay Pal integration with Android APP

In this example we will be making a simple Android application which will be integrated with PayPal service, just to show how it is done through PayPal developer sandbox account.

First we will need a new Android project in Android Studio, and then we will to create an PayPal developers account. On the following link is simple step-by-step tutorial how to create the account:
https://developer.paypal.com/docs/classic/lifecycle/sb_create-accounts/

After the creating the account, very important is to see you BUSINESS account for testing, because from there we will need the client_id for receiving payment, and it will look something like this:

 

.

Next we need to create the layout for the app:

Next you need to add the PayPal library to your app build.gradle:

implementation com.paypal.sdk:paypal-android-sdk:2.16.0

NOTE: We are going to use ButterKnife Library also for binding the Views from the XML, like buttons, text views, edit text etc.
ButterKnife by Jake Wharton Library with examples

First we declare our global variables and views:

Than in our onCreate() method we bind the view with ButterKnife and initialize the PayPal configuration for our app using the sandbox account and our client_id which is initialized in our Constants class, show later in this example.

The Constants class:

This is the payPalConfigurationInit() method for initilizing the PayPal where our config is addressed as sandbox and we use our previously acclaimed client_id from our developers account:

The startPayPalService() method where we create new Intent witch we use to redirect our app to the PayPal service and add some extra PayPal config and previously initialized PayPalConfiguration object in our previous method:

On our button click we start/process the payment:

In our processPayment() method we get the amount entered in our editText and check if it is not empty. If its not empty then create new PayPalPayment object to send data to the PayPal service for processing. A new intent is created and put some extra key value pairs for processing and we start that intent with startActivityForResult to get some data from the service. We add our payPalConfiguration and our paypalPayment. If the editText is empty, we show only Toast message that nothing is entered :

In our onActivityResult we get our wanted data from the service, and we get our payment confirmation wit some data received in a JSON response. Than we extract what we need from it and start our own PaymentDetailsActivity:

Finally we must stop the PayPal service in our onDestroy method:

Here is our own custom PaymentDetailsActivity. Here we use previously acquired data from the PyPal service and do what ever we want. In this example we use data only for showing that the payment is successful:

And the layout for this class:

Spread the love

Leave a Reply

Your email address will not be published.