Advertisement
bakhridinova

Untitled

Dec 15th, 2023 (edited)
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import lombok.RequiredArgsConstructor;
  2. import org.springframework.stereotype.Service;
  3.  
  4. @Service
  5. @RequiredArgsConstructor
  6. public class PaymentService {
  7.     private final PaymentRepository paymentRepository;
  8.     private final FeeService feeService;
  9.     private final UserService userService;
  10.     private final CbrRestClient cbrRestClient;
  11.  
  12.     /**
  13.      * detailed documentation
  14.      * @param paymentDetails
  15.      */
  16.     @Transactional
  17.     public void processPayment(PaymentDetails paymentDetails) {
  18.         double someGoodName = cbrRestClient.doRequest().getRates().get(paymentDetails.getCurrency().getCode());
  19.         double amountInUsd = paymentDetails.getAmount() * someGoodName;
  20.         User user = userService.findCurrentUser();
  21.         paymentRepository.save(new Payment(amountInUsd, user, paymentDetails.getRecipientId()));
  22.         feeService.processFee(amountInUsd, user);
  23.     }
  24. }
  25.  
  26. import lombok.RequiredArgsConstructor;
  27. import org.springframework.stereotype.Service;
  28.  
  29. @Service
  30. @RequiredArgsConstructor
  31. public class FeeService {
  32.     private final FeeRepository feeRepository;
  33.     private final NotificationRestClient notificationRestClient;
  34.  
  35.     /**
  36.      * detailed documentation
  37.      * @param amountInUsd
  38.      * @param user
  39.      */
  40.     public void processFee(double amountInUsd, User user) {
  41.         double coefficient = 0.015;
  42.         if (amountInUsd > 1000) {
  43.             coefficient = 0.01;
  44.         }
  45.         if (amountInUsd > 5000) {
  46.             coefficient = 0.005;
  47.         }
  48.         try {
  49.             feeRepository.save(new Fee(amountInUsd * coefficient, user));
  50.             notificationRestClient.notify(payment);
  51.         } catch (DataAccessException e) {
  52.             // log cause
  53.             // throw new SomeCustomRuntimeException();
  54.         } catch (Exception e) {
  55.             // log cause
  56.             // throw new SomeOtherCustomRuntimeException();
  57.         }
  58.     }
  59. }
  60.  
  61. import lombok.Data;
  62.  
  63. @Data
  64. public class PaymentDetails {
  65.     private double amount;
  66.     private Currency currency;
  67.     private Long recipientId;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement