Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package com.bookstore.onlinebookstore.service;
  2.  
  3. import java.math.BigDecimal;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10.  
  11. import com.bookstore.onlinebookstore.model.Payment;
  12. import com.bookstore.onlinebookstore.repository.PaymentRepository;
  13. import com.braintreegateway.BraintreeGateway;
  14. import com.braintreegateway.ClientTokenRequest;
  15. import com.braintreegateway.Environment;
  16. import com.braintreegateway.Result;
  17. import com.braintreegateway.Transaction;
  18. import com.braintreegateway.TransactionRequest;
  19. import com.braintreegateway.ValidationError;
  20.  
  21. @Service
  22. public class PaymentService {
  23. /* ********Braintree Payment Transaction Credentials ************ */
  24. private static final String MERCHANT_ID = "64fmbfx4mt6pc69j";
  25. private static final String PUBLIC_KEY = "cnt5rnqt5zxcmcbf";
  26. private static final String PRIVATE_KEY = "e533d5e2074d2bdad3e78fb988e000f6";
  27.  
  28. /* ********Braintree Payment Transaction Credentials ************ */
  29.  
  30. @Autowired
  31. PaymentRepository paymentRepository;
  32.  
  33. public BraintreeGateway getBrainTreeGateway() {
  34. return new BraintreeGateway(Environment.SANDBOX, MERCHANT_ID, PUBLIC_KEY, PRIVATE_KEY);
  35. }
  36.  
  37. public Map<String, String> getClientToken() {
  38. BraintreeGateway gateway = getBrainTreeGateway();
  39. ClientTokenRequest clientTokenRequest = new ClientTokenRequest();
  40. String clientToken = gateway.clientToken().generate(clientTokenRequest);
  41. HashMap<String, String> map = new HashMap<>();
  42. map.put("clientToken", clientToken);
  43. return map;
  44. }
  45.  
  46. public Boolean makePayment(Long orderId, BigDecimal totalCost, String paymentMethodNonce) {
  47. String transactionId = processPayment(totalCost, paymentMethodNonce);
  48. if (transactionId != null) {
  49. insertPayment(orderId, transactionId);
  50. return true;
  51. }
  52. return false;
  53. }
  54.  
  55. private void insertPayment(Long orderId, String transactionId) {
  56. Payment payment = new Payment();
  57. payment.setDateCreated(new Date());
  58. payment.setTransactionId(transactionId);
  59. payment.setOrderId(orderId);
  60. paymentRepository.save(payment);
  61.  
  62. }
  63.  
  64. public String processPayment(BigDecimal totalCost, String paymentMethodNonce) {
  65. Result<Transaction> result = this.processTransaction(totalCost, paymentMethodNonce);
  66.  
  67. if (result.isSuccess()) {
  68. Transaction transaction = result.getTarget();
  69. System.out.println("Success!: " + transaction.getId());
  70. String transactionId = transaction.getId();
  71. System.out.println(transactionId);
  72. return transactionId;
  73. } else if (result.getTransaction() != null) {
  74. Transaction transaction = result.getTransaction();
  75. System.out.println("Failed!: " + transaction.getId());
  76. System.out.println("Error processing transaction:");
  77. System.out.println(" Status: " + transaction.getStatus());
  78. System.out.println(" Code: " + transaction.getProcessorResponseCode());
  79. System.out.println(" Text: " + transaction.getProcessorResponseText());
  80. return null;
  81. } else {
  82. for (ValidationError error : result.getErrors().getAllDeepValidationErrors()) {
  83. System.out.println("Attribute: " + error.getAttribute());
  84. System.out.println(" Code: " + error.getCode());
  85. System.out.println(" Message: " + error.getMessage());
  86. }
  87. return null;
  88. }
  89. }
  90.  
  91. public Result<Transaction> processTransaction(BigDecimal totalCost, String paymentMethodNonce) {
  92. TransactionRequest req = new TransactionRequest().amount(totalCost).paymentMethodNonce(paymentMethodNonce)
  93. .options().submitForSettlement(true).done();
  94.  
  95. Result<Transaction> result = this.getBrainTreeGateway().transaction().sale(req);
  96. System.out.println(result.getMessage());
  97. return result;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement