Files
bbps-backend/src/main/java/com/billpaymentservice/bbps/serviceImpl/HttpClientApi.java
T
2025-10-28 15:46:31 +05:30

61 lines
2.1 KiB
Java

package com.billpaymentservice.bbps.serviceImpl;
import java.util.Collections;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
@Service
public class HttpClientApi {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientApi.class);
public HashMap<String, Object> getRequestForPaymentStatus(String url) {
HashMap<String, Object> responseMap = new HashMap<>();
try {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<Void> request = new HttpEntity<>(headers);
LOGGER.info("BBPS: Payment Status Request URL: {}", url);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
String responseBody = response.getBody();
LOGGER.info("Pesapal: Payment Status Response Body: {}", responseBody);
// Set response values in map
responseMap.put("data", responseBody);
} catch (HttpClientErrorException | HttpServerErrorException httpEx) {
LOGGER.error("BBPS: HTTP error during payment status check", httpEx);
responseMap.put("respCode", String.valueOf(httpEx.getStatusCode()));
responseMap.put("data", "Exception: " + httpEx.getResponseBodyAsString());
} catch (Exception e) {
LOGGER.error("BBPS: Unexpected error during payment status check", e);
responseMap.put("respCode", String.valueOf(HttpStatus.EXPECTATION_FAILED));
responseMap.put("error", "Exception During REST Call: " + e.getMessage());
}
return responseMap;
}
}