public async Task <PaymentViewPostOutput> PostPayment(PaymentViewPost paymentViewPost)
        {
            Fee             fee           = _feeRepository.GetFeeByPortion(paymentViewPost.NumberOfPortions);
            CheckingAccount sourceAccount = await _checkingAccountRepository.GetAccountById(paymentViewPost.SourceAccountId);

            CheckingAccount destinationAccount = await _checkingAccountRepository.GetAccountById(paymentViewPost.DestinationAccountId);

            paymentViewPost.Amount = paymentViewPost.Amount * ((100 + fee.Value) / 100);
            var portion = paymentViewPost.Amount / fee.NumberOfPortions;

            if (sourceAccount.Balance < portion)
            {
                return(null);
            }

            sourceAccount.Balance      = sourceAccount.Balance - portion;
            destinationAccount.Balance = destinationAccount.Balance + portion;

            var payment = new Payment(paymentViewPost);

            for (int i = 0; i < fee.NumberOfPortions; i++)
            {
                _entryRepository.InsertEntry(new Installment(portion, DateTime.Now.AddMonths(i), InstallmentTypeEnum.DEBIT.ToString(), paymentViewPost.SourceAccountId));
                _entryRepository.InsertEntry(new Installment(portion, DateTime.Now.AddMonths(i), InstallmentTypeEnum.CREDIT.ToString(), paymentViewPost.DestinationAccountId));
            }

            _paymentRepository.InsertPayment(payment);
            _checkingAccountRepository.Save();

            return(new PaymentViewPostOutput(paymentViewPost.Amount, sourceAccount.Balance, destinationAccount.Balance));
        }
示例#2
0
        public async Task PostPaymentAmountTest()
        {
            var manager = new PaymentManager(_checkingAccountRepository, _paymentRepository, _feeRepository, _entryRepository);

            Payment payment = new Payment();

            payment.SourceAccountId      = 1;
            payment.DestinationAccountId = 2;
            payment.Amount = 100;
            payment.NumberOfInstallments = 1;

            var value = payment.Amount * (100 + _feeRepository.GetFeeByPortion(payment.NumberOfInstallments).Value) / 100;

            var result = await manager.PostPayment(new PaymentViewPost(payment));

            Assert.AreEqual(value, result.NetValue);
            Assert.AreEqual(103.79, result.NetValue);
        }