├── .gitignore ├── pom.xml └── src └── main └── java └── pac ├── Application.java ├── controllers └── PayPalController.java └── paypal └── PayPalClient.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *.iml 3 | /target -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | groupId 8 | spring-boot-paypal 9 | 1.0-SNAPSHOT 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.5.8.RELEASE 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-web 19 | 20 | 21 | com.paypal.sdk 22 | rest-api-sdk 23 | 1.13.1 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/java/pac/Application.java: -------------------------------------------------------------------------------- 1 | package pac; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) { 9 | SpringApplication.run(Application.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/pac/controllers/PayPalController.java: -------------------------------------------------------------------------------- 1 | package pac.controllers; 2 | import org.springframework.beans.factory.annotation.Autowired; 3 | import org.springframework.web.bind.annotation.*; 4 | import pac.paypal.PayPalClient; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import java.util.Map; 8 | 9 | @RestController 10 | @RequestMapping(value = "/paypal") 11 | public class PayPalController { 12 | 13 | private final PayPalClient payPalClient; 14 | @Autowired 15 | PayPalController(PayPalClient payPalClient){ 16 | this.payPalClient = payPalClient; 17 | } 18 | 19 | @CrossOrigin(origins = "http://localhost:4200") 20 | @PostMapping(value = "/make/payment") 21 | public Map makePayment(@RequestParam("sum") String sum){ 22 | return payPalClient.createPayment(sum); 23 | } 24 | 25 | @CrossOrigin(origins = "http://localhost:4200") 26 | @PostMapping(value = "/complete/payment") 27 | public Map completePayment(HttpServletRequest request, @RequestParam("paymentId") String paymentId, @RequestParam("payerId") String payerId){ 28 | return payPalClient.completePayment(request); 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/pac/paypal/PayPalClient.java: -------------------------------------------------------------------------------- 1 | package pac.paypal; 2 | 3 | import com.paypal.api.payments.*; 4 | import com.paypal.base.rest.APIContext; 5 | import com.paypal.base.rest.PayPalRESTException; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Component; 8 | 9 | import javax.servlet.http.HttpServletRequest; 10 | import java.util.ArrayList; 11 | import java.util.HashMap; 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | @Component 16 | public class PayPalClient { 17 | 18 | String clientId = "AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS"; 19 | String clientSecret = "EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL"; 20 | 21 | // @Autowired 22 | // PayPalClient(){} 23 | 24 | public Map createPayment(String sum){ 25 | Map response = new HashMap(); 26 | Amount amount = new Amount(); 27 | amount.setCurrency("USD"); 28 | amount.setTotal(sum); 29 | Transaction transaction = new Transaction(); 30 | transaction.setAmount(amount); 31 | List transactions = new ArrayList(); 32 | transactions.add(transaction); 33 | 34 | Payer payer = new Payer(); 35 | payer.setPaymentMethod("paypal"); 36 | 37 | Payment payment = new Payment(); 38 | payment.setIntent("sale"); 39 | payment.setPayer(payer); 40 | payment.setTransactions(transactions); 41 | 42 | RedirectUrls redirectUrls = new RedirectUrls(); 43 | redirectUrls.setCancelUrl("http://localhost:4200/cancel"); 44 | redirectUrls.setReturnUrl("http://localhost:4200"); 45 | payment.setRedirectUrls(redirectUrls); 46 | Payment createdPayment; 47 | try { 48 | String redirectUrl = ""; 49 | APIContext context = new APIContext(clientId, clientSecret, "sandbox"); 50 | createdPayment = payment.create(context); 51 | if(createdPayment!=null){ 52 | List links = createdPayment.getLinks(); 53 | for (Links link:links) { 54 | if(link.getRel().equals("approval_url")){ 55 | redirectUrl = link.getHref(); 56 | break; 57 | } 58 | } 59 | response.put("status", "success"); 60 | response.put("redirect_url", redirectUrl); 61 | } 62 | } catch (PayPalRESTException e) { 63 | System.out.println("Error happened during payment creation!"); 64 | } 65 | return response; 66 | } 67 | 68 | 69 | public Map completePayment(HttpServletRequest req){ 70 | Map response = new HashMap(); 71 | Payment payment = new Payment(); 72 | payment.setId(req.getParameter("paymentId")); 73 | PaymentExecution paymentExecution = new PaymentExecution(); 74 | paymentExecution.setPayerId(req.getParameter("payerId")); 75 | try { 76 | APIContext context = new APIContext(clientId, clientSecret, "sandbox"); 77 | Payment createdPayment = payment.execute(context, paymentExecution); 78 | if(createdPayment!=null){ 79 | response.put("status", "success"); 80 | response.put("payment", createdPayment); 81 | } 82 | } catch (PayPalRESTException e) { 83 | System.err.println(e.getDetails()); 84 | } 85 | return response; 86 | } 87 | 88 | 89 | 90 | } 91 | --------------------------------------------------------------------------------