Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/util/ResponseParser.java
T

135 lines
5.2 KiB
Java

package com.billpaymentservice.bbps.util;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.billpaymentservice.bbps.dto.FaqDetailsResponse;
import com.billpaymentservice.bbps.dto.GeneralResponse;
import com.billpaymentservice.bbps.dto.MasterApiResponse;
import com.billpaymentservice.bbps.dto.MasterCategoryBillerResponse;
import com.billpaymentservice.bbps.dto.response.BillerResponse;
import com.billpaymentservice.bbps.dto.response.CategoryResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
public class ResponseParser {
private static final Logger LOGGER = LoggerFactory.getLogger(ResponseParser.class);
private final ObjectMapper mapper = new ObjectMapper();
public MasterApiResponse parseResponse(String jsonResponse) {
MasterApiResponse finalResponse = new MasterApiResponse();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode root = mapper.readTree(jsonResponse);
// ✅ Parse generalResp correctly
JsonNode generalRespNode = root.path("generalResp");
GeneralResponse generalResp = new GeneralResponse();
generalResp.setStatusCode(generalRespNode.path("statusCode").asText());
generalResp.setStatus(generalRespNode.path("status").asText());
generalResp.setMessage(generalRespNode.path("message").asText());
generalResp.setRemarks(generalRespNode.path("remarks").asText());
finalResponse.setGeneralResp(generalResp);
// ✅ Parse categoryRespList
List<CategoryResponse> categoryList = new ArrayList<>();
JsonNode categoryArray = root.path("categoryRespList");
if (categoryArray.isArray()) {
for (JsonNode node : categoryArray) {
CategoryResponse data = new CategoryResponse();
data.setCategoryId(node.path("categoryId").asText());
data.setCategoryName(node.path("categoryName").asText());
data.setCategoryIcon(node.path("categoryIcon").asText());
data.setCategoryDomain(node.path("categoryDomain").asText(""));
data.setButtonName(node.path("buttonName").asText(""));
data.setTextArea(node.path("textArea").asText(""));
// Parse faqDetailsList
List<FaqDetailsResponse> faqList = new ArrayList<>();
JsonNode faqArray = node.path("faqDetailsList");
if (faqArray.isArray()) {
for (JsonNode faqNode : faqArray) {
FaqDetailsResponse faq = new FaqDetailsResponse();
faq.setName(faqNode.path("name").asText());
faq.setValue(faqNode.path("value").asText());
faqList.add(faq);
}
}
data.setFaqDetailsList(faqList);
categoryList.add(data);
}
}
finalResponse.setCategoryRespList(categoryList);
} catch (Exception e) {
throw new RuntimeException("Failed to parse BBPS master API response", e);
}
return finalResponse;
}
public BillerResponse billerResponse(String jsonResponse) {
BillerResponse billerResponse = new BillerResponse();
try {
JsonNode root = mapper.readTree(jsonResponse);
// ✅ Pagination info
billerResponse.setPageNo(root.path("pageNo").asInt());
billerResponse.setPageSize(root.path("pageSize").asInt());
billerResponse.setTotalElements(root.path("totalElements").asLong());
billerResponse.setTotalPages(root.path("totalPages").asInt());
// ✅ General Response Info
JsonNode generalNode = root.path("generalResp");
GeneralResponse generalResp = new GeneralResponse();
generalResp.setStatusCode(generalNode.path("statusCode").asText());
generalResp.setStatus(generalNode.path("status").asText());
generalResp.setMessage(generalNode.path("message").asText());
generalResp.setRemarks(generalNode.path("remarks").asText());
billerResponse.setGeneralResp(generalResp);
// ✅ Parse biller list
List<MasterCategoryBillerResponse> billerList = new ArrayList<>();
JsonNode billerArray = root.path("billerResp");
if (billerArray.isArray()) {
for (JsonNode billerNode : billerArray) {
MasterCategoryBillerResponse biller = new MasterCategoryBillerResponse();
biller.setBillerId(billerNode.path("billerId").asText());
biller.setBillerName(billerNode.path("billerName").asText());
biller.setBillerType(billerNode.path("billerType").asText());
biller.setBillerCategory(billerNode.path("billerCategory").asText());
biller.setBillerCoverage(billerNode.path("billerCoverage").asText());
biller.setBillerResponseType(billerNode.path("billerResponseType").asText());
biller.setBillerDescription(billerNode.path("billerDescription").asText());
biller.setPlanMDMRequirement(billerNode.path("planMDMRequirement").asText());
biller.setAdhocBiller(billerNode.path("adhocBiller").asText());
biller.setPaymentAmountExactness(billerNode.path("paymentAmountExactness").asText());
billerList.add(biller);
}
}
billerResponse.setBillerResp(billerList);
LOGGER.info("✅ Successfully parsed BillerResponse with {} billers", billerList.size());
} catch (Exception e) {
LOGGER.error("❌ Error parsing BillerResponse: {}", e.getMessage(), e);
throw new RuntimeException("Failed to parse BBPS Biller API response", e);
}
return billerResponse;
}
}