Skip to content

Commit

Permalink
so fixed failing evetns
Browse files Browse the repository at this point in the history
  • Loading branch information
samehajala committed Dec 10, 2024
1 parent acb9fa3 commit 9641b16
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion payment/src/main/java/payment/services/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface PaymentService {

PaymentResponseDTO processPayment(PaymentRequestDTO paymentRequest);

boolean completePayment(UUID paymentUuid) ;

Optional<PaymentResponseDTO> getPaymentById(UUID paymentId);

Expand Down
17 changes: 16 additions & 1 deletion payment/src/main/java/payment/services/PaymentServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -115,4 +119,15 @@ public List<PaymentResponseDTO> 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 ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface PaymentOutBoxProcessor {
public void processOutboxEvents();
// public void processFailedEvents() ;
public void processFailedEvents() ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,6 +24,8 @@ public class PaymentOutBoxProcessorImpl implements PaymentOutBoxProcessor {

@Inject
PaymentRepository paymentRepository;
@Inject
PaymentService paymentService ;

@Inject
@RestClient
Expand All @@ -36,7 +39,7 @@ public void processOutboxEvents() {
List<PaymentOutBox> unprocessedEvents = boxRepository.findUnprocessedEvents();

for (PaymentOutBox event : unprocessedEvents) {
try {

Payment payment = paymentRepository.findById(event.getPaymentId());
BankPaymentRequest bankPaymentRequest = new BankPaymentRequest(
event.getPaymentId(),
Expand All @@ -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'");
}

}

0 comments on commit 9641b16

Please sign in to comment.