├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── javatechie │ │ └── spring │ │ └── paypal │ │ └── api │ │ ├── Order.java │ │ ├── PaypalConfig.java │ │ ├── PaypalController.java │ │ ├── PaypalService.java │ │ └── SpringPaypalExampleApplication.java └── resources │ ├── application.properties │ └── templates │ ├── cancel.html │ ├── home.html │ └── success.html └── test └── java └── com └── javatechie └── spring └── paypal └── api └── SpringPaypalExampleApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-paypal-example 2 | How to perform payment integration using paypal 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.springframework.boot 8 | spring-boot-starter-parent 9 | 2.1.4.RELEASE 10 | 11 | 12 | com.javatechie 13 | spring-paypal-example 14 | 0.0.1-SNAPSHOT 15 | spring-paypal-example 16 | payment integration with paypal 17 | 18 | 19 | 1.8 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | com.paypal.sdk 32 | rest-api-sdk 33 | 1.4.2 34 | 35 | 36 | org.projectlombok 37 | lombok 38 | true 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/paypal/api/Order.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | import lombok.ToString; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @NoArgsConstructor 11 | @ToString 12 | public class Order { 13 | 14 | private double price; 15 | private String currency; 16 | private String method; 17 | private String intent; 18 | private String description; 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/paypal/api/PaypalConfig.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | import com.paypal.api.payments.PaymentHistory; 11 | import com.paypal.base.rest.APIContext; 12 | import com.paypal.base.rest.OAuthTokenCredential; 13 | import com.paypal.base.rest.PayPalRESTException; 14 | 15 | @Configuration 16 | public class PaypalConfig { 17 | 18 | @Value("${paypal.client.id}") 19 | private String clientId; 20 | @Value("${paypal.client.secret}") 21 | private String clientSecret; 22 | @Value("${paypal.mode}") 23 | private String mode; 24 | 25 | @Bean 26 | public Map paypalSdkConfig() { 27 | Map configMap = new HashMap<>(); 28 | configMap.put("mode", mode); 29 | return configMap; 30 | } 31 | 32 | @Bean 33 | public OAuthTokenCredential oAuthTokenCredential() { 34 | return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig()); 35 | } 36 | 37 | @Bean 38 | public APIContext apiContext() throws PayPalRESTException { 39 | APIContext context = new APIContext(oAuthTokenCredential().getAccessToken()); 40 | context.setConfigurationMap(paypalSdkConfig()); 41 | return context; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/paypal/api/PaypalController.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.ModelAttribute; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | 10 | import com.paypal.api.payments.Links; 11 | import com.paypal.api.payments.Payment; 12 | import com.paypal.base.rest.PayPalRESTException; 13 | 14 | @Controller 15 | public class PaypalController { 16 | 17 | @Autowired 18 | PaypalService service; 19 | 20 | public static final String SUCCESS_URL = "pay/success"; 21 | public static final String CANCEL_URL = "pay/cancel"; 22 | 23 | @GetMapping("/") 24 | public String home() { 25 | return "home"; 26 | } 27 | 28 | @PostMapping("/pay") 29 | public String payment(@ModelAttribute("order") Order order) { 30 | try { 31 | Payment payment = service.createPayment(order.getPrice(), order.getCurrency(), order.getMethod(), 32 | order.getIntent(), order.getDescription(), "http://localhost:9090/" + CANCEL_URL, 33 | "http://localhost:9090/" + SUCCESS_URL); 34 | for(Links link:payment.getLinks()) { 35 | if(link.getRel().equals("approval_url")) { 36 | return "redirect:"+link.getHref(); 37 | } 38 | } 39 | 40 | } catch (PayPalRESTException e) { 41 | 42 | e.printStackTrace(); 43 | } 44 | return "redirect:/"; 45 | } 46 | 47 | @GetMapping(value = CANCEL_URL) 48 | public String cancelPay() { 49 | return "cancel"; 50 | } 51 | 52 | @GetMapping(value = SUCCESS_URL) 53 | public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) { 54 | try { 55 | Payment payment = service.executePayment(paymentId, payerId); 56 | System.out.println(payment.toJSON()); 57 | if (payment.getState().equals("approved")) { 58 | return "success"; 59 | } 60 | } catch (PayPalRESTException e) { 61 | System.out.println(e.getMessage()); 62 | } 63 | return "redirect:/"; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/paypal/api/PaypalService.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import java.math.BigDecimal; 4 | import java.math.RoundingMode; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | 11 | import com.paypal.api.payments.Amount; 12 | import com.paypal.api.payments.Payer; 13 | import com.paypal.api.payments.Payment; 14 | import com.paypal.api.payments.PaymentExecution; 15 | import com.paypal.api.payments.RedirectUrls; 16 | import com.paypal.api.payments.Transaction; 17 | import com.paypal.base.rest.APIContext; 18 | import com.paypal.base.rest.PayPalRESTException; 19 | 20 | @Service 21 | public class PaypalService { 22 | 23 | @Autowired 24 | private APIContext apiContext; 25 | 26 | 27 | public Payment createPayment( 28 | Double total, 29 | String currency, 30 | String method, 31 | String intent, 32 | String description, 33 | String cancelUrl, 34 | String successUrl) throws PayPalRESTException{ 35 | Amount amount = new Amount(); 36 | amount.setCurrency(currency); 37 | total = new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue(); 38 | amount.setTotal(String.format("%.2f", total)); 39 | 40 | Transaction transaction = new Transaction(); 41 | transaction.setDescription(description); 42 | transaction.setAmount(amount); 43 | 44 | List transactions = new ArrayList<>(); 45 | transactions.add(transaction); 46 | 47 | Payer payer = new Payer(); 48 | payer.setPaymentMethod(method.toString()); 49 | 50 | Payment payment = new Payment(); 51 | payment.setIntent(intent.toString()); 52 | payment.setPayer(payer); 53 | payment.setTransactions(transactions); 54 | RedirectUrls redirectUrls = new RedirectUrls(); 55 | redirectUrls.setCancelUrl(cancelUrl); 56 | redirectUrls.setReturnUrl(successUrl); 57 | payment.setRedirectUrls(redirectUrls); 58 | 59 | return payment.create(apiContext); 60 | } 61 | 62 | public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{ 63 | Payment payment = new Payment(); 64 | payment.setId(paymentId); 65 | PaymentExecution paymentExecute = new PaymentExecution(); 66 | paymentExecute.setPayerId(payerId); 67 | return payment.execute(apiContext, paymentExecute); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/paypal/api/SpringPaypalExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringPaypalExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringPaypalExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port: 9090 2 | 3 | paypal.mode=sandbox 4 | paypal.client.id=AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS 5 | paypal.client.secret=EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL -------------------------------------------------------------------------------- /src/main/resources/templates/cancel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 | Payment Failure 9 | 10 | -------------------------------------------------------------------------------- /src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | Payment 120 | Accepted Cards 121 | 122 | 123 | 124 | 125 | 126 | 127 | Total 128 | 129 | Currency 130 | 131 | Payment Method 132 | 133 | Intent 134 | 135 | Payment Description 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | Cart 4 147 | Product 1 $1 148 | Product 2 $4 149 | Product 3 $3 150 | Product 4 $2 151 | 152 | Total $10 153 | 154 | 155 | 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /src/main/resources/templates/success.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Insert title here 6 | 7 | 8 | Payment Success 9 | 10 | -------------------------------------------------------------------------------- /src/test/java/com/javatechie/spring/paypal/api/SpringPaypalExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.paypal.api; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringPaypalExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------
Product 1 $1
Product 2 $4
Product 3 $3
Product 4 $2
Total $10