Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete unused payment links on order payment and update related #1578

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
@Repository
public interface NotificationParameterRepository extends JpaRepository<NotificationParameter, Long> {
Optional<Set<NotificationParameter>> findNotificationParameterByUserNotification(UserNotification userNotification);

Optional<NotificationParameter> findNotificationParameterByUserNotificationAndKey(UserNotification userNotification,
String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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> userNotification = userNotificationRepository
.findUserNotificationByOrderAndNotificationType(order, NotificationType.UNPAID_ORDER);
if (userNotification.isPresent()) {
Optional<NotificationParameter> notificationParameter = notificationParameterRepository
.findNotificationParameterByUserNotificationAndKey(userNotification.get(), PAY_BUTTON);
notificationParameter.ifPresent(notificationParameterRepository::delete);
}
}
}
5 changes: 5 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2868,6 +2868,11 @@ public static Set<NotificationParameter> getNotificationParameterSet() {
return parameters;
}

public static Optional<NotificationParameter> getNotificationPaymentLink() {
return Optional
.ofNullable(NotificationParameter.builder().key("payButton").value("https://pay.monobank.ua/api").build());
}

private static Set<NotificationParameter> createNotificationParameterSet2() {
Set<NotificationParameter> parameters = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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.Event;
Expand All @@ -69,6 +71,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;
Expand All @@ -90,6 +93,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;
Expand All @@ -102,6 +106,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;
Expand Down Expand Up @@ -187,6 +192,8 @@
import static greencity.ModelUtils.getLocation;
import static greencity.ModelUtils.getMaximumAmountOfAddresses;
import static greencity.ModelUtils.getMonoBankPaymentResponseDto;
import static greencity.ModelUtils.getNotificationParameterSet;
import static greencity.ModelUtils.getNotificationPaymentLink;
import static greencity.ModelUtils.getOrder;
import static greencity.ModelUtils.getOrder2;
import static greencity.ModelUtils.getOrderCount;
Expand Down Expand Up @@ -226,6 +233,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;
Expand Down Expand Up @@ -397,6 +405,12 @@ class UBSClientServiceImplTest {
@Mock
private NotificationServiceImpl notificationServiceImpl;

@Mock
private UserNotificationRepository userNotificationRepository;

@Mock
private NotificationParameterRepository notificationParameterRepository;

@Value("${greencity.monobank.token}")
private String token;

Expand Down Expand Up @@ -3791,6 +3805,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);

Expand All @@ -3801,6 +3821,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
Expand Down Expand Up @@ -4241,13 +4265,23 @@ 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);

verify(orderRepository).findById(order.getId());
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
Expand Down
Loading