Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- public class PaymentService {
- @Autowired
- private PaymentRepository paymentRepository;
- @Autowired
- private FeeRepository feeRepository;
- @Autowired
- private UserRepository userRepository;
- @Autowired
- private NotificationRestClient notificationRestClient;
- @Autowired
- private CbrRestClient cbrRestClient;
- @Transactional
- public void processPayment(double amount, Currency currency, Long recipientId) {
- double amountInUsd = amount * cbrRestClient.doRequest().getRates().get(currency.getCode());
- Long userId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- User user = userRepository.findUserById(userId);
- Payment payment = new Payment(amountInUsd, user, recipientId);
- paymentRepository.save(payment);
- if (amountInUsd < 1000) {
- Fee fee = new Fee(amountInUsd * 0.015, user);
- feeRepository.save(fee);
- }
- if (amountInUsd > 1000) {
- Fee fee = new Fee(amountInUsd * 0.01, user);
- feeRepository.save(fee);
- }
- if (amountInUsd > 5000) {
- Fee fee = new Fee(amountInUsd * 0.005, user);
- feeRepository.save(fee);
- }
- try {
- notificationRestClient.notify(payment);
- } catch (Exception e) {
- // do nothing
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment