added requestBillFetch api
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package com.billpaymentservice.bbps.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RestClientConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestClient restClient() throws Exception {
|
||||||
|
// ✅ Create trust all SSL context
|
||||||
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||||
|
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||||
|
new X509TrustManager() {
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return new X509Certificate[0];
|
||||||
|
}
|
||||||
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {
|
||||||
|
}
|
||||||
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||||
|
|
||||||
|
// ✅ Create RestClient with custom SSL context
|
||||||
|
return RestClient.builder()
|
||||||
|
.requestFactory(new HttpComponentsClientHttpRequestFactory())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.billpaymentservice.bbps.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import com.billpaymentservice.bbps.dto.ResponseModel;
|
||||||
|
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
|
||||||
|
import com.billpaymentservice.bbps.service.AgentService;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/agent")
|
||||||
|
public class AgentController {
|
||||||
|
|
||||||
|
private final AgentService agentService;
|
||||||
|
|
||||||
|
public AgentController(AgentService agentService) {
|
||||||
|
this.agentService = agentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches bill details from external BBPS service
|
||||||
|
*/
|
||||||
|
@PostMapping(
|
||||||
|
value = "/requestBillFetch",
|
||||||
|
consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||||
|
produces = MediaType.APPLICATION_JSON_VALUE
|
||||||
|
)
|
||||||
|
public ResponseEntity<ResponseModel> requestBillFetch(
|
||||||
|
@Validated @RequestBody FetchBillRequest fetchBillRequest) {
|
||||||
|
|
||||||
|
ResponseModel response = agentService.initiateBillFetch(fetchBillRequest);
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,15 +10,19 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.ToString;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@ToString
|
||||||
public class FetchBillRequest extends BaseClass {
|
public class FetchBillRequest extends BaseClass {
|
||||||
|
|
||||||
private String tenantId; // M
|
private String tenantId; // M
|
||||||
private String billerId; // M
|
private String billerId; // M
|
||||||
private String billerName; // M
|
private String billerName; //M
|
||||||
|
private String digiledgeKey; // M
|
||||||
|
private String agentId; // M
|
||||||
private String billerCategory; // M
|
private String billerCategory; // M
|
||||||
private String clientReqId; // C
|
private String clientReqId; // C
|
||||||
private String macAdress; // C
|
private String macAdress; // C
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
package com.billpaymentservice.bbps.dto.response;
|
package com.billpaymentservice.bbps.dto.response;
|
||||||
|
|
||||||
import com.billpaymentservice.bbps.dto.GeneralResponse;
|
import com.billpaymentservice.bbps.dto.GeneralResponse;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class FetchBillResponse {
|
public class FetchBillResponse {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
private GeneralResponse genericResponse;
|
private GeneralResponse genericResponse;
|
||||||
private String billId;
|
private String billId;
|
||||||
private String refId;
|
private String refId;
|
||||||
@@ -28,6 +28,4 @@ public class FetchBillResponse {
|
|||||||
private String modifiedDate;
|
private String modifiedDate;
|
||||||
private long createdEpochDate;
|
private long createdEpochDate;
|
||||||
private long modifiedEpochDate;
|
private long modifiedEpochDate;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.billpaymentservice.bbps.service;
|
||||||
|
|
||||||
|
import com.billpaymentservice.bbps.dto.ResponseModel;
|
||||||
|
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
|
||||||
|
|
||||||
|
public interface AgentService {
|
||||||
|
|
||||||
|
public ResponseModel initiateBillFetch(FetchBillRequest fetchBillRequest);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.billpaymentservice.bbps.serviceImpl;
|
package com.billpaymentservice.bbps.serviceImpl;
|
||||||
|
|
||||||
|
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -12,53 +10,64 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.time.Duration;
|
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import com.billpaymentservice.bbps.dto.request.FetchBillRequest;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
public class HttpService {
|
public class HttpService {
|
||||||
private static final Logger log = LoggerFactory.getLogger(HttpService.class);
|
private static final Logger log = LoggerFactory.getLogger(HttpService.class);
|
||||||
private final RestClient restClient;
|
private final RestClient restClient;
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
HttpService() {
|
|
||||||
HttpClient httpClient = HttpClient.newBuilder()
|
|
||||||
.connectTimeout(Duration.ofSeconds(5))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
JdkClientHttpRequestFactory factory = new JdkClientHttpRequestFactory(httpClient);
|
public HashMap<String, String> postRequest(String url, String payload, HttpHeaders customHeaders) {
|
||||||
factory.setReadTimeout(Duration.ofSeconds(10));
|
|
||||||
|
|
||||||
this.restClient = RestClient.builder()
|
|
||||||
.requestFactory(factory)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public HashMap<String, String> postRequest(String url, String payload) {
|
|
||||||
HashMap<String, String> response = new HashMap<>();
|
HashMap<String, String> response = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
HttpHeaders headers = new HttpHeaders();
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
|
||||||
|
if (customHeaders != null) {
|
||||||
|
customHeaders.forEach((key, value) -> {
|
||||||
|
if (!key.equalsIgnoreCase(HttpHeaders.CONTENT_TYPE)) {
|
||||||
|
headers.addAll(key, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
log.info("📡 Bank POST Request to URL: {}", url);
|
|
||||||
|
|
||||||
|
// ✅ MAKE THE REQUEST
|
||||||
ResponseEntity<String> restResponse = restClient.post()
|
ResponseEntity<String> restResponse = restClient.post()
|
||||||
.uri(url)
|
.uri(url)
|
||||||
.headers(h -> h.addAll(headers))
|
.headers(h -> h.addAll(headers))
|
||||||
.body(payload)
|
.body(payload)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.toEntity(String.class);
|
.toEntity(String.class);
|
||||||
|
|
||||||
response.put("respCode", String.valueOf(restResponse.getStatusCode().value()));
|
response.put("respCode", String.valueOf(restResponse.getStatusCode().value()));
|
||||||
response.put("data", restResponse.getBody());
|
response.put("data", restResponse.getBody());
|
||||||
|
|
||||||
|
log.info("✅ HttpService Response Status: {}", restResponse.getStatusCode());
|
||||||
|
|
||||||
} catch (HttpClientErrorException | HttpServerErrorException httpException) {
|
} catch (HttpClientErrorException.Forbidden e) {
|
||||||
response.put("respCode", String.valueOf(httpException.getStatusCode()));
|
// ✅ SPECIAL 403 HANDLING
|
||||||
response.put("data", "Exception : " + httpException.getResponseBodyAsString());
|
log.error("🚫 HttpService: 403 FORBIDDEN");
|
||||||
|
response.put("respCode", "403");
|
||||||
|
response.put("data", "FORBIDDEN - Check credentials in HttpService");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
response.put("data", "Exception :" + e.getMessage());
|
log.error("❌ HttpService Exception: {}", e.getMessage());
|
||||||
log.error("❌ Exception during POST call to {} -> {}", url, e.getMessage());
|
response.put("respCode", "500");
|
||||||
|
response.put("data", "HttpService Error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -155,7 +155,6 @@ public class MasterDataServiceImpl implements MasterDataService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseModel fetchMasterDataCategoryWise(MerchantRequestModel requestModel,
|
public ResponseModel fetchMasterDataCategoryWise(MerchantRequestModel requestModel,
|
||||||
String category, int pageStart, int pageEnd) {
|
String category, int pageStart, int pageEnd) {
|
||||||
|
|||||||
@@ -28,5 +28,6 @@ public class BBPSConstant {
|
|||||||
|
|
||||||
public static final String BILLER_DATA_FETCH_URL="/billersById?billerId=";
|
public static final String BILLER_DATA_FETCH_URL="/billersById?billerId=";
|
||||||
public static final String CUSTOMPARAM_URL="/customerParam?billerId=";
|
public static final String CUSTOMPARAM_URL="/customerParam?billerId=";
|
||||||
|
public static final String BILL_FETCH_INTI="/couapp/bills/fetch";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ spring.datasource.hikari.auto-commit=true
|
|||||||
logging.level.io.github.resilience4j.retry=DEBUG
|
logging.level.io.github.resilience4j.retry=DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# BBPS URLs
|
# BBPS URLs
|
||||||
master.data.url = https://bbps-staging.digiledge.in/agent/cou-master/masters
|
master.data.url = https://bbps-staging.digiledge.in/agent/cou-master/masters
|
||||||
|
agent.data.url=https://bbps-staging.digiledge.in/agent/bill-payment-new
|
||||||
|
|
||||||
|
|
||||||
# Resilience4j Retry
|
# Resilience4j Retry
|
||||||
|
|||||||
Reference in New Issue
Block a user