Posted inSoftware / Programming / Technology

Paypal Payment Gateway Integration using Java

Paypal Payment Gateway Integration using Java

In this Post We Will Explain About is Paypal Payment Gateway Integration using Java With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Accept PayPal with the Payments API using Java Example

In this post we will show you Best way to implement Integrate Paypal in Web Application using Java, hear for How to integrate Paypal Website Payment in Javawith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Integrate Paypal in Web Application using Java

Paypal is most and very popular payment gateway system available.Paypal provides all the currency exchange Rest API implementation step by step to simple integrate gateway to your ad web-application or website.We Are here using Java Based Restful API to integrate some account Paypal and payment will be simple made on our web-site.

In this laexample example we’re learn to cart total and some total_shiiping as HTML input, We can calculate them some as per your own.

We can download simple project with source code also at the all end of this tutorials.

Paypal is providing the developer guideline and url where we can test our local application.we will use the express checkout method for payment in this example.

paypal payment gateway integration in java source code

Step 1. Open the simple paypal developer link https://developer.paypal.com/
Step 2. We need to make two different some email address , One for Merchant account and other for buyer account respectively.
Step 3. Simple push on Like as a“Sign Up Now” button.
Step 4. Enter the your some information to access the data sandbox environment.
Step 5. Simple push on “Agree and Submit” simple button.
Step 6. Now login with given like as a username/password.
Step 7. Simple push on “Test Account” link
Step 8. make a new test account and provide the some Informations in textfields also.
Step 9. choose the “Account Type” as we want either “Merchant” or “buyer”
Step 10.It will created the default some email id for us.
Step 11.simple data set Credit card data information as “NONE” .
Step 12.Set the some balance amount for your any account.

Dependency


  4.0.0
  livePaypal_example
  livePaypal_example
  war
  0.0.1-SNAPSHOT
  livePaypal_example Maven Webapp
  http://maven.apache.org
  
    
      live24u
      live24u
      3.8.1
      example
    
    
  com.paypal.sdk
  rest-api-sdk
  LATEST



    javax.servlet
    javax.servlet-api
    3.1.0
    provided


    javax.servlet.jsp
    javax.servlet.jsp-api
    2.3.1
    provided



    javax.servlet
    jstl
    1.2

 
 
  
    livePaypal_example
  

Paypal API Credentials

We simple need to create a devloper app under developer account using simple paypal web-site and simple client id and secret need to Put in PaypalServlet.java.

Step 1 : Client ID
Step 2 : Client Secret

index.jsp

Here We have larning HTML inputs simple cart total as well as total_shiiping. We can calculate them as some own way.



Step by setpPaypal Payment Example by Pakainfo.com

Live Cart Sub Total
Total Shipping

LiveServicePayment.java

This simple class simple defines the functions for paypal payment integrate processing.

package com.liveJavaPaypal.livePaypal_example;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.Details;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.PaymentExecution;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
 
public class LiveServicePayment {
    private APIContext apiContext;
    public LiveServicePayment(String clientId,String clientSecret,String mode) {
        apiContext = new APIContext(clientId, clientSecret, mode);  
    }
    public void proecssPayment(HttpServletRequest request,HttpServletResponse response) {
        //Devloped By Pakainfo.com Set payer details
        Payer payer = new Payer();
        payer.setPaymentMethod("paypal");
 
        //Devloped By Pakainfo.com Set redirect URLs
        RedirectUrls redirectUrls = new RedirectUrls();
        redirectUrls.setCancelUrl("http://localhost:8080/livePaypal_example/cancel");
        redirectUrls.setReturnUrl("http://localhost:8080/livePaypal_example/LiveDirectPay");
 
        //Devloped By Pakainfo.com Set payment details
        double total_shiiping = Double.parseDouble(request.getParameter("total_shiiping"));
        double total_Cart = Double.parseDouble(request.getParameter("total_Cart"));
        double tax  = (total_Cart*14)/100;
         
        Details details = new Details();
        details.setShipping(total_shiiping+"");
        details.setSubtotal(total_Cart+"");
        details.setTax(tax+"");
         
         
        double total = total_Cart+total_shiiping+tax;
         
        //Devloped By Pakainfo.com Payment amount
        Amount amount = new Amount();
        amount.setCurrency("USD");
        //Devloped By Pakainfo.com Total must be equal to sum of total_shiiping, tax and subtotal.
        amount.setTotal(""+total);
        amount.setDetails(details);
 
        //Devloped By Pakainfo.com Transaction information
        Transaction transaction = new Transaction();
        transaction.setAmount(amount);
        transaction
          .setDescription("This is the Pakainfo.com payment transaction description.");
 
        //Devloped By Pakainfo.com Add transaction to a list
        List live_tansactions = new ArrayList();
        live_tansactions.add(transaction);
 
        //Devloped By Pakainfo.com Add payment details
        Payment payment = new Payment();
        payment.setIntent("sale");
        payment.setPayer(payer);
        payment.setRedirectUrls(redirectUrls);
        payment.setTransactions(live_tansactions);  
         
        //Devloped By Pakainfo.com Create payment
        try {
          Payment liveCreatedPay = payment.create(apiContext);
           

          Iterator links = liveCreatedPay.getLinks().iterator();
          while (links.hasNext()) {
            Links link = links.next();
            if (link.getRel().equalsIgnoreCase("approval_url")) {
              // REDIRECT USER TO link.getHref()    
             response.sendRedirect(link.getHref()); 
            }
          }
        } catch (PayPalRESTException e) {
            System.err.println(e.getDetails());
        } catch (IOException e) {
            e.printStackTrace();
        }
         
    }
    public void totalPaymentMethod(HttpServletRequest req) {
        Payment payment = new Payment();
        payment.setId(req.getParameter("paymentId"));
 
        PaymentExecution paymentExecution = new PaymentExecution();
        paymentExecution.setPayerId(req.getParameter("PayerID"));
        try {
          Payment liveCreatedPay = payment.execute(apiContext, paymentExecution);
          System.out.println(liveCreatedPay);
        } catch (PayPalRESTException e) {
          System.err.println(e.getDetails());
        }
    }
}

PaypalServlet.java

This java servlet will process step by step the HTML form and create rest api call to the implementation paypal for further some package processing. The Paypal implementation API response shall be printed in cmd console. we can use them some accordingly as per your some need.

package com.liveJavaPaypal.livePaypal_example;
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PaypalServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Devloped By Pakainfo.com TODO Auto-generated method stub
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         
        LiveServicePayment livepaypal_Service;
        if (request.getParameter("PayerID") != null) {
            livepaypal_Service = new LiveServicePayment("live_YOUR_CLIENT_ID","live_YOUR_CLIENT_SECRET","sandbox");
            livepaypal_Service.totalPaymentMethod(request);
             
        } else {
            livepaypal_Service = new LiveServicePayment("live_YOUR_CLIENT_ID","live_YOUR_CLIENT_SECRET","sandbox");
            livepaypal_Service.proecssPayment(request,response);
        }
    }
 
}

Download

I hope you have Got What is paypal payment gateway integration in java source code And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype