diff --git a/dao/src/main/java/greencity/repository/NotificationParameterRepository.java b/dao/src/main/java/greencity/repository/NotificationParameterRepository.java index ea06e6f26..1658c1806 100644 --- a/dao/src/main/java/greencity/repository/NotificationParameterRepository.java +++ b/dao/src/main/java/greencity/repository/NotificationParameterRepository.java @@ -10,4 +10,7 @@ @Repository public interface NotificationParameterRepository extends JpaRepository { Optional> findNotificationParameterByUserNotification(UserNotification userNotification); + + Optional findNotificationParameterByUserNotificationAndKey(UserNotification userNotification, + String key); } diff --git a/service/src/main/java/greencity/service/ubs/UBSClientServiceImpl.java b/service/src/main/java/greencity/service/ubs/UBSClientServiceImpl.java index a0d3f8a3e..0785868ca 100644 --- a/service/src/main/java/greencity/service/ubs/UBSClientServiceImpl.java +++ b/service/src/main/java/greencity/service/ubs/UBSClientServiceImpl.java @@ -63,6 +63,8 @@ import greencity.dto.user.UserProfileDto; import greencity.dto.user.UserProfileUpdateDto; import greencity.entity.coords.Coordinates; +import greencity.entity.notifications.NotificationParameter; +import greencity.entity.notifications.UserNotification; import greencity.entity.order.Bag; import greencity.entity.order.Certificate; import greencity.entity.order.ChangeOfPoints; @@ -92,6 +94,7 @@ import greencity.enums.CourierLimit; import greencity.enums.LocationStatus; import greencity.enums.MonoBankStatuses; +import greencity.enums.NotificationType; import greencity.enums.OrderPaymentStatus; import greencity.enums.OrderStatus; import greencity.enums.PaymentStatus; @@ -115,6 +118,7 @@ import greencity.repository.EmployeeRepository; import greencity.repository.EventRepository; import greencity.repository.LocationRepository; +import greencity.repository.NotificationParameterRepository; import greencity.repository.OrderAddressRepository; import greencity.repository.OrderBagRepository; import greencity.repository.OrderPaymentStatusTranslationRepository; @@ -127,6 +131,7 @@ import greencity.repository.TariffsInfoRepository; import greencity.repository.TelegramBotRepository; import greencity.repository.UBSUserRepository; +import greencity.repository.UserNotificationRepository; import greencity.repository.UserRepository; import greencity.repository.ViberBotRepository; import greencity.service.DistanceCalculationUtils; @@ -231,6 +236,7 @@ public class UBSClientServiceImpl implements UBSClientService { private static final Long CITY_ID_KIEV = 3L; private static final String KYIV_CITY = "Kyiv City"; private static final Integer VALIDITY_DURATION_TEN_DAYS = 864000; + private static final String PAY_BUTTON = "payButton"; private final UserRepository userRepository; private final BagRepository bagRepository; private final UBSUserRepository ubsUserRepository; @@ -267,6 +273,8 @@ public class UBSClientServiceImpl implements UBSClientService { private final MonoBankClient monoBankClient; private final NotificationServiceImpl notificationServiceImpl; private final UnpaidOrderNotificator unpaidOrderNotificator; + private final UserNotificationRepository userNotificationRepository; + private final NotificationParameterRepository notificationParameterRepository; @Value("${greencity.bots.viber-bot-uri}") private String viberBotUri; @@ -1689,6 +1697,7 @@ protected void checkOrderStatusApproved(PaymentResponseDto dto, orderPayment.setPaymentStatus(PaymentStatus.PAID); order.setOrderPaymentStatus(OrderPaymentStatus.PAID); orderPayment.setOrder(order); + removePaymentLinkForOrder(order); paymentRepository.save(orderPayment); orderRepository.save(order); eventService.save(OrderHistory.ORDER_PAID, OrderHistory.SYSTEM, order); @@ -2178,6 +2187,7 @@ private void checkPaymentResponseStatus(MonoBankPaymentResponseDto response, Pay case SUCCESS -> { updatePaymentAndOrderStatus(payment, order, PaymentStatus.PAID, OrderPaymentStatus.PAID); logPaymentEvent(order, payment.getPaymentId()); + removePaymentLinkForOrder(order); } case REVERSED -> { updatePaymentAndOrderStatus(payment, order, PaymentStatus.UNPAID, OrderPaymentStatus.UNPAID); @@ -2212,4 +2222,14 @@ private void logPaymentEvent(Order order, String paymentId) { eventService.save(OrderHistory.ORDER_PAID, OrderHistory.SYSTEM, order); eventService.save(OrderHistory.ADD_PAYMENT_SYSTEM + paymentId, OrderHistory.SYSTEM, order); } + + private void removePaymentLinkForOrder(Order order) { + Optional userNotification = userNotificationRepository + .findUserNotificationByOrderAndNotificationType(order, NotificationType.UNPAID_ORDER); + if (userNotification.isPresent()) { + Optional notificationParameter = notificationParameterRepository + .findNotificationParameterByUserNotificationAndKey(userNotification.get(), PAY_BUTTON); + notificationParameter.ifPresent(notificationParameterRepository::delete); + } + } } diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index 7dc6aa214..18b51f7be 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -2868,6 +2868,11 @@ public static Set getNotificationParameterSet() { return parameters; } + public static Optional getNotificationPaymentLink() { + return Optional + .ofNullable(NotificationParameter.builder().key("payButton").value("https://pay.monobank.ua/api").build()); + } + private static Set createNotificationParameterSet2() { Set parameters = new HashSet<>(); diff --git a/service/src/test/java/greencity/service/ubs/UBSClientServiceImplTest.java b/service/src/test/java/greencity/service/ubs/UBSClientServiceImplTest.java index 29cfae9d5..a3add9536 100644 --- a/service/src/test/java/greencity/service/ubs/UBSClientServiceImplTest.java +++ b/service/src/test/java/greencity/service/ubs/UBSClientServiceImplTest.java @@ -48,6 +48,7 @@ import greencity.dto.user.UserProfileDto; import greencity.dto.user.UserProfileUpdateDto; import greencity.entity.coords.Coordinates; +import greencity.entity.notifications.UserNotification; import greencity.entity.order.Bag; import greencity.entity.order.Certificate; import greencity.entity.order.Event; @@ -69,6 +70,7 @@ import greencity.enums.CertificateStatus; import greencity.enums.CourierLimit; import greencity.enums.LocationStatus; +import greencity.enums.NotificationType; import greencity.enums.OrderPaymentStatus; import greencity.enums.OrderStatus; import greencity.enums.PaymentStatus; @@ -90,6 +92,7 @@ import greencity.repository.EmployeeRepository; import greencity.repository.EventRepository; import greencity.repository.LocationRepository; +import greencity.repository.NotificationParameterRepository; import greencity.repository.OrderAddressRepository; import greencity.repository.OrderBagRepository; import greencity.repository.OrderPaymentStatusTranslationRepository; @@ -102,6 +105,7 @@ import greencity.repository.TariffsInfoRepository; import greencity.repository.TelegramBotRepository; import greencity.repository.UBSUserRepository; +import greencity.repository.UserNotificationRepository; import greencity.repository.UserRepository; import greencity.repository.ViberBotRepository; import greencity.service.google.GoogleApiService; @@ -187,6 +191,7 @@ import static greencity.ModelUtils.getLocation; import static greencity.ModelUtils.getMaximumAmountOfAddresses; import static greencity.ModelUtils.getMonoBankPaymentResponseDto; +import static greencity.ModelUtils.getNotificationPaymentLink; import static greencity.ModelUtils.getOrder; import static greencity.ModelUtils.getOrder2; import static greencity.ModelUtils.getOrderCount; @@ -226,6 +231,7 @@ import static greencity.ModelUtils.getUser; import static greencity.ModelUtils.getUserForCreate; import static greencity.ModelUtils.getUserInfoDto; +import static greencity.ModelUtils.getUserNotificationForUnpaidOrder; import static greencity.ModelUtils.getUserPointsAndAllBagsDto; import static greencity.ModelUtils.getUserProfileCreateDto; import static greencity.ModelUtils.getUserProfileUpdateDto; @@ -397,6 +403,12 @@ class UBSClientServiceImplTest { @Mock private NotificationServiceImpl notificationServiceImpl; + @Mock + private UserNotificationRepository userNotificationRepository; + + @Mock + private NotificationParameterRepository notificationParameterRepository; + @Value("${greencity.monobank.token}") private String token; @@ -3791,6 +3803,12 @@ void testValidatePaymentSuccess() { when(orderRepository.findById(1L)).thenReturn(Optional.of(expectedOrder)); when(encryptionUtil.formResponseSignature(any(PaymentResponseWayForPay.class), eq(wayForPaySecret))) .thenReturn("signature"); + when(userNotificationRepository.findUserNotificationByOrderAndNotificationType(any(Order.class), + any(NotificationType.class))) + .thenReturn(Optional.ofNullable(getUserNotificationForUnpaidOrder())); + when(notificationParameterRepository + .findNotificationParameterByUserNotificationAndKey(any(UserNotification.class), anyString())) + .thenReturn(getNotificationPaymentLink()); PaymentResponseWayForPay result = ubsClientService.validatePayment(response); @@ -3801,6 +3819,10 @@ void testValidatePaymentSuccess() { verify(orderRepository).findById(1L); verify(encryptionUtil).formResponseSignature(any(PaymentResponseWayForPay.class), eq(wayForPaySecret)); + verify(userNotificationRepository) + .findUserNotificationByOrderAndNotificationType(any(Order.class), any(NotificationType.class)); + verify(notificationParameterRepository) + .findNotificationParameterByUserNotificationAndKey(any(UserNotification.class), anyString()); } @Test @@ -4241,6 +4263,12 @@ void validatePaymentFromMonoBankWithSuccessStatusTest() { Order order = getOrder(); when(orderRepository.findById(anyLong())).thenReturn(Optional.of(order)); + when(userNotificationRepository.findUserNotificationByOrderAndNotificationType(any(Order.class), + any(NotificationType.class))) + .thenReturn(Optional.ofNullable(getUserNotificationForUnpaidOrder())); + when(notificationParameterRepository + .findNotificationParameterByUserNotificationAndKey(any(UserNotification.class), anyString())) + .thenReturn(getNotificationPaymentLink()); ubsClientService.validatePaymentFromMonoBank(response); @@ -4248,6 +4276,10 @@ void validatePaymentFromMonoBankWithSuccessStatusTest() { verify(paymentRepository).save(any()); verify(orderRepository).save(any()); verify(eventService, times(2)).save(anyString(), anyString(), any()); + verify(userNotificationRepository) + .findUserNotificationByOrderAndNotificationType(any(Order.class), any(NotificationType.class)); + verify(notificationParameterRepository) + .findNotificationParameterByUserNotificationAndKey(any(UserNotification.class), anyString()); } @ParameterizedTest