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.

Creating Paypal REST API App

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.

PayPalConfig.java

Java

1

2

3

4

5

6

7

8

9

10

packagenet.simplifiedcoding.paypalintegration;

/**

* Created by Belal on 5/1/2016.

*/

publicclassPayPalConfig{

publicstaticfinalStringPAYPAL_CLIENT_ID="YOUR PAYPAL CLIENT ID";

}

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.

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.

build.gradle

1

2

3

4

5

6

7

8

dependencies{

compile fileTree(dir:'libs',include:['*.jar'])

testCompile'junit:junit:4.12'

compile'com.android.support:appcompat-v7:23.3.0'

//You have to add this line

compile'com.paypal.sdk:paypal-android-sdk:2.14.2'

}

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.

//Starting a new activity for the payment details and also putting the payment details with intent

startActivity(newIntent(this,ConfirmationActivity.class)

.putExtra("PaymentDetails",paymentDetails)

.putExtra("PaymentAmount",paymentAmount));

}catch(JSONExceptione){

Log.e("paymentExample","an extremely unlikely failure occurred: ",e);

}

}

}elseif(resultCode==Activity.RESULT_CANCELED){

Log.i("paymentExample","The user canceled.");

}elseif(resultCode==PaymentActivity.RESULT_EXTRAS_INVALID){

Log.i("paymentExample","An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");

}

}

}

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

{

"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"

}

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.

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

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,

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

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.

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 :)…

{“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”}

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.﻿

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.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap