Skip to content

Commit

Permalink
task completed payment methods refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
MolchanovAlexander committed Jun 11, 2024
1 parent c9c7ceb commit ba1383a
Showing 1 changed file with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,21 @@ public PaymentResponseDto createPaymentSession(
var duration = java.time.temporal.ChronoUnit.DAYS.between(
booking.getCheckInDate(),
booking.getCheckOutDate());
var amountTopay = accommodation.dailyRate().multiply(BigDecimal.valueOf(duration));
var amountToPay = accommodation.dailyRate().multiply(BigDecimal.valueOf(duration));
User user = (User) authentication.getPrincipal();
return getPaymentResponseDto(sessionDto, user, amountToPay);
}

private PaymentResponseDto getPaymentResponseDto(
PaymentRequestDto sessionDto,
User user,
BigDecimal amountToPay
) {
try {
Customer customer = findOrCreateCustomer(
user.getEmail(), user.getFirstName());
SessionCreateParams.Builder sessionCreateParamsBuilder = getParamsBuilder(
sessionDto, customer, amountTopay);
sessionDto, customer, amountToPay);

SessionCreateParams.PaymentIntentData paymentIntentData =
SessionCreateParams.PaymentIntentData.builder()
Expand Down Expand Up @@ -98,19 +106,24 @@ public PaymentResponseDto retrieveSession(String id) {
);
PaymentResponseDto responseDto = paymentMapper.toDto(payment);
if (session.getPaymentStatus().equals("paid")) {
payment.setStatus(Payment.Status.PAID);
paymentRepository.save(payment);
responseDto = paymentMapper.toDto(payment);
Booking booking = bookingService.getBooking(payment.getBookingId());
booking.setStatus(Booking.Status.CONFIRMED);
bookingService.saveBooking(booking);
responseDto.setMessage("Payment successful.");
return responseDto;
return getPaymentResponseDtoFromPayment(payment);
}
responseDto.setMessage("Payment paused, you can complete it later.");
return responseDto;
}

private PaymentResponseDto getPaymentResponseDtoFromPayment(Payment payment) {
PaymentResponseDto responseDto;
payment.setStatus(Payment.Status.PAID);
paymentRepository.save(payment);
responseDto = paymentMapper.toDto(payment);
Booking booking = bookingService.getBooking(payment.getBookingId());
booking.setStatus(Booking.Status.CONFIRMED);
bookingService.saveBooking(booking);
responseDto.setMessage("Payment successful.");
return responseDto;
}

@Scheduled(cron = "0 0 */1 * * *", zone = "Europe/Kiev")
public void checkExpiredSessions() {
List<Payment> expiredPayments =
Expand All @@ -128,7 +141,7 @@ public List<PaymentResponseDto> findPaymentsByUserId(Long userId, Pageable pagea

public SessionCreateParams.Builder getParamsBuilder(
PaymentRequestDto sessionDto, Customer customer,
BigDecimal amountTopay) {
BigDecimal amountToPay) {
SessionCreateParams.Builder sessionCreateParamsBuilder = SessionCreateParams.builder()
.setMode(SessionCreateParams.Mode.PAYMENT)
.setCustomer(customer.getId())
Expand All @@ -149,7 +162,7 @@ public SessionCreateParams.Builder getParamsBuilder(
.build()
)
.setCurrency("USD")
.setUnitAmountDecimal(amountTopay
.setUnitAmountDecimal(amountToPay
.multiply(BigDecimal.valueOf(100L))
)
.build())
Expand Down

0 comments on commit ba1383a

Please sign in to comment.