diff --git a/payment/src/main/java/payment/services/PaymentService.java b/payment/src/main/java/payment/services/PaymentService.java index 26fdb28e..a6fdf170 100644 --- a/payment/src/main/java/payment/services/PaymentService.java +++ b/payment/src/main/java/payment/services/PaymentService.java @@ -10,7 +10,7 @@ public interface PaymentService { PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequest); - + boolean completePayment(UUID paymentUuid) ; Optional getPaymentById(UUID paymentId); diff --git a/payment/src/main/java/payment/services/PaymentServiceImpl.java b/payment/src/main/java/payment/services/PaymentServiceImpl.java index e0d501c3..066c9aa2 100644 --- a/payment/src/main/java/payment/services/PaymentServiceImpl.java +++ b/payment/src/main/java/payment/services/PaymentServiceImpl.java @@ -63,7 +63,11 @@ public PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequest) { creditCardRequest.setCardCode(paymentRequest.getCardCode()); creditCardRequest.setSecretNumber(paymentRequest.getCardNumber()); cardServices.registerCreditCard(creditCardRequest); - + if(paymentRequest.getCardCode()!=paymentRequest.getCardNumber()){ + payment.setPaymentStatus(PaymentStatus.FAILED); + paymentRepository.updatePayment(payment) ; + return paymentMapper.toResponseDTO(savedPayment); + } // 6. Create a PaymentOutBox entry for the Outbox pattern PaymentOutBox outBoxEvent = new PaymentOutBox(); @@ -115,4 +119,15 @@ public List getPaymentsByDate(LocalDateTime date, UUID custo .map(paymentMapper::toResponseDTO) .collect(Collectors.toList()); } + + @Override + public boolean completePayment(UUID paymentUuid) { + Payment payment=paymentRepository.findById(paymentUuid) ; + if (payment != null && payment.getPaymentStatus() == PaymentStatus.PENDING){ + payment.setPaymentStatus(PaymentStatus.COMPLETED); + paymentRepository.updatePayment(payment) ; + return true ; + } + return false ; + } } diff --git a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessor.java b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessor.java index 3e3435a4..6ef6d715 100644 --- a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessor.java +++ b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessor.java @@ -2,5 +2,5 @@ public interface PaymentOutBoxProcessor { public void processOutboxEvents(); - // public void processFailedEvents() ; + public void processFailedEvents() ; } diff --git a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java index a189bbc3..f4e61f00 100644 --- a/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java +++ b/payment/src/main/java/payment/services/outBoxProcessor/PaymentOutBoxProcessorImpl.java @@ -14,6 +14,7 @@ import payment.domain.objectValues.PaymentStatus; import payment.repository.PaymentRepository; import payment.repository.outBoxRepository.PaymentOutBoxRepository; +import payment.services.PaymentService; @ApplicationScoped public class PaymentOutBoxProcessorImpl implements PaymentOutBoxProcessor { @@ -23,6 +24,8 @@ public class PaymentOutBoxProcessorImpl implements PaymentOutBoxProcessor { @Inject PaymentRepository paymentRepository; + @Inject + PaymentService paymentService ; @Inject @RestClient @@ -36,7 +39,7 @@ public void processOutboxEvents() { List unprocessedEvents = boxRepository.findUnprocessedEvents(); for (PaymentOutBox event : unprocessedEvents) { - try { + Payment payment = paymentRepository.findById(event.getPaymentId()); BankPaymentRequest bankPaymentRequest = new BankPaymentRequest( event.getPaymentId(), @@ -45,25 +48,27 @@ public void processOutboxEvents() { event.getCardCode() ); - + try { Response response = bankClient.makeNewPayment(bankPaymentRequest) ; if (response.getStatus() == Response.Status.OK.getStatusCode()) { event.setProcessed(true); event.setPaymentStatus(PaymentStatus.COMPLETED); - payment.setPaymentStatus(PaymentStatus.COMPLETED); - boxRepository.update(event); - paymentRepository.updatePayment(payment); - } else { - event.setProcessed(false); - event.setPaymentStatus(PaymentStatus.FAILED); boxRepository.update(event); - } - + paymentService.completePayment(payment.getPaymentId()) ; + } } catch (Exception e) { event.setProcessed(true); event.setPaymentStatus(PaymentStatus.FAILED); - boxRepository.update(event); + boxRepository.update(event); + } } } + + @Override + public void processFailedEvents() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'processFailedEvents'"); + } + }