Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import lombok.RequiredArgsConstructor;
- import org.springframework.stereotype.Service;
- @Service
- @RequiredArgsConstructor
- public class PaymentService {
- private final PaymentRepository paymentRepository;
- private final FeeService feeService;
- private final UserService userService;
- private final CbrRestClient cbrRestClient;
- /**
- * detailed documentation
- * @param paymentDetails
- */
- @Transactional
- public void processPayment(PaymentDetails paymentDetails) {
- double someGoodName = cbrRestClient.doRequest().getRates().get(paymentDetails.getCurrency().getCode());
- double amountInUsd = paymentDetails.getAmount() * someGoodName;
- User user = userService.findCurrentUser();
- paymentRepository.save(new Payment(amountInUsd, user, paymentDetails.getRecipientId()));
- feeService.processFee(amountInUsd, user);
- }
- }
- import lombok.RequiredArgsConstructor;
- import org.springframework.stereotype.Service;
- @Service
- @RequiredArgsConstructor
- public class FeeService {
- private final FeeRepository feeRepository;
- private final NotificationRestClient notificationRestClient;
- /**
- * detailed documentation
- * @param amountInUsd
- * @param user
- */
- public void processFee(double amountInUsd, User user) {
- double coefficient = 0.015;
- if (amountInUsd > 1000) {
- coefficient = 0.01;
- }
- if (amountInUsd > 5000) {
- coefficient = 0.005;
- }
- try {
- feeRepository.save(new Fee(amountInUsd * coefficient, user));
- notificationRestClient.notify(payment);
- } catch (DataAccessException e) {
- // log cause
- // throw new SomeCustomRuntimeException();
- } catch (Exception e) {
- // log cause
- // throw new SomeOtherCustomRuntimeException();
- }
- }
- }
- import lombok.Data;
- @Data
- public class PaymentDetails {
- private double amount;
- private Currency currency;
- private Long recipientId;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement