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.

338 lines
17 KiB

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