BankAdapterClient.java
Bridget/payment-gateway-microservices/services/payment-service/src/main/java/com/checkout/paymentgw/payment/client/BankAdapterClient.java
package com.checkout.paymentgw.payment.client;
import com.checkout.paymentgw.payment.api.dto.PostPaymentRequest;
import com.checkout.paymentgw.payment.exception.BankErrorException;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClient;
/**
* Anti-corruption layer wrapper around {@code bank-adapter-service}. The downstream
* service is the only place that knows the bank's wire format; this client only
* speaks the internal canonical model.
*/
@Component
public class BankAdapterClient {
private static final Logger LOG = LoggerFactory.getLogger(BankAdapterClient.class);
private final RestClient http;
public BankAdapterClient(RestClient.Builder builder,
org.springframework.beans.factory.annotation.Value("${payment.bank-adapter-base-url}")
String baseUrl) {
this.http = builder.baseUrl(baseUrl).build();
}
/**
* @throws BankErrorException for any non-2xx outcome. The caller decides
* whether to translate it to {@code DECLINED}, {@code FAILED}, or 5xx.
*/
@CircuitBreaker(name = "bankAdapter")
@Retry(name = "bankAdapter")
@TimeLimiter(name = "bankAdapter")
public BankAuthorisation authorise(PostPaymentRequest request) {
LOG.debug("authorise call to bank-adapter");
try {
BankAuthorisation response = http.post()
.uri("/internal/authorise")
.body(request)
.retrieve()
.body(BankAuthorisation.class);
if (response == null) {
throw new BankErrorException(HttpStatus.BAD_GATEWAY, "empty response");
}
return response;
} catch (HttpServerErrorException.ServiceUnavailable e) {
throw new BankErrorException(HttpStatus.SERVICE_UNAVAILABLE, e.getMessage());
} catch (HttpServerErrorException e) {
throw new BankErrorException(HttpStatus.BAD_GATEWAY, e.getMessage());
} catch (HttpClientErrorException e) {
throw new BankErrorException(HttpStatus.BAD_REQUEST, e.getMessage());
}
}
}
Artículos relacionados
PaymentGatewayApplication.java
PaymentGatewayApplication.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/PaymentGatewayApplication.java).
Leer artículo →ApplicationConfiguration.java
ApplicationConfiguration.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/ApplicationConfiguration.java).
Leer artículo →RequestResponseLogger.java
RequestResponseLogger.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/configuration/RequestResponseLogger.java).
Leer artículo →PaymentGatewayController.java
PaymentGatewayController.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/controller/PaymentGatewayController.java).
Leer artículo →PaymentStatus.java
PaymentStatus.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/enums/PaymentStatus.java).
Leer artículo →BankErrorException.java
BankErrorException.java — java source code from the Bridget learning materials (Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/exception/BankErrorException.java).
Leer artículo →