PaymentGatewayService.java
Bridget/payment-gateway-challenge-java/src/main/java/com/checkout/payment/gateway/service/PaymentGatewayService.java
package com.checkout.payment.gateway.service;
import com.checkout.payment.gateway.enums.PaymentStatus;
import com.checkout.payment.gateway.exception.BankErrorException;
import com.checkout.payment.gateway.exception.EventProcessingException;
import com.checkout.payment.gateway.model.BankPaymentRequest;
import com.checkout.payment.gateway.model.BankPaymentResponse;
import com.checkout.payment.gateway.model.GetPaymentResponse;
import com.checkout.payment.gateway.model.PaymentMapper;
import com.checkout.payment.gateway.model.PostPaymentRequest;
import com.checkout.payment.gateway.model.PostPaymentResponse;
import com.checkout.payment.gateway.repository.PaymentsRepository;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
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 PaymentGatewayService {
private static final Logger LOG = LoggerFactory.getLogger(PaymentGatewayService.class);
private final PaymentsRepository paymentsRepository;
private final RestTemplate restTemplate;
private final String acquiringBankPaymentsUrl;
public PaymentGatewayService(
PaymentsRepository paymentsRepository,
RestTemplate restTemplate,
@Value("${acquiring.bank.payments-url}") String acquiringBankPaymentsUrl) {
this.paymentsRepository = paymentsRepository;
this.restTemplate = restTemplate;
this.acquiringBankPaymentsUrl = acquiringBankPaymentsUrl;
}
public GetPaymentResponse getPaymentById(UUID id) {
LOG.debug("Requesting access to payment with ID {}", id);
return PaymentMapper.toGetResponse(
paymentsRepository.get(id).orElseThrow(() -> new EventProcessingException("Invalid ID")));
}
public PostPaymentResponse createPayment(PostPaymentRequest paymentRequest) {
LOG.debug("Requesting to create payment");
PostPaymentResponse result = processPayment(paymentRequest);
paymentsRepository.add(result);
LOG.info(
"Payment {} with {}{} (card ending with {})",
result.getStatus().getName(),
result.getCurrency(),
result.getAmount(),
result.getCardNumberLastFour());
return result;
}
public PostPaymentResponse processPayment(PostPaymentRequest paymentRequest) {
BankPaymentResponse response =
sendPaymentRequestToBank(PaymentMapper.toBankRequest(paymentRequest));
PaymentStatus paymentStatus =
response.isAuthorized() ? PaymentStatus.AUTHORIZED : PaymentStatus.DECLINED;
return PaymentMapper.toPostResponse(paymentRequest, UUID.randomUUID(), paymentStatus);
}
public BankPaymentResponse sendPaymentRequestToBank(BankPaymentRequest paymentRequest) {
LOG.debug("Sending request to the bank {}", acquiringBankPaymentsUrl);
BankPaymentResponse response;
try {
response =
restTemplate.postForObject(
acquiringBankPaymentsUrl, paymentRequest, BankPaymentResponse.class);
if (response == null) {
throw new BankErrorException(
HttpStatus.BAD_GATEWAY, "Empty response from acquiring bank");
}
LOG.debug("Received response from the bank authorized={}", response.isAuthorized());
} catch (HttpServerErrorException.ServiceUnavailable e) {
throw new BankErrorException(e.getStatusCode(), e.getMessage());
} catch (HttpServerErrorException e) {
throw new BankErrorException(HttpStatus.BAD_GATEWAY, e.getMessage());
} catch (HttpClientErrorException e) {
throw new BankErrorException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (Exception e) {
throw new BankErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
}
return response;
}
}
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 →