I want to use RestTemplate for my API call. I am trying to initialize a payment gateway but it always returning an error message that says java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode. I really need help on how to resolve it.It was actually working when I was passing the values direct without having to reference it from somewhere. This is what I did.
@Component
public class RestTemplateService {
private RestTemplate restTemplate;
@Autowired
public RestTemplateService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.errorHandler(new RestTemplateResponseErrorHandler())
.build();
}
public <T> ResponseEntity<CustomPaymentResponse> post(String url, T req, HttpHeaders headers) {
HttpEntity entity = headers == null ? new HttpEntity(req) : new HttpEntity(req, headers);
ResponseEntity<CustomPaymentResponse> response = restTemplate.exchange(url, HttpMethod.POST, entity, CustomPaymentResponse.class);
return response;
}
public ResponseEntity<CustomPaymentResponse> get(String url, HttpHeaders headers) {
HttpEntity entity = headers == null ? null : new HttpEntity(headers);
ResponseEntity<CustomPaymentResponse> response = restTemplate.exchange(url, HttpMethod.GET, entity, CustomPaymentResponse.class);
return response;
}
public ResponseEntity<CreatePlanResponse> delete(String url, HttpHeaders headers) {
HttpEntity entity = headers == null ? null : new HttpEntity(headers);
ResponseEntity<CreatePlanResponse> response = restTemplate.exchange(url, HttpMethod.DELETE, entity, CreatePlanResponse.class);
return response;
}
}
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
HttpStatus statusCode = (HttpStatus) httpResponse.getStatusCode();
return (statusCode.is4xxClientError() || statusCode.is5xxServerError());
}
@Override
public void handleError(ClientHttpResponse httpResponse) throws IOException {
HttpStatus statusCode = (HttpStatus) httpResponse.getStatusCode();
if (statusCode.is5xxServerError()) {
// handle SERVER_ERROR
} else if (statusCode.is4xxClientError()) {
// handle CLIENT_ERROR
if (statusCode == HttpStatus.NOT_FOUND) {
try {
throw new ChangeSetPersister.NotFoundException();
} catch (ChangeSetPersister.NotFoundException e) {
throw new RuntimeException(e);
}
}
}
}
}
@Service
@RequiredArgsConstructor
public class UserPayStackPaymentServiceImpl implements UserPayStackPaymentService{
@Value("${payStackSecretKey}")
private String payStackSecretKey;
private final RestTemplateService restTemplate;
private final UserPayStackPaymentRepository userPayStackPaymentRepository;
private final SaleRepository saleRepository;
@Override
public ResponseEntity<CustomPaymentResponse> initializePayment(Long orderId) {
Map<String, String> request = new HashMap<>();
Optional<SaleEntity> order = saleRepository.findById(orderId);
double amountToBePaid = order.get().getTotalPrice() * 100;
request.put("email", order.get().getBuyer().getEmail());
request.put("amount", String.valueOf(amountToBePaid));
ResponseEntity<CustomPaymentResponse> response = restTemplate.post(PAYSTACK_INITIALIZE_PAY, request, PaymentConstants.headers());
System.out.println("The initialize payment response is "+response.getBody().toString());
if (response.getStatusCode() == HttpStatus.OK) {
System.out.println("The response is ok in this place "+response.getBody().getData().toString());
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> mapResponse = objectMapper.convertValue(response.getBody().getData(), Map.class);
UserPaymentStack userPaymentStack = UserPaymentStack.builder()
.user(order.get().getBuyer())
.amount(order.get().getTotalPrice())
.status(PaymentStatus.PENDING.name())
.order(order.get())
.reference((String) mapResponse.get("reference"))
.accessCode((String) mapResponse.get("access_code"))
.build();
userPayStackPaymentRepository.save(userPaymentStack);
return ResponseEntity.ok( CustomPaymentResponse.builder()
.message(response.getBody().getMessage())
.status(true)
.data(mapResponse)
.build());
}
System.out.println("Response is not ok in this place "+response.getBody().getMessage());
return ResponseEntity.badRequest().body(new CustomPaymentResponse(false,response.getBody().getMessage(),null));
}
}