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.

335 lines
17 KiB

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