package com.gcc.communityHall.paymentService.scheduler;/* // Import necessary packages package com.gcc.communityHall.paymentService.Scheduler; import com.fasterxml.jackson.databind.ObjectMapper; import com.gcc.communityHall.entity.BookingDetails; import com.gcc.communityHall.entity.Payments; import com.gcc.communityHall.entity.RefundTransaction; import com.gcc.communityHall.paymentService.config.PaymentConfig; import com.gcc.communityHall.paymentService.entity.RefundClientInfo; import com.gcc.communityHall.paymentService.entity.RefundResponse; import com.gcc.communityHall.paymentService.entity.TransactionResponse; import com.gcc.communityHall.paymentService.entity.TransactionStatusLog; import com.gcc.communityHall.paymentService.repository.RefundClientInfoRepository; import com.gcc.communityHall.paymentService.repository.TransRefundResRepository; import com.gcc.communityHall.paymentService.repository.TransactionResponseRepository; import com.gcc.communityHall.paymentService.repository.TransactionStatusLogRepository; import com.gcc.communityHall.paymentService.service.PaymentGatewayServiceImpl; import com.gcc.communityHall.repository.PaymentsRepository; import com.gcc.communityHall.service.BookingService; import com.gcc.communityHall.service.TransactionService; import com.google.gson.Gson; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.time.LocalDate; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @EnableScheduling public class PaymentStatusScheduler { @Autowired public RefundClientInfoRepository refundClientInfoRepository; @Autowired public TransactionResponseRepository transactionResponseRepository; @Autowired private TransactionStatusLogRepository transactionStatusLogRepository; @Autowired private PaymentConfig paymentConfig; @Autowired private PaymentsRepository paymentsRepository; @Autowired private BookingService bookingService; @Autowired private PaymentGatewayServiceImpl paymentGatewayService; @Autowired private TransRefundResRepository transRefundResRepository; @Autowired private TransactionService transactionService; @Autowired private PaymentGatewayServiceImpl paymentGatewayServiceImpl; // @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Kolkata") //executed at 12:00 AM // @Scheduled(fixedRate = 900000) @Scheduled(fixedRate = 30000) public void fetchAndSavePaymentStatus() { // System.out.println("Fetching Payment Status...At 12:00 AM IST"); System.out.println("Fetching Payment Status...At 15 min Testing"); try { List> pendingTransactions = transactionResponseRepository.getTransResBycdateandStatus("temp"); for (Map transactionRecord : pendingTransactions) { try { Map status = new HashMap<>(); Map merchant = new HashMap<>(); Map transaction = new HashMap<>(); status.put("merchant", merchant); status.put("transaction", transaction); merchant.put("identifier", paymentConfig.getMerchantId()); transaction.put("identifier", transactionRecord.get("txansaction_id")); transaction.put("requestType", "O"); transaction.put("token", ""); transaction.put("reference", ""); System.out.println("====================================="+ transactionRecord.get("txansaction_id")); System.out.println(merchant); System.out.println(transaction); System.out.println(status); System.out.println(transactionRecord.toString()); System.out.println("======================================"); String bookingDate = transactionRecord.get("booking_date").toString(); // System.out.println("cdate check"+transactionRecord.get("cdate").toString()); String cdate = transactionRecord.get("cdate").toString(); if (cdate != null && !cdate.isEmpty()) { String[] dateParts1 = cdate.split(" "); String[] dateParts = dateParts1[0].split("-"); String formattedDate = dateParts[2] + "-" + dateParts[1] + "-" + dateParts[0]; transaction.put("dateTime", formattedDate); System.out.println("formattedDate"+formattedDate); } else { transaction.put("dateTime", ""); } Gson gson = new Gson(); String json = gson.toJson(status); System.out.println("Hello json "+json); String encryptHxRefund = paymentGatewayService.encryptHxRefund(json); RestTemplate restTemplate = new RestTemplate(); String url = "https://www.tpsl-india.in/PaymentGateway/merchant2.pg/" + paymentConfig.getMerchantId(); ResponseEntity postForEntity = restTemplate.postForEntity(url, encryptHxRefund, String.class); String decryptHxTRefund = paymentGatewayService.decryptHxTRefund(postForEntity.getBody()); Map myObj = new ObjectMapper().readValue(decryptHxTRefund, Map.class); Map paymentMethod = (Map)myObj.get("paymentMethod"); Map response = (Map)paymentMethod.get("paymentTransaction"); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println("hello2" + response); System.out.println("hello "+myObj); System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); TransactionStatusLog log = new TransactionStatusLog(); log.setBookingId(transactionRecord.get("booking_id").toString()); log.setUserId(transactionRecord.get("user_id").toString()); log.setTransactionId(transactionRecord.get("txansaction_id").toString()); // log.setStatus(response.get("status").toString()); log.setErrorMessage(response.get("errorMessage").toString()); log.setStatusMessage(response.get("statusMessage").toString()); log.setStatusCode(response.get("statusCode").toString()); log.setRefundIdentifier(response.get("refundIdentifier").toString()); // log.setResponse(response.toString()); log.setAmount(response.get("amount").toString()); log.setBalanceAmt(response.get("balanceAmount").toString()); log.setReceivedDate(response.get("dateTime").toString()); TransactionStatusLog saveLog = transactionStatusLogRepository.save(log); TransactionResponse transRes = transactionResponseRepository.findById(Integer.parseInt(transactionRecord.get("id").toString()) ).orElseThrow(() -> new Exception("TransactionResponse not found")); transRes.setStatusCode(log.getStatusCode()); transRes.setAmount(log.getAmount()); transRes.setTpslTxnId(response.get("identifier").toString()); transRes.setResponse(response.toString()); transRes.setDate(log.getReceivedDate()); transactionResponseRepository.save(transRes); Payments payments = paymentsRepository.findByBookingId(Integer.valueOf(String.valueOf(transRes.getBookingId()))); System.out.println("payments hello"+payments); if(payments!=null && response.get("statusCode").toString().equals("0300")) { // payments.setBookingDetails(payments.getBookingDetails()); payments.setTransactionId(transRes.getTxansactionId()); payments.setAmountReceived(Double.parseDouble(response.get("amount").toString())); payments.setPaymentStatus("Success"); Date date = new Date(payments.getCdate().getTime()); //paymentsRepository.updatePaymentInfo(payments.getPaymentId(),payments.getTransactionId(),payments.getAmountReceived(),payments.getPaymentStatus(),transRes.getBookingId()); bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.REFUNDED, "Booking Not Confirmed. Amount will be refunded shortly."); intiateRefund(transRes.getBookingId(),Integer.parseInt(transRes.getUserId())); }else if(response.get("statusCode").toString().equals("0300")){ payments = new Payments(); BookingDetails bookingsById = bookingService.getBookingsById(transRes.getBookingId()); payments.setBookingDetails(bookingsById); payments.setTransactionId(transRes.getTxansactionId()); payments.setAmountReceived(Double.parseDouble(response.get("amount").toString())); payments.setPaymentStatus("Success"); payments.setCdate(new Date()); //paymentsRepository.insertPaymentinfo(payments.getPaymentId(),payments.getTransactionId(),payments.getAmountReceived(),payments.getPaymentStatus(),transRes.getBookingId(),payments.getCdate() ); bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.REFUNDED, "Booking Not Confirmed. Amount will be refunded shortly."); intiateRefund(transRes.getBookingId(),Integer.parseInt(transRes.getUserId())); } else{ payments.setBookingDetails(payments.getBookingDetails()); payments.setTransactionId(transRes.getTxansactionId()); payments.setAmountReceived(Double.parseDouble(response.get("amount")!=null?response.get("amount").toString():"0")); payments.setPaymentStatus("Not Completed"); payments.setCdate(new Date()); paymentsRepository.save(payments); bookingService.updateBookingStatus(transRes.getBookingId(), BookingDetails.BookingStatus.PAYMENT_FAILURE, "Booking Not Confirmed. Amount Not Received."); } updateRefundInfo(response,log); } catch (Exception ex) { System.err.println("Error processing transaction record: " + ex.getMessage()); ex.printStackTrace(); } } } catch (Exception e) { System.err.println("Error fetching or saving payment status: " + e.getMessage()); e.printStackTrace(); } } public void updateRefundInfo(Map response,TransactionStatusLog log){ if(response.get("refundDdetails") != null ){ Map refundInfo= (Map) response.get("refundDdetails"); List> refundDetails = (List>) refundInfo.get("RefundDetails"); System.out.println("inifcon :"+ refundDetails); if(refundDetails != null && !refundDetails.isEmpty()){ RefundResponse refundResponse = new RefundResponse(); refundResponse.setAmount(response.get("amount")!=null?response.get("amount").toString():""); refundResponse.setBalaceAmount(response.get("balanceAmount")!=null?response.get("balanceAmount").toString():""); refundResponse.setTxansactionId(log.getTransactionId()); Map mapRef = refundDetails.get(refundDetails.size()-1); LocalDate df = LocalDate.now(); try{df =LocalDate.parse(mapRef.get("REF_TXN_DATE").toString());}catch (Exception e){df = LocalDate.now();}; refundResponse.setDate(df); if(mapRef.get("STATUS")!=null && mapRef.get("STATUS").toString().equals("INITIATED")){ refundResponse.setStatusCode("0400"); }else{ refundResponse.setStatusCode("0399"); } refundResponse.setResponse(refundDetails.toString()); refundResponse.setBookingId(Integer.parseInt(log.getBookingId())); refundResponse.setUserId(log.getUserId()); RefundResponse saverRefundResponse = transRefundResRepository.save(refundResponse); Double amt = Double.parseDouble(response.get("amount").toString()); Double balAmt = Double.parseDouble(response.get("balanceAmount").toString()); if(amt != balAmt ) { RefundTransaction refundTrans = transactionService.getRefundTrans(saverRefundResponse.getBookingId()); if (refundTrans != null) { refundTrans.setAmountRefunded(amt - balAmt); refundTrans.setRefundStatus("0400"); refundTrans.setRefundIdentifier(response.get("refundIdentifier").toString()); refundTrans.setCdate(new Date()); transactionService.saveRefundTransaction(refundTrans); } else { refundTrans = new RefundTransaction(); refundTrans.setAmountRefunded(amt - balAmt); refundTrans.setRefundStatus("0400"); refundTrans.setRefundIdentifier(response.get("refundIdentifier").toString()); refundTrans.setCdate(new Date()); refundTrans.setTransactionId(log.getTransactionId()); BookingDetails bookingsById = bookingService.getBookingsById(Integer.parseInt(log.getBookingId())); refundTrans.setBookingDetails(bookingsById); transactionService.saveRefundTransaction(refundTrans); } bookingService.updateRefundStatus(saverRefundResponse.getBookingId(),BookingDetails.RefundStatus.COMPLETED); } saveRefundClientInfo(refundDetails,log.getUserId() ,log.getBookingId(), log.getTransactionId()); } } } public String saveRefundClientInfo(List> refundDetail,String userId,String bookingId,String transactionId) { if (refundDetail == null || refundDetail.isEmpty()) { return "error"; } for (Map refund : refundDetail) { RefundClientInfo refundClientInfo = new RefundClientInfo(); refundClientInfo.setUserId(Integer.parseInt(userId)); refundClientInfo.setBookingId(Integer.parseInt(bookingId)); refundClientInfo.setTransactionId(transactionId); refundClientInfo.setClntRfndId(refund.get("clnt_rfnd_id")!=null?refund.get("clnt_rfnd_id").toString():""); refundClientInfo.setArnNo(refund.get("ARN_Number")!=null?refund.get("ARN_Number").toString():""); refundClientInfo.setGetRefTxnDate(refund.get("REF_TXN_DATE")!=null?refund.get("REF_TXN_DATE").toString():""); refundClientInfo.setRefTxnAmt(refund.get("REF_TXN_AMT")!=null?refund.get("REF_TXN_AMT").toString():""); refundClientInfo.setRefTxnId(refund.get("REF_TXN_ID")!=null?refund.get("REF_TXN_ID").toString():""); refundClientInfo.setStatus(refund.get("STATUS")!=null?refund.get("STATUS").toString():""); refundClientInfo.setSrno(refund.get("SR_NO")!=null?refund.get("SR_NO").toString():""); refundClientInfoRepository.save(refundClientInfo); } return "success"; } @Transactional public void intiateRefund(int bookingId,int userId) throws Exception { String s = paymentGatewayServiceImpl.refundTheAmount(bookingId, userId); if(s.equals("Refunded")){ bookingService.updateRefundStatus(bookingId,BookingDetails.RefundStatus.COMPLETED); }else { bookingService.updateRefundStatus(bookingId,BookingDetails.RefundStatus.FAILURE); } } } */