added requestBillFetch api
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package com.billpaymentservice.bbps.serviceImpl;
|
||||
|
||||
import com.billpaymentservice.bbps.dto.GeneralResponse;
|
||||
import com.billpaymentservice.bbps.dto.ResponseModel;
|
||||
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
|
||||
import com.billpaymentservice.bbps.dto.response.FetchBillResponse;
|
||||
import com.billpaymentservice.bbps.exception.CustomException;
|
||||
import com.billpaymentservice.bbps.service.AgentService;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AgentServiceImpl implements AgentService {
|
||||
|
||||
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";
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("x-digiledge-key", fetchBillRequest.getDigiledgeKey());
|
||||
headers.set("agentId", fetchBillRequest.getAgentId());
|
||||
headers.set("tenantId", fetchBillRequest.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(fetchBillRequest);
|
||||
|
||||
// ✅ Call the API using httpService
|
||||
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
|
||||
FetchBillResponse fetchBillResponse = objectMapper.convertValue(dataMap, FetchBillResponse.class);
|
||||
GeneralResponse generalResponse = fetchBillResponse.getGenericResponse();
|
||||
|
||||
// ✅ Build final ResponseModel
|
||||
responseModel = new ResponseModel(
|
||||
generalResponse.getMessage(),
|
||||
generalResponse.getStatus(),
|
||||
Integer.parseInt(generalResponse.getStatusCode()),
|
||||
fetchBillResponse
|
||||
);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("❌ Exception in initiateBillFetch(): {}", e.getMessage(), e);
|
||||
responseModel.setStatus("500");
|
||||
responseModel.setMessage("Exception occurred");
|
||||
responseModel.setData(e.getMessage());
|
||||
}
|
||||
|
||||
return responseModel;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user