Created
September 8, 2022 11:35
-
-
Save k-groshev/20ab0778da788bd42585dcedfce7b99b to your computer and use it in GitHub Desktop.
test java code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 amountInRub = amount * cbrRestClient.doRequest().getRates().get(currency.getCode()); | |
Long userId = (Long) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | |
User user = userRepository.findUserById(userId); | |
Payment payment = new Payment(amountInRub, user, recipientId); | |
paymentRepository.save(payment); | |
if (amountInRub < 1000) { | |
Fee fee = new Fee(amountInRub * 0.015, user); | |
feeRepository.save(fee); | |
} | |
if (amountInRub > 1000) { | |
Fee fee = new Fee(amountInRub * 0.01, user); | |
feeRepository.save(fee); | |
} | |
if (amountInRub > 5000) { | |
Fee fee = new Fee(amountInRub * 0.005, user); | |
feeRepository.save(fee); | |
} | |
try { | |
notificationRestClient.notify(payment); | |
} catch (Exception e) { | |
// do nothing | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment