You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. package com.gcc.communityHall.paymentService.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.gcc.communityHall.entity.BookingDetails;
  4. import com.gcc.communityHall.entity.Payments;
  5. import com.gcc.communityHall.entity.RefundTransaction;
  6. import com.gcc.communityHall.paymentService.config.PaymentConfig;
  7. import com.gcc.communityHall.paymentService.entity.RefundClientInfo;
  8. import com.gcc.communityHall.paymentService.entity.RefundResponse;
  9. import com.gcc.communityHall.paymentService.entity.TransactionResponse;
  10. import com.gcc.communityHall.paymentService.entity.TransactionStatusLog;
  11. import com.gcc.communityHall.paymentService.repository.RefundClientInfoRepository;
  12. import com.gcc.communityHall.paymentService.repository.TransRefundResRepository;
  13. import com.gcc.communityHall.paymentService.repository.TransactionResponseRepository;
  14. import com.gcc.communityHall.paymentService.repository.TransactionStatusLogRepository;
  15. import com.gcc.communityHall.paymentService.service.PaymentGatewayServiceImpl;
  16. import com.gcc.communityHall.repository.PaymentsRepository;
  17. import com.gcc.communityHall.service.BookingService;
  18. import com.gcc.communityHall.service.TransactionService;
  19. import com.google.gson.Gson;
  20. import jakarta.transaction.Transactional;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.http.ResponseEntity;
  23. import org.springframework.web.bind.annotation.GetMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import org.springframework.web.client.RestTemplate;
  26. import java.time.LocalDate;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. @RestController
  32. public class RefundController {
  33. @Autowired
  34. public RefundClientInfoRepository refundClientInfoRepository;
  35. @Autowired
  36. public TransactionResponseRepository transactionResponseRepository;
  37. @Autowired
  38. private TransactionStatusLogRepository transactionStatusLogRepository;
  39. @Autowired
  40. private PaymentConfig paymentConfig;
  41. @Autowired
  42. private PaymentsRepository paymentsRepository;
  43. @Autowired
  44. private BookingService bookingService;
  45. @Autowired
  46. private PaymentGatewayServiceImpl paymentGatewayService;
  47. @Autowired
  48. private TransRefundResRepository transRefundResRepository;
  49. @Autowired
  50. private TransactionService transactionService;
  51. @Autowired
  52. private PaymentGatewayServiceImpl paymentGatewayServiceImpl;
  53. @GetMapping(value = "/refundProcess")
  54. public ResponseEntity refundProcess() {
  55. try {
  56. List<Map<String, Object>> pendingTransactions = transactionResponseRepository.getTransactionResponseByStatus("temp");
  57. for (Map<String, Object> transactionRecord : pendingTransactions) {
  58. try {
  59. Map<String, Object> status = new HashMap<>();
  60. Map<String, Object> merchant = new HashMap<>();
  61. Map<String, Object> transaction = new HashMap<>();
  62. status.put("merchant", merchant);
  63. status.put("transaction", transaction);
  64. merchant.put("identifier", paymentConfig.getMerchantId());
  65. transaction.put("identifier", transactionRecord.get("txansaction_id"));
  66. transaction.put("requestType", "O");
  67. transaction.put("token", "");
  68. transaction.put("reference", "");
  69. System.out.println("=====================================" + transactionRecord.get("txansaction_id"));
  70. System.out.println(merchant);
  71. System.out.println(transaction);
  72. System.out.println(status);
  73. System.out.println("======================================");
  74. String bookingDate = transactionRecord.get("booking_date").toString();
  75. if (bookingDate != null && !bookingDate.isEmpty()) {
  76. String[] dateParts = bookingDate.split("-");
  77. String formattedDate = dateParts[2] + "-" + dateParts[1] + "-" + dateParts[0];
  78. transaction.put("dateTime", formattedDate);
  79. } else {
  80. transaction.put("dateTime", "");
  81. }
  82. Gson gson = new Gson();
  83. String json = gson.toJson(status);
  84. System.out.println("Hello json " + json);
  85. String encryptHxRefund = paymentGatewayService.encryptHxRefund(json);
  86. RestTemplate restTemplate = new RestTemplate();
  87. String url = "https://www.tpsl-india.in/PaymentGateway/merchant2.pg/" + paymentConfig.getMerchantId();
  88. ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, encryptHxRefund, String.class);
  89. String decryptHxTRefund = paymentGatewayService.decryptHxTRefund(postForEntity.getBody());
  90. Map<String, Object> myObj = new ObjectMapper().readValue(decryptHxTRefund, Map.class);
  91. Map<String, Object> paymentMethod = (Map<String, Object>) myObj.get("paymentMethod");
  92. Map<String, Object> response = (Map<String, Object>) paymentMethod.get("paymentTransaction");
  93. System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  94. System.out.println("hello2" + response);
  95. System.out.println("hello " + myObj);
  96. System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  97. TransactionStatusLog log = new TransactionStatusLog();
  98. log.setBookingId(transactionRecord.get("booking_id").toString());
  99. log.setUserId(transactionRecord.get("user_id").toString());
  100. log.setTransactionId(transactionRecord.get("txansaction_id").toString());
  101. // log.setStatus(response.get("status").toString());
  102. log.setErrorMessage(response.get("errorMessage").toString());
  103. log.setStatusMessage(response.get("statusMessage").toString());
  104. log.setStatusCode(response.get("statusCode").toString());
  105. log.setRefundIdentifier(response.get("refundIdentifier").toString());
  106. // log.setResponse(response.toString());
  107. log.setAmount(response.get("amount").toString());
  108. log.setBalanceAmt(response.get("balanceAmount").toString());
  109. log.setReceivedDate(response.get("dateTime").toString());
  110. TransactionStatusLog saveLog = transactionStatusLogRepository.save(log);
  111. TransactionResponse transRes = transactionResponseRepository.findById(Integer.parseInt(transactionRecord.get("id").toString())
  112. ).orElseThrow(() -> new Exception("TransactionResponse not found"));
  113. transRes.setStatusCode(log.getStatusCode());
  114. transRes.setAmount(log.getAmount());
  115. transRes.setTpslTxnId(response.get("identifier").toString());
  116. transRes.setResponse(response.toString());
  117. transRes.setDate(log.getReceivedDate());
  118. transactionResponseRepository.save(transRes);
  119. Payments payments = paymentsRepository.findByBookingId(Integer.valueOf(String.valueOf(transRes.getBookingId())));
  120. System.out.println("payments hello" + payments);
  121. if (payments != null && response.get("statusCode").toString().equals("0300")) {
  122. // payments.setBookingDetails(payments.getBookingDetails());
  123. payments.setTransactionId(transRes.getTxansactionId());
  124. payments.setAmountReceived(Double.parseDouble(response.get("amount").toString()));
  125. payments.setPaymentStatus("Success");
  126. payments.setCdate(new Date());
  127. paymentsRepository.save(payments);
  128. bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.REFUNDED, "Booking Not Confirmed. Amount will be refunded shortly.");
  129. intiateRefund(transRes.getBookingId(), Integer.parseInt(transRes.getUserId()));
  130. } else if (response.get("statusCode").toString().equals("0300")) {
  131. payments = new Payments();
  132. BookingDetails bookingsById = bookingService.getBookingsById(transRes.getBookingId());
  133. payments.setBookingDetails(bookingsById);
  134. payments.setTransactionId(transRes.getTxansactionId());
  135. payments.setAmountReceived(Double.parseDouble(response.get("amount").toString()));
  136. payments.setPaymentStatus("Success");
  137. payments.setCdate(new Date());
  138. paymentsRepository.save(payments);
  139. bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.REFUNDED, "Booking Not Confirmed. Amount will be refunded shortly.");
  140. intiateRefund(transRes.getBookingId(), Integer.parseInt(transRes.getUserId()));
  141. } else {
  142. payments = new Payments();
  143. payments.setBookingDetails(payments.getBookingDetails());
  144. payments.setTransactionId(transRes.getTxansactionId());
  145. try {
  146. payments.setAmountReceived(Double.parseDouble(response.get("amount") != null ? response.get("amount").toString() : "0"));//error here
  147. }catch (Exception e ){
  148. payments.setAmountReceived(0);
  149. }
  150. payments.setPaymentStatus("Not Completed");
  151. payments.setCdate(new Date());
  152. paymentsRepository.save(payments);
  153. System.out.println("bookingId "+transRes.getBookingId());
  154. bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.PAYMENT_FAILURE, "Booking Not Confirmed. Amount Not Received.");
  155. }
  156. updateRefundInfo(response, log);
  157. } catch (Exception ex) {
  158. System.err.println("Error processing transaction record: " + ex.getMessage());
  159. ex.printStackTrace();
  160. }
  161. }
  162. } catch (Exception e) {
  163. System.err.println("Error fetching or saving payment status: " + e.getMessage());
  164. e.printStackTrace();
  165. }
  166. return ResponseEntity.ok("refundProcess");
  167. }
  168. public void updateRefundInfo(Map<String, Object> response, TransactionStatusLog log) {
  169. if (response.get("refundDdetails") != null) {
  170. Map<String, Object> refundInfo = (Map<String, Object>) response.get("refundDdetails");
  171. List<Map<String, Object>> refundDetails = (List<Map<String, Object>>) refundInfo.get("RefundDetails");
  172. System.out.println("inifcon :" + refundDetails);
  173. if (refundDetails != null && !refundDetails.isEmpty()) {
  174. RefundResponse refundResponse = new RefundResponse();
  175. refundResponse.setAmount(response.get("amount") != null ? response.get("amount").toString() : "");
  176. refundResponse.setBalaceAmount(response.get("balanceAmount") != null ? response.get("balanceAmount").toString() : "");
  177. refundResponse.setTxansactionId(log.getTransactionId());
  178. Map<String, Object> mapRef = refundDetails.get(refundDetails.size() - 1);
  179. LocalDate df = LocalDate.now();
  180. try {
  181. df = LocalDate.parse(mapRef.get("REF_TXN_DATE").toString());
  182. } catch (Exception e) {
  183. df = LocalDate.now();
  184. }
  185. ;
  186. refundResponse.setDate(df);
  187. if (mapRef.get("STATUS") != null && mapRef.get("STATUS").toString().equals("INITIATED")) {
  188. refundResponse.setStatusCode("0400");
  189. } else {
  190. refundResponse.setStatusCode("0399");
  191. }
  192. refundResponse.setResponse(refundDetails.toString());
  193. refundResponse.setBookingId(Integer.parseInt(log.getBookingId()));
  194. refundResponse.setUserId(log.getUserId());
  195. RefundResponse saverRefundResponse = transRefundResRepository.save(refundResponse);
  196. Double amt = Double.parseDouble(response.get("amount").toString());
  197. Double balAmt = Double.parseDouble(response.get("balanceAmount").toString());
  198. if (amt != balAmt) {
  199. RefundTransaction refundTrans = transactionService.getRefundTrans(saverRefundResponse.getBookingId());
  200. if (refundTrans != null) {
  201. refundTrans.setAmountRefunded(amt - balAmt);
  202. refundTrans.setRefundStatus("0400");
  203. refundTrans.setRefundIdentifier(response.get("refundIdentifier").toString());
  204. refundTrans.setCdate(new Date());
  205. transactionService.saveRefundTransaction(refundTrans);
  206. } else {
  207. refundTrans = new RefundTransaction();
  208. refundTrans.setAmountRefunded(amt - balAmt);
  209. refundTrans.setRefundStatus("0400");
  210. refundTrans.setRefundIdentifier(response.get("refundIdentifier").toString());
  211. refundTrans.setCdate(new Date());
  212. refundTrans.setTransactionId(log.getTransactionId());
  213. BookingDetails bookingsById = bookingService.getBookingsById(Integer.parseInt(log.getBookingId()));
  214. refundTrans.setBookingDetails(bookingsById);
  215. transactionService.saveRefundTransaction(refundTrans);
  216. }
  217. bookingService.updateRefundStatus(saverRefundResponse.getBookingId(), BookingDetails.RefundStatus.COMPLETED);
  218. }
  219. saveRefundClientInfo(refundDetails, log.getUserId(), log.getBookingId(), log.getTransactionId());
  220. }
  221. }
  222. }
  223. public String saveRefundClientInfo(List<Map<String, Object>> refundDetail, String userId, String bookingId, String transactionId) {
  224. if (refundDetail == null || refundDetail.isEmpty()) {
  225. return "error";
  226. }
  227. for (Map<String, Object> refund : refundDetail) {
  228. RefundClientInfo refundClientInfo = new RefundClientInfo();
  229. refundClientInfo.setUserId(Integer.parseInt(userId));
  230. refundClientInfo.setBookingId(Integer.parseInt(bookingId));
  231. refundClientInfo.setTransactionId(transactionId);
  232. refundClientInfo.setClntRfndId(refund.get("clnt_rfnd_id") != null ? refund.get("clnt_rfnd_id").toString() : "");
  233. refundClientInfo.setArnNo(refund.get("ARN_Number") != null ? refund.get("ARN_Number").toString() : "");
  234. refundClientInfo.setGetRefTxnDate(refund.get("REF_TXN_DATE") != null ? refund.get("REF_TXN_DATE").toString() : "");
  235. refundClientInfo.setRefTxnAmt(refund.get("REF_TXN_AMT") != null ? refund.get("REF_TXN_AMT").toString() : "");
  236. refundClientInfo.setRefTxnId(refund.get("REF_TXN_ID") != null ? refund.get("REF_TXN_ID").toString() : "");
  237. refundClientInfo.setStatus(refund.get("STATUS") != null ? refund.get("STATUS").toString() : "");
  238. refundClientInfo.setSrno(refund.get("SR_NO") != null ? refund.get("SR_NO").toString() : "");
  239. refundClientInfoRepository.save(refundClientInfo);
  240. }
  241. return "success";
  242. }
  243. @Transactional
  244. public void intiateRefund(int bookingId, int userId) throws Exception {
  245. String s = paymentGatewayServiceImpl.refundTheAmount(bookingId, userId);
  246. if (s.equals("Refunded")) {
  247. bookingService.updateRefundStatus(bookingId, BookingDetails.RefundStatus.COMPLETED);
  248. } else {
  249. bookingService.updateRefundStatus(bookingId, BookingDetails.RefundStatus.FAILURE);
  250. }
  251. }
  252. }