Paypal Integration in android application (Demo App Source Code)
In this tutorial, I will show you how we can integrate the PayPal payment method into your android application. I also provided a sample app code.

In this tutorial, we are going to use Braintree drop-in API for PayPal integration. by using Braintree payments API we can easily integrate PayPal into our android application. for integration, you must have a PayPal business account and a Braintree developer account. here I will show test implementation using the sandbox.
The first step is to create a PayPal developer account.
second create a Braintree sandbox developer account, after account creation you just need to follow these simple steps
for PayPal integration first of all you need to add these dependencies in your app-level build.gradle file
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.braintreepayments.api:drop-in:5.0.1'
in above two dependencies one is for async get and post request and second is for Braintree PayPal API. if you already have any dependencies for server-side requests then you don't need to add loopj async library just add Braintree payments. API library in your app-level build.gradle file
Brain Tree Client Token Server-side implementation.
After having these two dependencies in your project next step is to create a Braintree client token using Braintree API keys. you don't need to worry about anything I will show you all the steps required for client token generation.
for client token first of all you need to host a Braintree server-side PHP script in your hosting server. if you don't have any hosting server you can create a free hosting account using 000webhost
here is Braintree server-side PHP script Click Here to Download Php Server side script
when you have this php script then you just need to host it in you hosting server. after hosting you unzip this file and open paypal-serverside folder there you will see a index.php file you just need to add you braintree api keys in that file.
How To Get Braintree Api Keys ?
For Braintree API keys you need now to log in to your Braintree developer account and then go to the dashboard there click on the setting button that located at the top right side of the page simply click on that and then choose API keys there may you find your API keys if not then you need to generate your API keys simply click on generate the key.
when you have your API keys then simply put these API keys in Braintree server-side index.php file.
Enable Paypal In Braintree Dashboard
For PayPal integration, you also need to enable PayPal in your Braintree account dashboard. for enabling PayPal in Braintree you also need your PayPal API keys just put your PayPal keys in the Braintree dashboard and enable the PayPal option. all the payments made by clients will be reflected in your PayPal account.
Send Get Request from android application to braintree php script for Client token Generation
In this step, you now need to write code in the android project to send a Get request that will point to Braintree server-side index.php file. I have used loopj API for Server-side requests so here is the sample code. how you can send Get request using loopj API.
String clientToken;
BraintreeFragment mBraintreeFragment;
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://yourwebsite.com/paypal/index.php", new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
Toast.makeText(DeliveryActivity.this, ""+throwable.getMessage(), Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
clientToken = responseString;
try {
mBraintreeFragment = BraintreeFragment.newInstance(DeliveryActivity.this, clientToken);
// mBraintreeFragment is ready to use!
setupBraintreeAndStartExpressCheckout();
} catch (InvalidArgumentException e) {
// There was an issue with your authorization string.
}
}
});
when you will run this code you will receive the client token in on success method of above code simply store that client token in string.
after having the client token next step is to create BraintreeFragment Object as you can see in the above code we have created Braintree Fragment object in the onSuccess method just you need to do the same after having Braintree fragment object & client token now we are ready to call PayPal method.
for PayPal method we used setupBraintreeAndStartExpressCheckout(); this method in onSuccess .So now we will write code for this method
setupBraintreeAndStartExpressCheckout method Code
public void setupBraintreeAndStartExpressCheckout() {
PayPalRequest request = new PayPalRequest("10")
.currencyCode("USD")
.intent(PayPalRequest.INTENT_AUTHORIZE);
PayPal.requestOneTimePayment(mBraintreeFragment, request);
}
when you will run this code you will receive a payment response in Braintree callback method now let's write code for a call back
for a call back you need to implement Braintree PaymentMethodNonceCreatedListener listeners in your activity or fragment.
after implement, you will need to override some methods here is the method override code
@Override
public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("nonce", paymentMethodNonce.getNonce());
params.put("ammount", "10");
client.post("https://yourwebsite.com/paypal/transaction.php",params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
//loadingDialog.show();
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String s = new String(responseBody, StandardCharsets.UTF_8);
if (s.equals("success")){
Map<String,Object> updateStatus = new HashMap<>();
updateStatus.put("Payment Status","Paid with Paypal ");
updateStatus.put("Order Status","Ordered");
firebaseFirestore.collection("ORDERS").document(order_id).update(updateStatus)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
Map<String,Object> userOrder = new HashMap<>();
userOrder.put("order_id",order_id);
userOrder.put("time",FieldValue.serverTimestamp());
firebaseFirestore.collection("USERS").document(FirebaseAuth.getInstance().getUid()).collection("USER_ORDERS").document(order_id).set(userOrder)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
showConfirmationLayout();
// loadingDialog.dismiss();
}else {
Toast.makeText(DeliveryActivity.this, "Failed to update user's orderList", Toast.LENGTH_SHORT).show();
}
}
});
}else {
Toast.makeText(DeliveryActivity.this, "Order Cancelled", Toast.LENGTH_LONG).show();
}
}
});
Toast.makeText(DeliveryActivity.this, "transaction successful", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(DeliveryActivity.this, "transaction failure", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
loadingDialog.dismiss();
}
});
}
above method will call if payment successfully made by the client or failed if payment made by the client we will get payment none using this code
paymentMethodNonce.getNonce()
paymentMethodNonce object we will receive in
onPaymentMethodNonceCreated method just you need to call getNone() method for to get payment method Nonce
now we need to submit this nonce in our server-side Braintree script using POST Request. we also need to submit the total amount with nonce
in our Braintree server-side PHP script there two PHP files that we are using in this implementation first one is index.php for client token
generation and the second is transaction.php file. when you will Send nonce and total amount in transaction.php file you will receive a successful response in the onSuccess method if everything you have followed properly.
i hope you understand the above code. if you have any query please write down a comment below I will be happy to help you.
Thanks.
What's Your Reaction?






