resolve conflict by me
This commit is contained in:
@@ -5,7 +5,7 @@ import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class BillPaymentRequest {
|
||||
public class BillPaymentRequest extends BaseClass {
|
||||
|
||||
private String tenantId;
|
||||
private String refId;
|
||||
|
||||
@@ -4,5 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.billpaymentservice.bbps.entites.InboundTraceLog;
|
||||
public interface InboundTraceLogRepository extends JpaRepository<InboundTraceLog, Long> {
|
||||
|
||||
|
||||
public InboundTraceLog findByRequestId(String requestId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.billpaymentservice.bbps.serviceImpl;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -17,8 +18,12 @@ import com.billpaymentservice.bbps.dto.response.BillDataResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.FetchBillResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.FetchBillValidatestResponse;
|
||||
import com.billpaymentservice.bbps.entites.BharatConnectMaster;
|
||||
import com.billpaymentservice.bbps.entites.InboundTraceLog;
|
||||
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.repository.BharatConnectMasterRepository;
|
||||
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
|
||||
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
|
||||
import com.billpaymentservice.bbps.service.AgentService;
|
||||
import com.billpaymentservice.bbps.util.BBPSConstant;
|
||||
import com.billpaymentservice.bbps.util.Helper;
|
||||
@@ -42,89 +47,177 @@ public class AgentServiceImpl implements AgentService {
|
||||
private final BharatConnectMasterRepository beneficiaryDetailsRepository;
|
||||
|
||||
public AgentServiceImpl(RestClient restClient, HttpService httpService, Helper helper,
|
||||
BharatConnectMasterRepository beneficiaryDetailsRepository) {
|
||||
BharatConnectMasterRepository beneficiaryDetailsRepository,InboundTraceLogRepository inboundTraceLogRepository,
|
||||
OutboundTraceLogRepository outboundTraceLogRepository) {
|
||||
this.restClient = restClient;
|
||||
this.httpService = httpService;
|
||||
this.helper = helper;
|
||||
this.beneficiaryDetailsRepository = beneficiaryDetailsRepository;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseModel initiateBillFetch(FetchBillRequest fetchBillRequest) {
|
||||
String apiName = "InitiateBillFetch";
|
||||
String txnId = helper.generateTxnId(15);
|
||||
String requestId = fetchBillRequest.getReqId();
|
||||
String url = agentDtaUrl + BBPSConstant.BILL_FETCH_INTI;
|
||||
|
||||
helper.validateMerchant(fetchBillRequest.getMid());
|
||||
helper.validateMerchant(fetchBillRequest.getMid());
|
||||
|
||||
String url = agentDtaUrl + BBPSConstant.BILL_FETCH_INTI;
|
||||
System.out.println(url);
|
||||
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);
|
||||
|
||||
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
String requestJson = helper.convertObjectToJson(fetchBillRequest);
|
||||
|
||||
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 & outbound logs
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnId, apiName, fetchBillRequest.getMid(), "POST", requestJson);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, requestId, apiName, url, "POST", requestJson);
|
||||
|
||||
ResponseModel responseModel = new ResponseModel();
|
||||
try {
|
||||
Map<String, String> responseMap = httpService.postRequest(url, requestJson, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
// ✅ Convert request to JSON
|
||||
String json = objectMapper.writeValueAsString(fetchBillRequest);
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
helper.updateInboundLogFailure(inboundLog, "Empty response from BBPS", "FAILED");
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
|
||||
// ✅ Call the API using httpService
|
||||
Map<String, String> responseMap = httpService.postRequest(url, json, headers);
|
||||
log.info("Response -: " + responseMap);
|
||||
String responseData = responseMap.get("data");
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_STRUCTURE");
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
|
||||
log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
FetchBillResponse fetchBillResponse = objectMapper.convertValue(dataMap, FetchBillResponse.class);
|
||||
GeneralResp generalResponse = fetchBillResponse.getGenericResponse();
|
||||
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResponse.getStatus());
|
||||
|
||||
// ✅ Convert JSON string → Map
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
// ✅ Directly call fetchBillById (no polling)
|
||||
BillFetchByBillIdRequest billFetchByBillIdRequest = new BillFetchByBillIdRequest();
|
||||
billFetchByBillIdRequest.setBillId(fetchBillResponse.getBillId());
|
||||
billFetchByBillIdRequest.setMid(fetchBillRequest.getMid());
|
||||
billFetchByBillIdRequest.setReqId(requestId);
|
||||
|
||||
// ✅ Validate generalResp
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
return fetchBillById(billFetchByBillIdRequest, inboundLog);
|
||||
|
||||
// ✅ Convert to your custom DTO
|
||||
FetchBillResponse fetchBillResponse = objectMapper.convertValue(dataMap, FetchBillResponse.class);
|
||||
GeneralResp generalResponse = fetchBillResponse.getGenericResponse();
|
||||
//
|
||||
// start a new thread here
|
||||
helper.startPolling(fetchBillResponse, res -> {
|
||||
BillFetchByBillIdRequest pollReq = new BillFetchByBillIdRequest();
|
||||
pollReq.setBillId(res.getBillId());
|
||||
return fetchBillById(pollReq);
|
||||
}, 3, // interval in seconds
|
||||
15 // total time in seconds
|
||||
);
|
||||
|
||||
// ✅ Build final ResponseModel
|
||||
responseModel = new ResponseModel(generalResponse.getMessage(), generalResponse.getStatus(),
|
||||
Integer.parseInt(generalResponse.getStatusCode()), fetchBillResponse);
|
||||
|
||||
} catch (CustomException ex) {
|
||||
log.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
return ex.getResponseModel(); // Directly return ResponseModel from exception
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
responseModel.setStatus("500");
|
||||
responseModel.setMessage("Exception occurred");
|
||||
responseModel.setData(e.getMessage());
|
||||
}
|
||||
|
||||
return responseModel;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.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;
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "EXCEPTION");
|
||||
throw new CustomException("Bill Fetch initiation failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ResponseModel fetchBillById(BillFetchByBillIdRequest billFetchByBillIdRequest, InboundTraceLog inboundLog) {
|
||||
String apiName = "FetchBillById";
|
||||
String txnId = helper.generateTxnId(15);
|
||||
String requestId = billFetchByBillIdRequest.getReqId();
|
||||
String billId = billFetchByBillIdRequest.getBillId();
|
||||
String url = agentDtaUrl + BBPSConstant.BILL_FETCH_BY_ID + "1ced7566-1add-4e01-9ed6-04cd67ac9684";
|
||||
|
||||
helper.validateMerchant(billFetchByBillIdRequest.getMid());
|
||||
|
||||
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);
|
||||
|
||||
// ✅ Loop setup
|
||||
long startTime = System.currentTimeMillis();
|
||||
long maxWaitTimeMs = 40000; // 40 seconds
|
||||
long intervalMs = 5000; // 5 seconds
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
Map<String, String> responseMap = httpService.getRequest(url, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
log.info("📦 Bill FetchById Response: {}", responseData);
|
||||
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
|
||||
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
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"))
|
||||
);
|
||||
}
|
||||
|
||||
BillDataResponse responseWrapper = objectMapper.convertValue(dataMap, BillDataResponse.class);
|
||||
GeneralResp generalResp = responseWrapper.getGenericResponse();
|
||||
|
||||
String status = generalResp.getStatus();
|
||||
log.info("📡 Current Bill Status: {}", status);
|
||||
|
||||
// ✅ Check for "INPROGRESS" or pending state
|
||||
if ("INPROGRESS".equalsIgnoreCase(status)) {
|
||||
if (System.currentTimeMillis() - startTime > maxWaitTimeMs) {
|
||||
helper.updateInboundLogFailure(inboundLog, "Timeout after 40 seconds", "FAILED");
|
||||
throw new CustomException("Timeout waiting for bill fetch completion", "FAILED", 408, "Bill fetch still in progress");
|
||||
}
|
||||
|
||||
Thread.sleep(intervalMs);
|
||||
continue; // retry again
|
||||
}
|
||||
|
||||
// ✅ Final successful response
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return new ResponseModel(
|
||||
generalResp.getMessage(),
|
||||
generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()),
|
||||
responseWrapper
|
||||
);
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing BillById 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 bill status check");
|
||||
} catch (CustomException ce) {
|
||||
throw ce;
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in fetchBillById(): {}", e.getMessage(), e);
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "EXCEPTION");
|
||||
throw new CustomException("Bill Fetch by ID failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseModel fetchBillById(BillFetchByBillIdRequest billFetchByBillIdRequest) {
|
||||
|
||||
@@ -179,140 +272,140 @@ public class AgentServiceImpl implements AgentService {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseModel initiateBillValidate(FetchBillValidatestRequest fetchBillValidateRequest) {
|
||||
|
||||
ResponseModel responseModel = new ResponseModel();
|
||||
try {
|
||||
|
||||
helper.validateMerchant(fetchBillValidateRequest.getMid());
|
||||
|
||||
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
|
||||
if (headersData == null) {
|
||||
responseModel = new ResponseModel("Tenant Id Is Null", "Not Found", BBPSConstant.BADREQUEST,
|
||||
headersData);
|
||||
return responseModel;
|
||||
}
|
||||
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();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
String json = objectMapper.writeValueAsString(fetchBillValidateRequest);
|
||||
String url = agentDtaUrl + BBPSConstant.BILLER_VALIDATE_URL;
|
||||
Map<String, String> responseMap = httpService.postRequest(url, json, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
|
||||
// ✅ Convert JSON string → Map
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
// ✅ Validate generalResp
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
|
||||
// ✅ Convert to your custom DTO
|
||||
FetchBillValidatestResponse fetchBillResponse = objectMapper.convertValue(dataMap,
|
||||
FetchBillValidatestResponse.class);
|
||||
GeneralResp generalResponse = fetchBillResponse.getGenericResponse();
|
||||
|
||||
// ✅ Build final ResponseModel
|
||||
responseModel = new ResponseModel(generalResponse.getMessage(), generalResponse.getStatus(),
|
||||
Integer.parseInt(generalResponse.getStatusCode()), fetchBillResponse);
|
||||
|
||||
} catch (CustomException ex) {
|
||||
log.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
return ex.getResponseModel(); // Directly return ResponseModel from exception
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
responseModel.setStatus("500");
|
||||
responseModel.setMessage("Exception occurred");
|
||||
responseModel.setData(e.getMessage());
|
||||
}
|
||||
|
||||
return responseModel;
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ResponseModel initiateBillValidateByBillId(BillFetchByBillIdRequest billRequest) {
|
||||
|
||||
ResponseModel responseModel = new ResponseModel();
|
||||
try {
|
||||
helper.validateMerchant(billRequest.getMid());
|
||||
|
||||
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
|
||||
if (headersData == null) {
|
||||
responseModel = new ResponseModel("Tenant Id Is Null", "Not Found", BBPSConstant.BADREQUEST,
|
||||
headersData);
|
||||
return responseModel;
|
||||
}
|
||||
|
||||
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();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
String json = objectMapper.writeValueAsString(billRequest);
|
||||
String url = agentDtaUrl + BBPSConstant.BILL_ID_URL + billRequest.getBillId();
|
||||
|
||||
log.info("Full URL" + url);
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
// ✅ Call the API using httpService
|
||||
Map<String, String> responseMap = httpService.getRequest(url, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
|
||||
// ✅ Convert JSON string → Map
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
// ✅ Validate generalResp
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
|
||||
// ✅ Convert to your custom DTO
|
||||
BillDataResponse fetchBillResponse = objectMapper.convertValue(dataMap, BillDataResponse.class);
|
||||
GeneralResp generalResponse = fetchBillResponse.getGenericResponse();
|
||||
|
||||
// ✅ Build final ResponseModel
|
||||
responseModel = new ResponseModel(generalResponse.getRemarks(), generalResponse.getStatus(),
|
||||
Integer.parseInt(generalResponse.getStatusCode()), fetchBillResponse);
|
||||
|
||||
} catch (CustomException ex) {
|
||||
log.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
return ex.getResponseModel(); // Directly return ResponseModel from exception
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
responseModel.setStatus("500");
|
||||
responseModel.setMessage("Exception occurred");
|
||||
responseModel.setData(e.getMessage());
|
||||
}
|
||||
|
||||
return responseModel;
|
||||
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* @Override public ResponseModel
|
||||
* initiateBillValidate(FetchBillValidatestRequest fetchBillValidateRequest) {
|
||||
*
|
||||
* ResponseModel responseModel = new ResponseModel(); try {
|
||||
*
|
||||
* helper.validateMerchant(fetchBillValidateRequest.getMid());
|
||||
*
|
||||
* BharatConnectMaster headersData =
|
||||
* beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
*
|
||||
* if (headersData == null) { responseModel = new
|
||||
* ResponseModel("Tenant Id Is Null", "Not Found", BBPSConstant.BADREQUEST,
|
||||
* headersData); return responseModel; } 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();
|
||||
* objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); String
|
||||
* json = objectMapper.writeValueAsString(fetchBillValidateRequest); String url
|
||||
* = agentDtaUrl + BBPSConstant.BILLER_VALIDATE_URL; Map<String, String>
|
||||
* responseMap = httpService.postRequest(url, json, headers); String
|
||||
* responseData = responseMap.get("data");
|
||||
*
|
||||
* log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
*
|
||||
* if (responseData == null || responseData.isEmpty()) { throw new
|
||||
* CustomException("Empty response from BBPS server"); }
|
||||
*
|
||||
* // ✅ Convert JSON string → Map Map<String, Object> dataMap =
|
||||
* objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>()
|
||||
* { });
|
||||
*
|
||||
* // ✅ Validate generalResp if (!dataMap.containsKey("genericResponse")) {
|
||||
* throw new CustomException("Invalid response structure from BBPS"); }
|
||||
*
|
||||
* // ✅ Convert to your custom DTO FetchBillValidatestResponse fetchBillResponse
|
||||
* = objectMapper.convertValue(dataMap, FetchBillValidatestResponse.class);
|
||||
* GeneralResp generalResponse = fetchBillResponse.getGenericResponse();
|
||||
*
|
||||
* // ✅ Build final ResponseModel responseModel = new
|
||||
* ResponseModel(generalResponse.getMessage(), generalResponse.getStatus(),
|
||||
* Integer.parseInt(generalResponse.getStatusCode()), fetchBillResponse);
|
||||
*
|
||||
* } catch (CustomException ex) { log.warn("Merchant validation failed: {}",
|
||||
* ex.getMessage()); return ex.getResponseModel(); // Directly return
|
||||
* ResponseModel from exception } catch (Exception e) {
|
||||
* log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
* responseModel.setStatus("500");
|
||||
* responseModel.setMessage("Exception occurred");
|
||||
* responseModel.setData(e.getMessage()); }
|
||||
*
|
||||
* return responseModel; }
|
||||
*
|
||||
* @Override public ResponseModel
|
||||
* initiateBillValidateByBillId(BillFetchByBillIdRequest billRequest) {
|
||||
*
|
||||
* ResponseModel responseModel = new ResponseModel(); try {
|
||||
* helper.validateMerchant(billRequest.getMid());
|
||||
*
|
||||
* BharatConnectMaster headersData =
|
||||
* beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
*
|
||||
* if (headersData == null) { responseModel = new
|
||||
* ResponseModel("Tenant Id Is Null", "Not Found", BBPSConstant.BADREQUEST,
|
||||
* headersData); return responseModel; }
|
||||
*
|
||||
* 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();
|
||||
* objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); String
|
||||
* json = objectMapper.writeValueAsString(billRequest); String url = agentDtaUrl
|
||||
* + BBPSConstant.BILL_ID_URL + billRequest.getBillId();
|
||||
*
|
||||
* log.info("Full URL" + url);
|
||||
* objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
*
|
||||
* // ✅ Call the API using httpService Map<String, String> responseMap =
|
||||
* httpService.getRequest(url, headers); String responseData =
|
||||
* responseMap.get("data");
|
||||
*
|
||||
* log.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
*
|
||||
* if (responseData == null || responseData.isEmpty()) { throw new
|
||||
* CustomException("Empty response from BBPS server"); }
|
||||
*
|
||||
* // ✅ Convert JSON string → Map Map<String, Object> dataMap =
|
||||
* objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>()
|
||||
* { });
|
||||
*
|
||||
* // ✅ Validate generalResp if (!dataMap.containsKey("genericResponse")) {
|
||||
* throw new CustomException("Invalid response structure from BBPS"); }
|
||||
*
|
||||
* // ✅ Convert to your custom DTO BillDataResponse fetchBillResponse =
|
||||
* objectMapper.convertValue(dataMap, BillDataResponse.class); GeneralResp
|
||||
* generalResponse = fetchBillResponse.getGenericResponse();
|
||||
*
|
||||
* // ✅ Build final ResponseModel responseModel = new
|
||||
* ResponseModel(generalResponse.getRemarks(), generalResponse.getStatus(),
|
||||
* Integer.parseInt(generalResponse.getStatusCode()), fetchBillResponse);
|
||||
*
|
||||
* } catch (CustomException ex) { log.warn("Merchant validation failed: {}",
|
||||
* ex.getMessage()); return ex.getResponseModel(); // Directly return
|
||||
* ResponseModel from exception } catch (Exception e) {
|
||||
* log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
* responseModel.setStatus("500");
|
||||
* responseModel.setMessage("Exception occurred");
|
||||
* responseModel.setData(e.getMessage()); }
|
||||
*
|
||||
* return responseModel;
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
+119
-146
@@ -87,53 +87,6 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
|
||||
}
|
||||
|
||||
/* ---------- Trace helper methods ---------- */
|
||||
private InboundTraceLog createInboundLog(String requestId, String txnId, String apiName, String mid,
|
||||
String httpMethod, String requestPayload) {
|
||||
InboundTraceLog inboundLog = new InboundTraceLog(requestId, txnId, apiName, mid == null ? "N/A" : mid,
|
||||
"BBPS_ORG", httpMethod, requestPayload == null ? "N/A" : requestPayload);
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
return inboundLog;
|
||||
}
|
||||
|
||||
private void updateInboundLogSuccess(InboundTraceLog inboundLog, String responsePayload, String responseCode) {
|
||||
if (inboundLog == null)
|
||||
return;
|
||||
inboundLog.setResponsePayload(responsePayload);
|
||||
inboundLog.setResponseCode(responseCode);
|
||||
inboundLog.setStatus("SUCCESS");
|
||||
inboundLog.setResponseTime(LocalDateTime.now());
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
}
|
||||
|
||||
private void updateInboundLogFailure(InboundTraceLog inboundLog, String responsePayload, String responseCode) {
|
||||
if (inboundLog == null)
|
||||
return;
|
||||
inboundLog.setResponsePayload(responsePayload);
|
||||
inboundLog.setResponseCode(responseCode);
|
||||
inboundLog.setStatus("FAILED");
|
||||
inboundLog.setResponseTime(LocalDateTime.now());
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
}
|
||||
|
||||
private OutboundTraceLog createOutboundLog(String txnId, String requestId, String apiName, String url,
|
||||
String httpMethod, String requestPayload) {
|
||||
OutboundTraceLog outboundLog = new OutboundTraceLog(txnId, requestId, apiName, url, httpMethod,
|
||||
requestPayload == null ? "N/A" : requestPayload);
|
||||
outboundTraceLogRepository.save(outboundLog);
|
||||
return outboundLog;
|
||||
}
|
||||
|
||||
private void updateOutboundLogResponse(OutboundTraceLog outboundLog, String responsePayload) {
|
||||
if (outboundLog == null)
|
||||
return;
|
||||
outboundLog.setResponsePayload(responsePayload);
|
||||
outboundLog.setResponseReceiveTime(LocalDateTime.now());
|
||||
outboundTraceLogRepository.save(outboundLog);
|
||||
}
|
||||
|
||||
/* ---------- End helpers ---------- */
|
||||
|
||||
@Override
|
||||
public ResponseModel getMasterData(MasterRequest masterRequest) {
|
||||
LOGGER.info("Request DTO: {}", masterRequest);
|
||||
@@ -142,10 +95,10 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String apiName = "GetMasterData";
|
||||
String requestId = masterRequest.getReqId();
|
||||
String requestJson = helper.convertObjectToJson(masterRequest);
|
||||
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, masterRequest.getMid(), "POST",
|
||||
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, masterRequest.getMid(), "POST",
|
||||
requestJson);
|
||||
|
||||
|
||||
try {
|
||||
// Merchant Validation
|
||||
helper.validateMerchant(masterRequest.getMid());
|
||||
@@ -159,7 +112,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
BharatConnectMaster headersData = beneficiaryDetailsRepository.findByOrgId("ORG001");
|
||||
log.info("Response -: " + headersData);
|
||||
if (headersData == null) {
|
||||
updateInboundLogFailure(inboundLog, "Tenant Id Is Null", "NOT_FOUND");
|
||||
helper.updateInboundLogFailure(inboundLog, "Tenant Id Is Null", "NOT_FOUND");
|
||||
return new ResponseModel("Tenant Id Is Null", "Not Found", BBPSConstant.BADREQUEST, headersData);
|
||||
}
|
||||
|
||||
@@ -167,20 +120,20 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.MASTER_CATEGORY_URL + headersData.getTenantId();
|
||||
LOGGER.info("Calling external API: {}", fullUrl);
|
||||
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(fullUrl);
|
||||
String responseData = responseMap.get("data");
|
||||
LOGGER.info("Response Data: {}", responseData);
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode
|
||||
@@ -194,13 +147,13 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException ex) {
|
||||
LOGGER.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
@@ -210,7 +163,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
|
||||
@Override
|
||||
public ResponseModel getBillerByBillerId(BillerDetailRequest billerDetailRequest) {
|
||||
|
||||
|
||||
helper.validateMerchant(billerDetailRequest.getMid());
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "GetBillerByBillerId";
|
||||
@@ -219,16 +172,16 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.BILLER_DATA_FETCH_URL + billerId;
|
||||
|
||||
String requestJson = helper.convertObjectToJson(billerDetailRequest);
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, billerDetailRequest.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, billerDetailRequest.getMid(), "POST",
|
||||
requestJson);
|
||||
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(fullUrl);
|
||||
String responseData = responseMap.get("data");
|
||||
log.info("data is :{}", responseData);
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
try {
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
@@ -236,7 +189,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, 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")));
|
||||
@@ -253,12 +206,12 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
}
|
||||
|
||||
@@ -271,7 +224,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String apiName = "GetCustomerParamByBillerId";
|
||||
String requestId = customerParamRequest.getReqId();
|
||||
String requestJson = helper.convertObjectToJson(customerParamRequest);
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, customerParamRequest.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, customerParamRequest.getMid(), "POST",
|
||||
requestJson);
|
||||
|
||||
try {
|
||||
@@ -285,19 +238,19 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String billerId = customerParamRequest.getBillerId();
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.CUSTOMPARAM_URL + billerId;
|
||||
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(fullUrl);
|
||||
String responseData = responseMap.get("data");
|
||||
log.info("data is :{}", responseData);
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -310,12 +263,12 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException ex) {
|
||||
LOGGER.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
@@ -338,7 +291,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String apiName = "FetchMasterDataCategoryWise";
|
||||
String requestId = masterRequest.getReqId();
|
||||
String requestJson = helper.convertObjectToJson(masterRequest);
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, masterRequest.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, masterRequest.getMid(), "POST",
|
||||
requestJson);
|
||||
|
||||
try {
|
||||
@@ -348,20 +301,20 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
BBPSConstant.BILLER_URL, masterRequest.getBillerName(), masterRequest.getCategory(),
|
||||
masterRequest.getPageStart(), masterRequest.getPageEnd());
|
||||
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(fullUrl);
|
||||
String responseData = responseMap.get("data");
|
||||
LOGGER.info("Response Data: {}", responseData);
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode
|
||||
@@ -375,12 +328,12 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException ex) {
|
||||
LOGGER.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
@@ -394,15 +347,15 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String apiName = "GetDispositionDetails";
|
||||
String requestId = dispositionDetailRequest.getReqId();
|
||||
;
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, dispositionDetailRequest.getMid(),
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, dispositionDetailRequest.getMid(),
|
||||
"GET", "N/A");
|
||||
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.DISPOSTION_URL;
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
Map<String, String> responseMap = httpService.getRequest(fullUrl);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
try {
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
@@ -411,7 +364,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -437,12 +390,12 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException ex) {
|
||||
LOGGER.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
@@ -457,25 +410,25 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "FetchCustomerParamGroup";
|
||||
String requestId = requestModel.getReqId();
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
helper.convertObjectToJson(requestModel));
|
||||
|
||||
try {
|
||||
helper.validateMerchant(requestModel.getMid());
|
||||
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.CUSTOMER_PARAM_GROUP + requestModel.getBillerId();
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> apiResponse = httpService.getRequest(fullUrl);
|
||||
String responseData = apiResponse.get("data").toString();
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -489,13 +442,13 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException ex) {
|
||||
LOGGER.warn("Merchant validation failed: {}", ex.getMessage());
|
||||
@@ -511,27 +464,27 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "FetchAllMasterAmountOption";
|
||||
String requestId = requestModel.getReqId();
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
helper.convertObjectToJson(requestModel));
|
||||
|
||||
try {
|
||||
helper.validateMerchant(requestModel.getMid());
|
||||
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.AMOUNT_OPTIONS + requestModel.getBillerId();
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> apiResponse = httpService.getRequest(fullUrl);
|
||||
log.info("Response" + apiResponse);
|
||||
String responseData = apiResponse.get("data").toString();
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -544,13 +497,13 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException e) {
|
||||
LOGGER.error("Custom Exception: {}", e.getMessage());
|
||||
@@ -566,27 +519,27 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "FetchPlanCategorie";
|
||||
String requestId = requestModel.getReqId();
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
helper.convertObjectToJson(requestModel));
|
||||
|
||||
try {
|
||||
helper.validateMerchant(requestModel.getMid());
|
||||
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.PLAN_CATEGORIES + requestModel.getBillerId();
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> apiResponse = httpService.getRequest(fullUrl);
|
||||
log.info("Response" + apiResponse);
|
||||
String responseData = apiResponse.get("data").toString();
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -599,13 +552,13 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException e) {
|
||||
LOGGER.error("Custom Exception: {}", e.getMessage());
|
||||
@@ -620,7 +573,7 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "FetchInitiatingChannel";
|
||||
String requestId = requestModel.getReqId();
|
||||
InboundTraceLog inboundLog = createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST",
|
||||
helper.convertObjectToJson(requestModel));
|
||||
|
||||
try {
|
||||
@@ -629,20 +582,20 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
String fullUrl = masterRegisterUrl + BBPSConstant.INITIATING_CHANNEL;
|
||||
LOGGER.info("Calling Master API: {}", fullUrl);
|
||||
|
||||
OutboundTraceLog outboundLog = createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, fullUrl, "GET", null);
|
||||
|
||||
Map<String, String> apiResponse = httpService.getRequest(fullUrl);
|
||||
log.info("Response" + apiResponse);
|
||||
String responseData = apiResponse.get("data").toString();
|
||||
|
||||
updateOutboundLogResponse(outboundLog, responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
@@ -655,13 +608,13 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
|
||||
updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException e) {
|
||||
LOGGER.error("Custom Exception: {}", e.getMessage());
|
||||
@@ -669,58 +622,78 @@ public class MasterDataServiceImpl implements MasterDataService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseModel fetchPlanByCategories(PlanRequest requestModel) {
|
||||
LOGGER.info("Fetching Initiating Fetch Plan Category By Bidder Id for MID={}", requestModel.getMid());
|
||||
try {
|
||||
helper.validateMerchant(requestModel.getMid());
|
||||
LOGGER.info("Fetching Initiating Fetch Plan Category By Biller Id for MID={}", requestModel.getMid());
|
||||
|
||||
// if (!"Valid".equalsIgnoreCase(validateMerchant)) {
|
||||
// LOGGER.warn("Merchant validation failed: {}", validateMerchant);
|
||||
// return new ResponseModel("Merchant validation failed", validateMerchant, BBPSConstant.BADREQUEST, null);
|
||||
// }
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "FetchPlanByCategories";
|
||||
String requestId = requestModel.getReqId();
|
||||
|
||||
String url = String.format("%s%s?billerId=%s%s%s%s&page=%d&pagesize=%d", masterRegisterUrl,
|
||||
BBPSConstant.PLANS_URL, requestModel.getBillerId(),
|
||||
requestModel.getCategory() != null ? "&category=" + requestModel.getCategory() : "",
|
||||
requestModel.getAmountInRupees() != null ? "&amountInRupees=" + requestModel.getAmountInRupees()
|
||||
: "",
|
||||
requestModel.getCircle() != null ? "&circle=" + requestModel.getCircle() : "",
|
||||
requestModel.getPage(), requestModel.getPagesize());
|
||||
helper.validateMerchant(requestModel.getMid());
|
||||
|
||||
// String fullUrl = masterRegisterUrl + BBPSConstant.PLANS_URL + url;
|
||||
LOGGER.info("Calling Master API: {}", url);
|
||||
String url = String.format("%s%s?billerId=%s%s%s%s&page=%d&pagesize=%d",
|
||||
masterRegisterUrl,
|
||||
BBPSConstant.PLANS_URL,
|
||||
requestModel.getBillerId(),
|
||||
requestModel.getCategory() != null ? "&category=" + requestModel.getCategory() : "",
|
||||
requestModel.getAmountInRupees() != null ? "&amountInRupees=" + requestModel.getAmountInRupees() : "",
|
||||
requestModel.getCircle() != null ? "&circle=" + requestModel.getCircle() : "",
|
||||
requestModel.getPage(),
|
||||
requestModel.getPagesize()
|
||||
);
|
||||
|
||||
Map<String, String> apiResponse = httpService.getRequest(url);
|
||||
log.info("Response" + apiResponse);
|
||||
String responseData = apiResponse.get("data").toString();
|
||||
LOGGER.info("Calling Master API: {}", url);
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
// Convert request to JSON and create inbound & outbound logs
|
||||
String requestJson = helper.convertObjectToJson(requestModel);
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, requestModel.getMid(), "POST", requestJson);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, url, "GET", null);
|
||||
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
throw new CustomException(String.valueOf(dataMap.get("message")), // message
|
||||
String.valueOf(dataMap.get("status")), // status
|
||||
Integer.parseInt(String.valueOf(dataMap.get("statusCode"))), // statusCode (int)
|
||||
String.valueOf(dataMap.get("remarks")));
|
||||
}
|
||||
try {
|
||||
Map<String, String> apiResponse = httpService.getRequest(url);
|
||||
String responseData = apiResponse.get("data");
|
||||
log.info("Response Data: {}", responseData);
|
||||
|
||||
PlanResponse responseWrapper = objectMapper.convertValue(dataMap, PlanResponse.class);
|
||||
GeneralResp generalResp = responseWrapper.getGeneralResp();
|
||||
// Update outbound log with response
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
ResponseModel responseModel = new ResponseModel(generalResp.getMessage(), generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()), responseWrapper);
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
|
||||
|
||||
return responseModel;
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
// Failure response
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, 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"))
|
||||
);
|
||||
}
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException e) {
|
||||
LOGGER.error("Custom Exception: {}", e.getMessage());
|
||||
throw e; // Rethrow custom exceptions to be handled by the controller
|
||||
}
|
||||
// Success response
|
||||
PlanResponse responseWrapper = objectMapper.convertValue(dataMap, PlanResponse.class);
|
||||
GeneralResp generalResp = responseWrapper.getGeneralResp();
|
||||
|
||||
ResponseModel responseModel = new ResponseModel(
|
||||
generalResp.getMessage(),
|
||||
generalResp.getStatus(),
|
||||
Integer.parseInt(generalResp.getStatusCode()),
|
||||
responseWrapper
|
||||
);
|
||||
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResp.getStatus());
|
||||
return responseModel;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Error parsing responseData JSON: {}", e.getMessage());
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
} catch (CustomException e) {
|
||||
|
||||
LOGGER.error("Custom Exception: {}", e.getMessage());
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "Custom_exception");
|
||||
throw e; // Rethrow for controller-level handling
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.billpaymentservice.bbps.serviceImpl;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
@@ -23,8 +24,12 @@ import com.billpaymentservice.bbps.dto.response.FetchBillResponse;
|
||||
import com.billpaymentservice.bbps.dto.response.TransactionStatusResponse;
|
||||
import com.billpaymentservice.bbps.entites.BharatConnectMaster;
|
||||
import com.billpaymentservice.bbps.entites.BillPayment;
|
||||
import com.billpaymentservice.bbps.entites.InboundTraceLog;
|
||||
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.repository.BharatConnectMasterRepository;
|
||||
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
|
||||
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
|
||||
import com.billpaymentservice.bbps.repository.PaymentRepository;
|
||||
import com.billpaymentservice.bbps.service.PaymentService;
|
||||
import com.billpaymentservice.bbps.util.BBPSConstant;
|
||||
@@ -43,99 +48,30 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
private final HttpService httpService;
|
||||
private final Helper helper;
|
||||
private final PaymentRepository paymentRepository;
|
||||
private final InboundTraceLogRepository inboundTraceLogRepository;
|
||||
private final OutboundTraceLogRepository outboundTraceLogRepository;
|
||||
|
||||
public PaymentServiceImpl(BharatConnectMasterRepository beneficiaryDetailsRepository, HttpService httpService,
|
||||
Helper helper, PaymentRepository paymentRepository) {
|
||||
Helper helper, PaymentRepository paymentRepository, InboundTraceLogRepository inboundTraceLogRepository,
|
||||
OutboundTraceLogRepository outboundTraceLogRepository) {
|
||||
this.beneficiaryDetailsRepository = beneficiaryDetailsRepository;
|
||||
this.httpService = httpService;
|
||||
this.helper = helper;
|
||||
this.paymentRepository = paymentRepository;
|
||||
this.inboundTraceLogRepository = inboundTraceLogRepository;
|
||||
this.outboundTraceLogRepository = outboundTraceLogRepository;
|
||||
}
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MasterDataServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public ResponseModel initiatePayment(BillPaymentRequest billPaymentRequest) {
|
||||
String apiName = "InitiatePayment";
|
||||
String txnId = helper.generateTxnId(15);
|
||||
String requestId = billPaymentRequest.getReqId();
|
||||
String url = paymentUrl + BBPSConstant.PAYMENT_URL;
|
||||
|
||||
String url = paymentUrl + BBPSConstant.PAYMENT_URL;
|
||||
System.out.println(url);
|
||||
|
||||
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);
|
||||
|
||||
ResponseModel responseModel = new ResponseModel();
|
||||
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
// ✅ Convert request to JSON
|
||||
String json = objectMapper.writeValueAsString(billPaymentRequest);
|
||||
|
||||
// ✅ Call the API using httpService
|
||||
Map<String, String> responseMap = httpService.postRequest(url, json, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
LOGGER.info("📦 Bill Fetch API Response: {}", responseData);
|
||||
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
|
||||
// ✅ Convert JSON string → Map
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData,
|
||||
new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
|
||||
// ✅ Validate generalResp
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
|
||||
// ✅ Convert to your custom DTO
|
||||
BillPaymentResponse billPaymentResponse = objectMapper.convertValue(dataMap, BillPaymentResponse.class);
|
||||
System.out.print(billPaymentResponse);
|
||||
GeneralResp generalResponse = billPaymentResponse.getGenericResponse();
|
||||
|
||||
//
|
||||
// start a new thread here
|
||||
/*
|
||||
* helper.startPolling(fetchBillResponse, res -> { BillFetchByBillIdRequest
|
||||
* pollReq = new BillFetchByBillIdRequest(); pollReq.setBillId(res.getBillId());
|
||||
* return fetchBillById(pollReq); }, 3, // interval in seconds 15 // total time
|
||||
* in seconds );
|
||||
*/
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
BillPayment entity = mapper.map(billPaymentResponse, BillPayment.class);
|
||||
paymentRepository.save(entity);
|
||||
TransactionStatusRequest transactionStatusRequest=new TransactionStatusRequest();
|
||||
//call the checkTransactionStatus
|
||||
transactionStatusRequest.setTxnId(helper.generateTxnId(15));
|
||||
System.out.println(transactionStatusRequest);
|
||||
responseModel= checkTrasactionStatus(transactionStatusRequest);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
responseModel.setStatus("500");
|
||||
responseModel.setMessage("Exception occurred");
|
||||
responseModel.setData(e.getMessage());
|
||||
}
|
||||
|
||||
return responseModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseModel checkTrasactionStatus(TransactionStatusRequest transactionStatusRequest) {
|
||||
|
||||
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());
|
||||
@@ -143,22 +79,171 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
String requestJson = helper.convertObjectToJson(billPaymentRequest);
|
||||
|
||||
// ✅ Create inbound & outbound logs
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnId, apiName, billPaymentRequest.getMid(), "POST", requestJson);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnId, requestId, apiName, url, "POST", requestJson);
|
||||
|
||||
try {
|
||||
Map<String, String> responseMap = httpService.postRequest(url, requestJson, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
|
||||
LOGGER.info("📦 Payment API Response: {}", responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
if (responseData == null || responseData.isEmpty()) {
|
||||
helper.updateInboundLogFailure(inboundLog, "Empty response from BBPS", "FAILED");
|
||||
throw new CustomException("Empty response from BBPS server");
|
||||
}
|
||||
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
|
||||
if (!dataMap.containsKey("genericResponse")) {
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, "INVALID_STRUCTURE");
|
||||
throw new CustomException("Invalid response structure from BBPS");
|
||||
}
|
||||
|
||||
BillPaymentResponse billPaymentResponse = objectMapper.convertValue(dataMap, BillPaymentResponse.class);
|
||||
GeneralResp generalResponse = billPaymentResponse.getGenericResponse();
|
||||
|
||||
ModelMapper mapper = new ModelMapper();
|
||||
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||
BillPayment entity = mapper.map(billPaymentResponse, BillPayment.class);
|
||||
paymentRepository.save(entity);
|
||||
|
||||
helper.updateInboundLogSuccess(inboundLog, responseData, generalResponse.getStatus());
|
||||
|
||||
// ✅ Trigger transaction status check
|
||||
TransactionStatusRequest transactionStatusRequest = new TransactionStatusRequest();
|
||||
transactionStatusRequest.setTxnId(entity.getTxnId());
|
||||
transactionStatusRequest.setReqId(requestId);
|
||||
|
||||
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";
|
||||
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);
|
||||
|
||||
// ✅ Polling configuration
|
||||
long startTime = System.currentTimeMillis();
|
||||
long maxWaitTimeMs = 40000; // 40 seconds timeout
|
||||
long intervalMs = 5000; // retry every 5 seconds
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseModel checkTrasactionStatus(TransactionStatusRequest transactionStatusRequest) {
|
||||
helper.validateMerchant(transactionStatusRequest.getMid());
|
||||
String txnID = helper.generateTxnId(15);
|
||||
String apiName = "checkTrasactionStatus";
|
||||
String requestId = transactionStatusRequest.getReqId();
|
||||
String requestJson = helper.convertObjectToJson(transactionStatusRequest);
|
||||
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);
|
||||
InboundTraceLog inboundLog = helper.createInboundLog(requestId, txnID, apiName, transactionStatusRequest.getMid(), "POST",
|
||||
requestJson);
|
||||
OutboundTraceLog outboundLog = helper.createOutboundLog(txnID, requestId, apiName, url, "GET", null);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
Map<String, String> responseMap = httpService.getRequest(url, headers);
|
||||
String responseData = responseMap.get("data");
|
||||
LOGGER.info("Transaction status response data: {}", responseData);
|
||||
helper.updateOutboundLogResponse(outboundLog, responseData);
|
||||
|
||||
try {
|
||||
Map<String, Object> dataMap = objectMapper.readValue(responseData, new TypeReference<Map<String, Object>>() {});
|
||||
|
||||
// If genericResponse key missing, throw exception
|
||||
if (!dataMap.containsKey("generalResp")) {
|
||||
helper.updateInboundLogFailure(inboundLog, responseData, String.valueOf(dataMap.get("status")));
|
||||
throw new CustomException(
|
||||
String.valueOf(dataMap.get("message")),
|
||||
String.valueOf(dataMap.get("status")),
|
||||
@@ -175,43 +260,18 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
|
||||
String status = generalResp.getStatus();
|
||||
LOGGER.info("Current transaction status: {}", status);
|
||||
|
||||
// ✅ If IN_PROGRESS → wait & retry
|
||||
if ("INPROGRESS".equalsIgnoreCase(status)) {
|
||||
|
||||
if (System.currentTimeMillis() - startTime > maxWaitTimeMs) {
|
||||
// Timeout after 40 sec
|
||||
throw new CustomException(
|
||||
"Timeout waiting for transaction completion",
|
||||
"FAILED",
|
||||
408,
|
||||
"Transaction still in progress after 40 seconds"
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
Thread.sleep(intervalMs);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new CustomException("Thread interrupted during retry");
|
||||
}
|
||||
|
||||
LOGGER.info("Transaction still IN_PROGRESS... retrying after {} seconds", (intervalMs / 1000));
|
||||
continue; // retry
|
||||
}
|
||||
|
||||
// ✅ If status is SUCCESS / FAILED / etc. → return response
|
||||
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 responseData JSON: {}", e.getMessage());
|
||||
helper.updateInboundLogFailure(inboundLog, e.getMessage(), "JSON_ERROR");
|
||||
throw new CustomException("Invalid JSON received from API");
|
||||
}
|
||||
}
|
||||
@@ -267,4 +327,4 @@ public class PaymentServiceImpl implements PaymentService {
|
||||
* }
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.billpaymentservice.bbps.util;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -11,11 +12,15 @@ import com.billpaymentservice.bbps.dto.ResponseModel;
|
||||
import com.billpaymentservice.bbps.entites.BBPSMastersData;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.repository.BBPSMasterDataRepo;
|
||||
import com.billpaymentservice.bbps.repository.InboundTraceLogRepository;
|
||||
import com.billpaymentservice.bbps.dto.request.BillerDetailRequest;
|
||||
import com.billpaymentservice.bbps.dto.request.MerchantRequestModel;
|
||||
import com.billpaymentservice.bbps.entites.BBPSMerchantValidation;
|
||||
import com.billpaymentservice.bbps.entites.InboundTraceLog;
|
||||
import com.billpaymentservice.bbps.entites.OutboundTraceLog;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.repository.MerchantRepository;
|
||||
import com.billpaymentservice.bbps.repository.OutboundTraceLogRepository;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -23,6 +28,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
public class Helper {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Helper.class);
|
||||
private final InboundTraceLogRepository inboundTraceLogRepository;
|
||||
private final OutboundTraceLogRepository outboundTraceLogRepository;
|
||||
|
||||
private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
private static final SecureRandom secureRandom = new SecureRandom();
|
||||
@@ -31,8 +38,11 @@ public class Helper {
|
||||
private final BBPSMasterDataRepo bbpsMasterDataRepo;
|
||||
|
||||
// Constructor Injection
|
||||
public Helper(BBPSMasterDataRepo bbpsMasterDataRepo) {
|
||||
public Helper(BBPSMasterDataRepo bbpsMasterDataRepo, InboundTraceLogRepository inboundTraceLogRepository,
|
||||
OutboundTraceLogRepository outboundTraceLogRepository) {
|
||||
this.bbpsMasterDataRepo = bbpsMasterDataRepo;
|
||||
this.inboundTraceLogRepository = inboundTraceLogRepository;
|
||||
this.outboundTraceLogRepository = outboundTraceLogRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,64 +84,6 @@ public class Helper {
|
||||
}).start();
|
||||
}
|
||||
|
||||
// public boolean isMerchantValidAndActive(MerchantRequestModel requestModel) {
|
||||
//
|
||||
// if (requestModel == null) {
|
||||
// LOGGER.warn("MerchantRequestModel or MID is null");
|
||||
// throw new CustomException("Invalid request: MID is required");
|
||||
// }
|
||||
//
|
||||
// // Fetch merchant details
|
||||
// BBPSMastersData merchantValidation = bbpsMasterDataRepo.findByMid(requestModel.getMid());
|
||||
//
|
||||
// if (merchantValidation == null) {
|
||||
// LOGGER.warn("Merchant not found for MID: {}", requestModel.getMid());
|
||||
// throw new CustomException("Merchant not found");
|
||||
// }
|
||||
//
|
||||
// if (merchantValidation.getApiKey() == null && merchantValidation.getSecreteKey() == null) {
|
||||
//
|
||||
// throw new CustomException("Merchant inactive");
|
||||
// }
|
||||
//
|
||||
// if (!Boolean.TRUE.equals(merchantValidation.getIsActive())) {
|
||||
// LOGGER.warn("Merchant inactive for MID: {}", requestModel.getMid());
|
||||
// throw new CustomException("Merchant inactive");
|
||||
// }
|
||||
//
|
||||
// LOGGER.info("Merchant is valid and active for MID: {}", requestModel.getMid());
|
||||
// return true;
|
||||
// }
|
||||
|
||||
// public String validateMerchant(String mid) {
|
||||
//
|
||||
// if (mid == null) {
|
||||
// LOGGER.warn("MerchantRequestModel or MID is null");
|
||||
// return "Mid is required";
|
||||
// }
|
||||
//
|
||||
// // Fetch merchant details
|
||||
// BBPSMastersData merchantValidation = bbpsMasterDataRepo.findByMid(mid);
|
||||
//
|
||||
// if (merchantValidation == null) {
|
||||
// LOGGER.warn("Merchant not found for MID: {}", mid);
|
||||
// return "Merchant not found by id";
|
||||
// }
|
||||
//
|
||||
// if (merchantValidation.getApiKey() == null && merchantValidation.getSecreteKey() == null) {
|
||||
// LOGGER.warn("Merchant BBPS service is not active MID: {}", mid);
|
||||
// return "Merchant BBPS Service is not active by mid";
|
||||
// }
|
||||
//
|
||||
// if (!Boolean.TRUE.equals(merchantValidation.getIsActive())) {
|
||||
// LOGGER.warn("Merchant inactive for MID: {}", mid);
|
||||
// return "Merchant is inactive";
|
||||
// }
|
||||
//
|
||||
// LOGGER.info("Merchant is valid and active for MID: {}", mid);
|
||||
// return "Valid"; // Success message
|
||||
// }
|
||||
|
||||
public void validateMerchant(String mid) {
|
||||
if (mid == null || mid == "") {
|
||||
LOGGER.warn("MerchantRequestModel or MID is null");
|
||||
@@ -181,5 +133,56 @@ public class Helper {
|
||||
return "{}"; // or "N/A" depending on your needs
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- Trace helper methods ---------- */
|
||||
|
||||
public InboundTraceLog createInboundLog(String requestId, String txnId, String apiName, String mid,
|
||||
String httpMethod, String requestPayload) {
|
||||
if(inboundTraceLogRepository.findByRequestId(requestId) != null) {
|
||||
throw new CustomException("Duplicate Request ID: " + requestId);
|
||||
}
|
||||
InboundTraceLog inboundLog = new InboundTraceLog(requestId, txnId, apiName, mid == null ? "N/A" : mid,
|
||||
"BBPS_ORG", httpMethod, requestPayload == null ? "N/A" : requestPayload);
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
return inboundLog;
|
||||
}
|
||||
|
||||
public void updateInboundLogSuccess(InboundTraceLog inboundLog, String responsePayload, String responseCode) {
|
||||
if (inboundLog == null)
|
||||
return;
|
||||
inboundLog.setResponsePayload(responsePayload);
|
||||
inboundLog.setResponseCode(responseCode);
|
||||
inboundLog.setStatus("SUCCESS");
|
||||
inboundLog.setResponseTime(LocalDateTime.now());
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
}
|
||||
|
||||
public void updateInboundLogFailure(InboundTraceLog inboundLog, String responsePayload, String responseCode) {
|
||||
if (inboundLog == null)
|
||||
return;
|
||||
inboundLog.setResponsePayload(responsePayload);
|
||||
inboundLog.setResponseCode(responseCode);
|
||||
inboundLog.setStatus("FAILED");
|
||||
inboundLog.setResponseTime(LocalDateTime.now());
|
||||
inboundTraceLogRepository.save(inboundLog);
|
||||
}
|
||||
|
||||
public OutboundTraceLog createOutboundLog(String txnId, String requestId, String apiName, String url,
|
||||
String httpMethod, String requestPayload) {
|
||||
OutboundTraceLog outboundLog = new OutboundTraceLog(txnId, requestId, apiName, url, httpMethod,
|
||||
requestPayload == null ? "N/A" : requestPayload);
|
||||
outboundTraceLogRepository.save(outboundLog);
|
||||
return outboundLog;
|
||||
}
|
||||
|
||||
public void updateOutboundLogResponse(OutboundTraceLog outboundLog, String responsePayload) {
|
||||
if (outboundLog == null)
|
||||
return;
|
||||
outboundLog.setResponsePayload(responsePayload);
|
||||
outboundLog.setResponseReceiveTime(LocalDateTime.now());
|
||||
outboundTraceLogRepository.save(outboundLog);
|
||||
}
|
||||
|
||||
/* ---------- End helpers ---------- */
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user