#230 Code Review of a Payment Processing Service

Companies:
Innotech Aston
Themes:
SpringTransactionsCode Review

Perform a code review:

package review;

@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
        }
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in