updated Code add logger

This commit is contained in:
Mohd-Arham
2025-11-24 17:40:49 +05:30
parent f85aa7ec06
commit 89cb17b108
10 changed files with 1004 additions and 856 deletions
@@ -75,15 +75,16 @@ public class AgentServiceImpl implements AgentService {
@Override
public ResponseModel initiateBillFetch(FetchBillRequest fetchBillRequest) {
LOGGER.info("Initiating bill fetch for request ID: {} and MID :{} and billerId:{} ", fetchBillRequest.getReqId(),
fetchBillRequest.getMid(),fetchBillRequest.getBillerId());
String apiName = "InitiateBillFetch";
String txnId = helper.generateTxnId(15);
String requestId = fetchBillRequest.getReqId();
String mid = fetchBillRequest.getMid();
LOGGER.info("[{}] START | reqId={} | mid={} | billerId={}", apiName, requestId, mid,
fetchBillRequest.getBillerId());
helper.validateMerchant(mid);
String txnId = helper.generateTxnId(15);
String fullUrl = agentDtaUrl + BBPSConstant.BILL_FETCH_INTI;
fetchBillRequest.setClientReqId(txnId);
helper.validateMerchant(fetchBillRequest.getMid());
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
HttpHeaders headers = new HttpHeaders();
headers.set("x-digiledge-key", headersData.getXDigiledgeKey());
@@ -106,7 +107,7 @@ public class AgentServiceImpl implements AgentService {
LOGGER.debug("Outbound log created successfully for txnId: {} and reqId: {}", txnId, requestId);
try {
LOGGER.info("Calling external API: {}", fullUrl);
LOGGER.info("[{}] Calling external API | URL={}", apiName, fullUrl);
Map<String, String> responseMap = httpService.postRequest(fullUrl, forwardJson, headers);
String responseData = responseMap.get("data");
@@ -154,6 +155,7 @@ public class AgentServiceImpl implements AgentService {
LOGGER.warn("Thread interrupted during delay: {}", e.getMessage());
}
LOGGER.info("Calling internal method Fetching bill by ID: {}", billFetchByBillIdRequest.getBillId());
LOGGER.info("[{}] END | reqId={} | status={}", apiName, requestId, generalResponse.getStatus());
return fetchBillById(billFetchByBillIdRequest, inboundLog);
} catch (JsonProcessingException e) {
LOGGER.error("JSON Parsing Error: {}", e.getMessage(), e);
@@ -223,86 +225,89 @@ public class AgentServiceImpl implements AgentService {
@Override
public ResponseModel fetchBillById(BillFetchByBillIdRequest billFetchByBillIdRequest) {
String apiName = "FetchBillById";
String txnId = helper.generateTxnId(15);
String reqId = billFetchByBillIdRequest.getReqId();
String billId = billFetchByBillIdRequest.getBillId();
String fullUrl = agentDtaUrl + BBPSConstant.BILL_FETCH_BY_ID + billFetchByBillIdRequest.getBillId();
String apiName = "FetchBillById";
String reqId = billFetchByBillIdRequest.getReqId();
String billId = billFetchByBillIdRequest.getBillId();
String mid = billFetchByBillIdRequest.getMid();
LOGGER.info("Initiating {} API for billId={} and reqId={}", apiName, billId, reqId);
LOGGER.info("[{}] START | reqId={} | mid={} | billId={}", apiName, reqId, mid, billId);
// create inbound log (GET -> no request body)
InboundTraceLog inboundLog = helper.createInboundLog(reqId, txnId, apiName,
billFetchByBillIdRequest.getMid(), "GET", null);
helper.validateMerchant(mid);
String txnId = helper.generateTxnId(15);
String fullUrl = agentDtaUrl + BBPSConstant.BILL_FETCH_BY_ID + billFetchByBillIdRequest.getBillId();
try {
helper.validateMerchant(billFetchByBillIdRequest.getMid());
LOGGER.debug("Merchant validated successfully for MID={}", billFetchByBillIdRequest.getMid());
LOGGER.info("Initiating {} API for billId={} and reqId={}", apiName, billId, reqId);
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);
// create inbound log (GET -> no request body)
InboundTraceLog inboundLog = helper.createInboundLog(reqId, txnId, apiName, billFetchByBillIdRequest.getMid(),
"GET", null);
// create outbound log
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, reqId, apiName, fullUrl, "GET", null);
LOGGER.info("Calling external BBPS API for FetchBillById, URL={}", fullUrl);
Map<String, String> responseMap = httpService.getRequest(fullUrl, headers);
String responseData = responseMap.get("data");
LOGGER.info("Response received from BBPS for billId={} : {}", billId, responseData);
try {
LOGGER.debug("Merchant validated successfully for MID={}", billFetchByBillIdRequest.getMid());
// update outbound log with response
helper.updateOutboundLogResponse(outboundLog, responseData);
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();
Map<String, Object> dataMap = objectMapper.readValue(responseData,
new TypeReference<Map<String, Object>>() {
});
// create outbound log
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, reqId, apiName, fullUrl, "GET", null);
LOGGER.info("[{}] Calling BBPS FetchBillById API | URL={}", apiName, fullUrl);
Map<String, String> responseMap = httpService.getRequest(fullUrl, headers);
String responseData = responseMap.get("data");
LOGGER.debug("[{}] BBPS Response | billId={} | data={}", apiName, billId, responseData);
if (!dataMap.containsKey("genericResponse")) {
helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_RESPONSE");
LOGGER.warn("Invalid response structure received for billId={} | Response={}", billId, responseData);
throw new CustomException(String.valueOf(dataMap.get("message")), // message
String.valueOf(dataMap.get("status")), // status
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode
String.valueOf(dataMap.get("remarks")));
}
// update outbound log with response
helper.updateOutboundLogResponse(outboundLog, responseData);
BillDataResponse responseWrapper = objectMapper.convertValue(dataMap, BillDataResponse.class);
GeneralResp generalResp = responseWrapper.getGenericResponse();
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> dataMap = objectMapper.readValue(responseData,
new TypeReference<Map<String, Object>>() {
});
// update inbound log success and persist bill data
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
helper.saveOrUpdateBillData(responseWrapper, billFetchByBillIdRequest, txnId);
LOGGER.info("Bill data saved successfully for billId={} | Status={}", billId, generalResp.getStatus());
if (!dataMap.containsKey("genericResponse")) {
helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_RESPONSE");
LOGGER.warn("Invalid response structure received for billId={} | Response={}", billId, responseData);
throw new CustomException(String.valueOf(dataMap.get("message")), // message
String.valueOf(dataMap.get("status")), // status
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode
String.valueOf(dataMap.get("remarks")));
}
return responseModel;
BillDataResponse responseWrapper = objectMapper.convertValue(dataMap, BillDataResponse.class);
GeneralResp generalResp = responseWrapper.getGenericResponse();
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
} catch (JsonProcessingException e) {
LOGGER.error("Error parsing responseData JSON for billId={} : {}", billId, e.getMessage(), e);
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
throw new CustomException("Invalid JSON received from API");
}
// update inbound log success and persist bill data
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
helper.saveOrUpdateBillData(responseWrapper, billFetchByBillIdRequest, txnId);
LOGGER.info("Bill data saved successfully for billId={} | Status={}", billId, generalResp.getStatus());
LOGGER.info("[{}] END | reqId={} | status={}", apiName, reqId, generalResp.getStatus());
return responseModel;
} catch (JsonProcessingException e) {
LOGGER.error("Error parsing responseData JSON for billId={} : {}", billId, e.getMessage(), e);
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
throw new CustomException("Invalid JSON received from API");
}
}
@Override
public ResponseModel initiateBillValidate(FetchBillValidatestRequest fetchBillValidateRequest) {
LOGGER.info("Initiating bill Validate for request ID: {} and MID:{} and billerId:{}", fetchBillValidateRequest.getReqId(),
fetchBillValidateRequest.getMid(),fetchBillValidateRequest.getBillerId());
public ResponseModel initiateBillValidate(FetchBillValidatestRequest fetchBillValidateRequest) {
String apiName = "InitiateBillValidate";
String txnId = helper.generateTxnId(15);
String reqId = fetchBillValidateRequest.getReqId();
String mid = fetchBillValidateRequest.getMid();
LOGGER.info("[{}] START | reqId={} | mid={} | billerId={}", apiName, fetchBillValidateRequest, mid,
fetchBillValidateRequest.getBillerId());
helper.validateMerchant(fetchBillValidateRequest.getMid());
String txnId = helper.generateTxnId(15);
String fullUrl = agentDtaUrl + BBPSConstant.BILLER_VALIDATE_URL;
fetchBillValidateRequest.setClientReqId(txnId);
helper.validateMerchant(fetchBillValidateRequest.getMid());
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
HttpHeaders headers = new HttpHeaders();
@@ -327,7 +332,7 @@ public class AgentServiceImpl implements AgentService {
LOGGER.debug("Outbound log created successfully for txnId: {} and reqId: {}", txnId, reqId);
try {
LOGGER.info("Calling external API: {}", fullUrl);
LOGGER.info("[{}] Calling external API | URL={}", apiName, fullUrl);
Map<String, String> responseMap = httpService.postRequest(fullUrl, forwardJson, headers);
String responseData = responseMap.get("data");
@@ -375,6 +380,7 @@ public class AgentServiceImpl implements AgentService {
LOGGER.warn("Thread interrupted during delay: {}", e.getMessage());
}
LOGGER.info("Calling internal method Fetching bill by ID: {}", billFetchByBillIdRequest.getBillId());
LOGGER.info("[{}] END | reqId={} | status={}", apiName, reqId, generalResponse.getStatus());
return BillvaidateByBillId(billFetchByBillIdRequest, inboundLog);
} catch (JsonProcessingException e) {
LOGGER.error("JSON Parsing Error: {}", e.getMessage(), e);
@@ -452,13 +458,16 @@ public class AgentServiceImpl implements AgentService {
}
}
@Override
public ResponseModel initiateBillValidateByBillId(BillFetchByBillIdRequest billRequest) {
String apiName = "FetchBillById";
String txnId = helper.generateTxnId(15);
String reqId = billRequest.getReqId();
String billId = billRequest.getBillId();
String mid = billRequest.getMid();
LOGGER.info("[{}] START | reqId={} | mid={} | billId={}", apiName, billRequest, mid, billId);
helper.validateMerchant(billRequest.getMid());
String txnId = helper.generateTxnId(15);
String fullUrl = agentDtaUrl + BBPSConstant.BILL_VALIDATE_ID_URL + billRequest.getBillId();
LOGGER.info("Initiating {} API for billId={} and reqId={}", apiName, billId, reqId);
@@ -467,7 +476,6 @@ public class AgentServiceImpl implements AgentService {
InboundTraceLog inboundLog = helper.createInboundLog(reqId, txnId, apiName, billRequest.getMid(), "GET", null);
try {
helper.validateMerchant(billRequest.getMid());
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
HttpHeaders headers = new HttpHeaders();
@@ -488,26 +496,29 @@ public class AgentServiceImpl implements AgentService {
helper.updateOutboundLogResponse(outboundLog, responseData);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> dataMap = objectMapper.readValue(responseData, new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>(){});
Map<String, Object> dataMap = objectMapper.readValue(responseData,
new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {
});
if (!dataMap.containsKey("genericResponse")) {
helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_RESPONSE");
LOGGER.warn("Invalid response structure received for billId={} | Response={}", billId, responseData);
throw new CustomException(String.valueOf(dataMap.get("message")),
String.valueOf(dataMap.get("status")),
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")));
}
FetchBillValidateResponse responseWrapper = objectMapper.convertValue(dataMap, FetchBillValidateResponse.class);
FetchBillValidateResponse responseWrapper = objectMapper.convertValue(dataMap,
FetchBillValidateResponse.class);
GeneralResp generalResp = responseWrapper.getGenericResponse();
// update inbound log and save validation data
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
helper.saveBillValidation(responseWrapper, billRequest);
LOGGER.info("Bill validation saved successfully for billId: {}", billId);
return new ResponseModel(generalResp.getMessage(), generalResp.getStatus(), Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
LOGGER.info("[{}] END | reqId={} | status={}", apiName, reqId, generalResp.getStatus());
return new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
} catch (com.fasterxml.jackson.core.JsonProcessingException e) {
LOGGER.error("Error parsing ValidateByBillId JSON: {}", e.getMessage(), e);
@@ -515,19 +526,23 @@ public class AgentServiceImpl implements AgentService {
throw new CustomException("Invalid JSON received from API");
}
}
//================================== Bill fetch and Bill Validate by Client Req Id==================================//
// ================================== Bill fetch and Bill Validate by Client Req
// Id==================================//
@Override
public ResponseModel fetchBillValidateByClientReqId(BillValidateByClientIdRequest billValidateByClientIdRequest) {
String apiName = "FetchBillValidateByClientIdRequest";
String txnId = helper.generateTxnId(15);
String reqId = billValidateByClientIdRequest.getReqId();
String clientReqId = billValidateByClientIdRequest.getClientReqId();
String mid = billValidateByClientIdRequest.getMid();
LOGGER.info("[{}] START | reqId={} | mid={} | clientReqId={}", apiName, reqId, mid,
billValidateByClientIdRequest.getClientReqId());
helper.validateMerchant(billValidateByClientIdRequest.getMid());
String txnId = helper.generateTxnId(15);
Optional<BillInitiate> billInitiate = billIntiateRepository.findByReqId(clientReqId);
if (billInitiate.isEmpty()) {
throw new CustomException("No Bill Found for given Client Req Id");
@@ -535,8 +550,6 @@ public class AgentServiceImpl implements AgentService {
String fullUrl = agentDtaUrl + BBPSConstant.BILL_VALIDATE_BY_CLIENT_REQ_ID + billInitiate.get().getTxnId();
helper.validateMerchant(billValidateByClientIdRequest.getMid());
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
HttpHeaders headers = new HttpHeaders();
headers.set("x-digiledge-key", headersData.getXDigiledgeKey());
@@ -553,7 +566,7 @@ public class AgentServiceImpl implements AgentService {
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, reqId, apiName, fullUrl, "GET", null);
try {
LOGGER.info("Calling BBPS API FetchBillValidateByClientReqId, URL={}", fullUrl);
LOGGER.info("[{}] Calling BBPS API | URL={} | reqId={}", apiName, fullUrl, reqId);
Map<String, String> responseMap = httpService.getRequest(fullUrl, headers);
String responseData = responseMap.get("data");
@@ -575,7 +588,7 @@ public class AgentServiceImpl implements AgentService {
GeneralResp generalResp = responseWrapper.getGenericResponse();
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
LOGGER.info("[{}] END | reqId={} | status={}", apiName, reqId, generalResp.getStatus());
return new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
@@ -589,9 +602,14 @@ public class AgentServiceImpl implements AgentService {
public ResponseModel fetchBillByClientReqId(BillFetchByClientIdRequest billFetchByClientIdRequest) {
String apiName = "FetchBillByClientIdRequest";
String txnId = helper.generateTxnId(15);
String reqId = billFetchByClientIdRequest.getReqId();
String clientReqId = billFetchByClientIdRequest.getClientReqId();
String mid = billFetchByClientIdRequest.getMid();
LOGGER.info("[{}] START | reqId={} | mid={} | clientReqId={}", apiName, reqId, mid,
billFetchByClientIdRequest.getClientReqId());
helper.validateMerchant(billFetchByClientIdRequest.getMid());
String txnId = helper.generateTxnId(15);
Optional<BillInitiate> billInitiate = billIntiateRepository.findByReqId(clientReqId);
if (billInitiate.isEmpty()) {
@@ -600,8 +618,6 @@ public class AgentServiceImpl implements AgentService {
String fullUrl = agentDtaUrl + BBPSConstant.BILL_VALIDATE_BY_CLIENT_REQ_ID + billInitiate.get().getTxnId();
helper.validateMerchant(billFetchByClientIdRequest.getMid());
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
HttpHeaders headers = new HttpHeaders();
headers.set("x-digiledge-key", headersData.getXDigiledgeKey());
@@ -618,7 +634,7 @@ public class AgentServiceImpl implements AgentService {
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, reqId, apiName, fullUrl, "GET", null);
try {
LOGGER.info("Calling BBPS API FetchBillByClientReqId, URL={}", fullUrl);
LOGGER.info("[{}] Calling external API | URL={} | reqId={}", apiName, fullUrl, reqId);
Map<String, String> responseMap = httpService.getRequest(fullUrl, headers);
String responseData = responseMap.get("data");
@@ -639,7 +655,7 @@ public class AgentServiceImpl implements AgentService {
GeneralResp generalResp = responseWrapper.getGenericResponse();
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
LOGGER.info("[{}] END | reqId={} | status={}", apiName, reqId, generalResp.getStatus());
return new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
@@ -649,7 +665,6 @@ public class AgentServiceImpl implements AgentService {
}
}
/*
* @Override public ResponseModel
* initiateBillValidate(FetchBillValidatestRequest fetchBillValidateRequest) {
@@ -837,5 +852,4 @@ public class AgentServiceImpl implements AgentService {
* }
*/
}