saved initate payment and checktxn status

This commit is contained in:
Lincpay Tech
2025-11-07 18:38:09 +05:30
parent 58db98cc06
commit af7cdaf5d7
15 changed files with 518 additions and 113 deletions
@@ -22,17 +22,25 @@ import com.billpaymentservice.bbps.dto.response.BillDataResponse;
import com.billpaymentservice.bbps.dto.response.BillPaymentResponse;
import com.billpaymentservice.bbps.dto.response.FetchBillResponse;
import com.billpaymentservice.bbps.dto.response.TransactionStatusResponse;
import com.billpaymentservice.bbps.entites.AgentFundTransfer;
import com.billpaymentservice.bbps.entites.BharatConnectMaster;
import com.billpaymentservice.bbps.entites.BillInitiate;
import com.billpaymentservice.bbps.entites.BillPayment;
import com.billpaymentservice.bbps.entites.InboundTraceLog;
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
import com.billpaymentservice.bbps.entites.PaymentInitiate;
import com.billpaymentservice.bbps.entites.TransactionMaster;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.repository.AgentFundTransferRepository;
import com.billpaymentservice.bbps.repository.BharatConnectMasterRepository;
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
import com.billpaymentservice.bbps.repository.PaymentIntiateRepository;
import com.billpaymentservice.bbps.repository.PaymentRepository;
import com.billpaymentservice.bbps.repository.TransactionMasterRepository;
import com.billpaymentservice.bbps.service.PaymentService;
import com.billpaymentservice.bbps.util.BBPSConstant;
import com.billpaymentservice.bbps.util.EntityRelationshipUtil;
import com.billpaymentservice.bbps.util.Helper;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -50,20 +58,27 @@ public class PaymentServiceImpl implements PaymentService {
private final PaymentRepository paymentRepository;
private final InboundTraceLogRepository inboundTraceLogRepository;
private final OutboundTraceLogRepository outboundTraceLogRepository;
private final PaymentIntiateRepository paymentIntiateRepository;
private final TransactionMasterRepository transactionMasterRepository;
private final AgentFundTransferRepository agentFundTransferRepository;
public PaymentServiceImpl(BharatConnectMasterRepository beneficiaryDetailsRepository, HttpService httpService,
Helper helper, PaymentRepository paymentRepository, InboundTraceLogRepository inboundTraceLogRepository,
OutboundTraceLogRepository outboundTraceLogRepository) {
OutboundTraceLogRepository outboundTraceLogRepository,
PaymentIntiateRepository paymentIntiateRepository,
TransactionMasterRepository transactionMasterRepository,
AgentFundTransferRepository agentFundTransferRepository) {
this.beneficiaryDetailsRepository = beneficiaryDetailsRepository;
this.httpService = httpService;
this.helper = helper;
this.paymentRepository = paymentRepository;
this.inboundTraceLogRepository = inboundTraceLogRepository;
this.outboundTraceLogRepository = outboundTraceLogRepository;
this.paymentIntiateRepository = paymentIntiateRepository;
this.transactionMasterRepository = transactionMasterRepository;
this.agentFundTransferRepository = agentFundTransferRepository;
}
private static final Logger LOGGER = LoggerFactory.getLogger(MasterDataServiceImpl.class);
@Override
public ResponseModel initiatePayment(BillPaymentRequest billPaymentRequest) {
String apiName = "InitiatePayment";
@@ -114,27 +129,35 @@ public class PaymentServiceImpl implements PaymentService {
paymentRepository.save(entity);
helper.updateInboundLogSuccess(inboundLog, responseData, generalResponse.getStatus());
PaymentInitiate paymentInitiate = new PaymentInitiate();
paymentInitiate.setTxnId(txnId);
paymentInitiate.setReqId(requestId);
paymentInitiate.setBankTxnId(billPaymentResponse.getTxnId());
paymentInitiate.setStatus(billPaymentResponse.getStatus());
paymentIntiateRepository.save(paymentInitiate);
// ✅ Trigger transaction status check
TransactionStatusRequest transactionStatusRequest = new TransactionStatusRequest();
transactionStatusRequest.setTxnId(entity.getTxnId());
transactionStatusRequest.setTxnId(billPaymentResponse.getTxnId());
transactionStatusRequest.setReqId(requestId);
try {
Thread.sleep(2000); // Initial wait before first status check
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.error("Thread interrupted: {}", e.getMessage());
}
//calling the method to check transaction status
return checkTrasactionStatus(transactionStatusRequest, inboundLog);
} catch (JsonProcessingException e) {
LOGGER.error("❌ JSON Parsing Error: {}", e.getMessage());
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
throw new CustomException("Invalid JSON received from API");
} catch (CustomException ce) {
throw ce; // will be handled by global exception handler
} catch (Exception e) {
LOGGER.error("❌ Exception in initiatePayment(): {}", e.getMessage(), e);
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "EXCEPTION");
throw new CustomException("Payment initiation failed: " + e.getMessage());
}
}
}
public ResponseModel checkTrasactionStatus(TransactionStatusRequest transactionStatusRequest,InboundTraceLog inboundLog) {
String apiName = "CheckTransactionStatus";
@@ -152,11 +175,6 @@ public class PaymentServiceImpl implements PaymentService {
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, requestId, apiName, url, "GET", null);
long startTime = System.currentTimeMillis();
long maxWaitTimeMs = 40000;
long intervalMs = 5000;
while (true) {
Map<String, String> responseMap = httpService.getRequest(url, headers);
String responseData = responseMap.get("data");
LOGGER.info("Transaction status response: {}", responseData);
@@ -178,20 +196,26 @@ public class PaymentServiceImpl implements PaymentService {
TransactionStatusResponse responseWrapper = objectMapper.convertValue(dataMap, TransactionStatusResponse.class);
GeneralResp generalResp = responseWrapper.getGeneralResp();
System.out.println("Response Wrapper:"+responseWrapper);
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
TransactionMaster transactionMaster = mapper.map(responseWrapper.getTransactionDAO(), TransactionMaster.class);
transactionMaster.setTxnId(txnId);
transactionMaster.setReqId(transactionStatusRequest.getReqId());
EntityRelationshipUtil.linkTransactionMasterRelationships(transactionMaster);
transactionMasterRepository.save(transactionMaster);
AgentFundTransfer agentFundTransfer = mapper.map(responseWrapper.getAgentFundTransfer(), AgentFundTransfer.class);
agentFundTransfer.setTxnId(txnId);
agentFundTransfer.setReqId(transactionStatusRequest.getReqId());
agentFundTransfer.setTransactionMasterId(transactionMaster.getBankTxnId());
agentFundTransferRepository.save(agentFundTransfer);
String status = generalResp.getStatus();
LOGGER.info("Current transaction status: {}", status);
if ("INPROGRESS".equalsIgnoreCase(status)) {
if (System.currentTimeMillis() - startTime > maxWaitTimeMs) {
helper.updateInboundLogFailure(inboundLog, "Timeout after 40 seconds", "FAILED");
throw new CustomException("Timeout waiting for transaction completion", "FAILED", 408, "Transaction still in progress");
}
Thread.sleep(intervalMs);
continue;
}
ResponseModel responseModel = new ResponseModel(
generalResp.getMessage(),
generalResp.getStatus(),
@@ -206,14 +230,9 @@ public class PaymentServiceImpl implements PaymentService {
LOGGER.error("Error parsing transaction status JSON: {}", e.getMessage());
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
throw new CustomException("Invalid JSON received from API");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "INTERRUPTED");
throw new CustomException("Thread interrupted during transaction check");
}
}
}
}
@Override
public ResponseModel checkTrasactionStatus(TransactionStatusRequest transactionStatusRequest) {
helper.validateMerchant(transactionStatusRequest.getMid());
@@ -230,7 +249,7 @@ public class PaymentServiceImpl implements PaymentService {
headers.setContentType(MediaType.APPLICATION_JSON);
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, transactionStatusRequest.getMid(), "POST",
requestJson);
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, url, "GET", null);
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, url, "GET", null);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> responseMap = httpService.getRequest(url, headers);
@@ -277,7 +296,7 @@ public class PaymentServiceImpl implements PaymentService {
}
}
/*
/* PollBack logic for the fetchTxnByTxnId API
* @Override public ResponseModel fetchTxnByTxnId(BillFetchByBillIdRequest
* billFetchByBillIdRequest) {
*
@@ -327,4 +346,75 @@ public class PaymentServiceImpl implements PaymentService {
* }
*/
/*
* public ResponseModel checkTrasactionStatus(TransactionStatusRequest
* transactionStatusRequest,InboundTraceLog inboundLog) { String apiName =
* "CheckTransactionStatus"; String txnId = helper.generateTxnId(15); //our
* genrated txnId String requestId = transactionStatusRequest.getReqId(); String
* url = paymentUrl + BBPSConstant.CHECK_TRANSACTION_URL +
* transactionStatusRequest.getTxnId(); ;
*
* BharatConnectMaster headersData =
* beneficiaryDetailsRepository.findByOrgId("ORG001"); HttpHeaders headers = new
* HttpHeaders(); headers.set("x-digiledge-key",
* headersData.getXDigiledgeKey()); headers.set("agentId",
* headersData.getAgentId()); headers.set("tenantId",
* headersData.getTenantId());
* headers.setContentType(MediaType.APPLICATION_JSON); ObjectMapper objectMapper
* = new ObjectMapper();
*
* OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, requestId,
* apiName, url, "GET", null);
*
* long startTime = System.currentTimeMillis(); long maxWaitTimeMs = 40000; long
* intervalMs = 5000;
*
* while (true) { Map<String, String> responseMap = httpService.getRequest(url,
* headers); String responseData = responseMap.get("data");
* LOGGER.info("Transaction status response: {}", responseData);
*
* helper.updateOutboundLogResponse(outboundLog, responseData);
*
* try { Map<String, Object> dataMap = objectMapper.readValue(responseData, new
* TypeReference<Map<String, Object>>() {});
*
* if (!dataMap.containsKey("generalResp")) {
* helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_RESPONSE");
* throw new CustomException( String.valueOf(dataMap.get("message")),
* String.valueOf(dataMap.get("status")),
* Integer.parseInt(String.valueOf(dataMap.get("statusCode"))),
* String.valueOf(dataMap.get("remarks")) ); }
*
* TransactionStatusResponse responseWrapper =
* objectMapper.convertValue(dataMap, TransactionStatusResponse.class);
* GeneralResp generalResp = responseWrapper.getGeneralResp();
*
* String status = generalResp.getStatus();
* LOGGER.info("Current transaction status: {}", status);
*
* if ("INPROGRESS".equalsIgnoreCase(status)) { if (System.currentTimeMillis() -
* startTime > maxWaitTimeMs) { helper.updateInboundLogFailure(inboundLog,
* "Timeout after 40 seconds", "FAILED"); throw new
* CustomException("Timeout waiting for transaction completion", "FAILED", 408,
* "Transaction still in progress"); }
*
* Thread.sleep(intervalMs); continue; }
*
* ResponseModel responseModel = new ResponseModel( generalResp.getMessage(),
* generalResp.getStatus(), Integer.parseInt(generalResp.getStatusCode()),
* responseWrapper );
*
* helper.updateInboundLogSuccess(inboundLog, responseData,
* generalResp.getStatus()); return responseModel;
*
* } catch (JsonProcessingException e) {
* LOGGER.error("Error parsing transaction status JSON: {}", e.getMessage());
* helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
* throw new CustomException("Invalid JSON received from API"); } catch
* (InterruptedException e) { Thread.currentThread().interrupt();
* helper.updateInboundLogFailure(inboundLog, e.getMessage(), "INTERRUPTED");
* throw new CustomException("Thread interrupted during transaction check"); } }
* }
*
*
*/