From ba1383a21baed17dd9406bb2d24d01a708838f09 Mon Sep 17 00:00:00 2001 From: MolchanovAlexander Date: Tue, 11 Jun 2024 19:57:26 +0300 Subject: [PATCH] task completed payment methods refactored --- .../service/impl/StripeServiceImpl.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/ua/accommodation/service/impl/StripeServiceImpl.java b/src/main/java/com/ua/accommodation/service/impl/StripeServiceImpl.java index 9b23d02..16ace1b 100644 --- a/src/main/java/com/ua/accommodation/service/impl/StripeServiceImpl.java +++ b/src/main/java/com/ua/accommodation/service/impl/StripeServiceImpl.java @@ -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() @@ -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 expiredPayments = @@ -128,7 +141,7 @@ public List 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()) @@ -149,7 +162,7 @@ public SessionCreateParams.Builder getParamsBuilder( .build() ) .setCurrency("USD") - .setUnitAmountDecimal(amountTopay + .setUnitAmountDecimal(amountToPay .multiply(BigDecimal.valueOf(100L)) ) .build())