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

Also Read :   AngularJs ng-controller directive with 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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>livePaypal_example</groupId>
  <artifactId>livePaypal_example</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>livePaypal_example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>live24u</groupId>
      <artifactId>live24u</artifactId>
      <version>3.8.1</version>
      <scope>example</scope>
    </dependency>
    <dependency>
  <groupId>com.paypal.sdk</groupId>
  <artifactId>rest-api-sdk</artifactId>
  <version>LATEST</version>
</dependency>
<!--Devloped By Live24u.com Servlet and JSP API Jars -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
<!--Devloped By Live24u.com JSTL Jars -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
 
 </dependencies>
  <build>
    <finalName>livePaypal_example</finalName>
  </build>
</project>

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.

Also Read :   what is Angular 2 and Difference between Angular 1 VS Angular 2

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.

<html>
<body>
<h2>Step by setpPaypal Payment Example by Live24u.com</h2>
 
<form action="http://localhost:8080/livePaypal_example/LiveDirectPay" method="post">
<table>
<tr>
<td>Live Cart Sub Total</td>
<td><input type="text" name="total_Cart"></td>
</tr>
<tr>
<td>Total Shipping</td>
<td><input type="text" name="total_shiiping"></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
 
</body>
</html>

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 Live24u.com Set payer details
        Payer payer = new Payer();
        payer.setPaymentMethod("paypal");
 
        //Devloped By Live24u.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 Live24u.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 Live24u.com Payment amount
        Amount amount = new Amount();
        amount.setCurrency("USD");
        //Devloped By Live24u.com Total must be equal to sum of total_shiiping, tax and subtotal.
        amount.setTotal(""+total);
        amount.setDetails(details);
 
        //Devloped By Live24u.com Transaction information
        Transaction transaction = new Transaction();
        transaction.setAmount(amount);
        transaction
          .setDescription("This is the Live24u.com payment transaction description.");
 
        //Devloped By Live24u.com Add transaction to a list
        List<Transaction> live_tansactions = new ArrayList<Transaction>();
        live_tansactions.add(transaction);
 
        //Devloped By Live24u.com Add payment details
        Payment payment = new Payment();
        payment.setIntent("sale");
        payment.setPayer(payer);
        payment.setRedirectUrls(redirectUrls);
        payment.setTransactions(live_tansactions);  
         
        //Devloped By Live24u.com Create payment
        try {
          Payment liveCreatedPay = payment.create(apiContext);
           

          Iterator<Links> 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.

Also Read :   AngularJS Controllers Inheritance - angular inherits Example

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 Live24u.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(live24u.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(live24u.com) Are Most Always Welcome.