added fetchBillByBillId

This commit is contained in:
himanshu
2025-10-29 13:55:49 +05:30
parent c8191b54cd
commit 5325f26a64
10 changed files with 262 additions and 10 deletions
@@ -1,12 +1,18 @@
package com.billpaymentservice.bbps.serviceImpl;
import com.billpaymentservice.bbps.dto.GeneralResp;
import com.billpaymentservice.bbps.dto.GeneralResponse;
import com.billpaymentservice.bbps.dto.ResponseModel;
import com.billpaymentservice.bbps.dto.request.BillFetchByBillIdRequest;
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
import com.billpaymentservice.bbps.dto.response.BillDataResponse;
import com.billpaymentservice.bbps.dto.response.BillerDataResponse;
import com.billpaymentservice.bbps.dto.response.FetchBillResponse;
import com.billpaymentservice.bbps.exception.CustomException;
import com.billpaymentservice.bbps.service.AgentService;
import com.billpaymentservice.bbps.util.BBPSConstant;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
@@ -15,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
@@ -24,12 +31,16 @@ import org.springframework.web.client.RestClient;
@RequiredArgsConstructor
public class AgentServiceImpl implements AgentService {
@Value("${agent.data.url}")
private String agentDtaUrl;
private final RestClient restClient;
private final HttpService httpService;// ✅ injected from config
@Override
public ResponseModel initiateBillFetch(FetchBillRequest fetchBillRequest) {
String url = "https://bbps-staging.digiledge.in/agent/bill-payment-new/couapp/bills/fetch";
String url = agentDtaUrl+BBPSConstant.BILL_FETCH_INTI;
System.out.println(url);
HttpHeaders headers = new HttpHeaders();
headers.set("x-digiledge-key", fetchBillRequest.getDigiledgeKey());
@@ -86,4 +97,53 @@ public class AgentServiceImpl implements AgentService {
return responseModel;
}
@Override
public ResponseModel fetchBillById(BillFetchByBillIdRequest billFetchByBillIdRequest) {
String billId = billFetchByBillIdRequest.getBillId();
String fullUrl = agentDtaUrl + BBPSConstant.BILL_FETCH_BY_ID+billId;
System.out.print(fullUrl);
HttpHeaders headers = new HttpHeaders();
headers.set("x-digiledge-key", billFetchByBillIdRequest.getDigiledgeKey());
headers.set("agentId", billFetchByBillIdRequest.getAgentId());
headers.set("tenantId", billFetchByBillIdRequest.getTenantId());
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String, String> responseMap = httpService.getRequest(fullUrl,headers);
String responseData = responseMap.get("data");
log.info("data is :{}", responseData);
ObjectMapper objectMapper = new ObjectMapper();
try {
Map<String, Object> dataMap = objectMapper.readValue(responseData,
new TypeReference<Map<String, Object>>() {
});
if (!dataMap.containsKey("genericResponse")) {
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")) );
}
BillDataResponse responseWrapper = objectMapper.convertValue(dataMap, BillDataResponse.class);
GeneralResp generalResp = responseWrapper.getGenericResponse();
ResponseModel responseModel=new ResponseModel( generalResp.getMessage(),
generalResp.getStatus(),
Integer.parseInt(generalResp.getStatusCode()),
responseWrapper);
return responseModel;
} catch (JsonProcessingException e) {
log.error("Error parsing responseData JSON: {}", e.getMessage());
throw new CustomException("Invalid JSON received from API");
}
}
}
@@ -42,7 +42,7 @@ public class HttpService {
}
headers.setContentType(MediaType.APPLICATION_JSON);
// ✅ MAKE THE REQUEST
ResponseEntity<String> restResponse = restClient.post()
.uri(url)
@@ -71,24 +71,78 @@ public class HttpService {
return response;
}
public HashMap<String, String> getRequest(String url) {
public HashMap<String, String> getRequest(String url, HttpHeaders customHeaders) {
HashMap<String, String> response = new HashMap<>();
try {
log.info("🌐 Bank GET Request to URL: {}", url);
HttpHeaders headers = new HttpHeaders();
if (customHeaders != null) {
headers.addAll(customHeaders);
}
log.info("🌐 Sending GET request to: {}", url);
log.info("📦 Headers: {}", headers);
ResponseEntity<String> restResponse = restClient.get()
.uri(url)
.retrieve()
.toEntity(String.class);
.uri(url)
.headers(h -> h.addAll(headers))
.retrieve()
.toEntity(String.class);
response.put("respCode", String.valueOf(restResponse.getStatusCode().value()));
response.put("data", restResponse.getBody());
log.info("✅ GET Response: {}", restResponse.getStatusCode());
} catch (HttpClientErrorException | HttpServerErrorException httpException) {
response.put("data", "Exception : " + httpException.getResponseBodyAsString());
log.error("❌ HTTP Error: {}", httpException.getMessage(), httpException);
response.put("respCode", String.valueOf(httpException.getStatusCode().value()));
response.put("data", httpException.getResponseBodyAsString());
} catch (Exception e) {
response.put("data", "Exception :" + e.getMessage());
log.error("❌ Exception during GET call to {} -> {}", url, e.getMessage());
log.error("❌ Exception during GET call: {}", e.getMessage(), e);
response.put("respCode", "500");
response.put("data", "Exception: " + e.getMessage());
}
// 🟢 You missed this line earlier
return response;
}
public HashMap<String, String> getRequest(String url) {
HashMap<String, String> response = new HashMap<>();
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
log.info("🌐 Sending GET request to: {}", url);
log.info("📦 Headers: {}", headers);
ResponseEntity<String> restResponse = restClient.get()
.uri(url)
.headers(h -> h.addAll(headers))
.retrieve()
.toEntity(String.class);
response.put("respCode", String.valueOf(restResponse.getStatusCode().value()));
response.put("data", restResponse.getBody());
log.info("✅ GET Response: {}", restResponse.getStatusCode());
} catch (HttpClientErrorException | HttpServerErrorException httpException) {
log.error("❌ HTTP Error: {}", httpException.getMessage(), httpException);
response.put("respCode", String.valueOf(httpException.getStatusCode().value()));
response.put("data", httpException.getResponseBodyAsString());
} catch (Exception e) {
log.error("❌ Exception during GET call: {}", e.getMessage(), e);
response.put("respCode", "500");
response.put("data", "Exception: " + e.getMessage());
}
return response;
}
}