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));
        }
        public async Task <bool> WithdrawAmmount(int accountId, decimal ammount)
        {
            var account = await checkingAccountRepository.GetAccountById(accountId);

            if (account == null)
            {
                throw new AccountNotFoundException("Source account id not found.");
            }

            account.Withdraw(ammount);

            var transaction = new Transaction(account.AccountId, (Math.Abs(ammount) * (-1)));

            transaction.InformCurrentBalance(account.CurrentBalance);

            account.AddTransaction(transaction);
            await checkingAccountRepository.Update(account);

            return(true);
        }
Esempio n. 3
0
        public async Task PostPaymentAccountBalanceTest()
        {
            var manager = new PaymentManager(_checkingAccountRepository, _paymentRepository, _feeRepository, _entryRepository);

            Payment payment = new Payment();

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

            var sourceBalance      = _checkingAccountRepository.GetAccountById(payment.SourceAccountId).Result.Balance;
            var destinationBalance = _checkingAccountRepository.GetAccountById(payment.DestinationAccountId).Result.Balance;

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

            var sourceBalanceAfter      = _checkingAccountRepository.GetAccountById(payment.SourceAccountId).Result.Balance;
            var destinationBalanceAfter = _checkingAccountRepository.GetAccountById(payment.DestinationAccountId).Result.Balance;

            Assert.AreEqual(sourceBalance - 107.77 / 3, sourceBalanceAfter, 0.01);
            Assert.AreEqual(destinationBalance + 107.77 / 3, destinationBalanceAfter, 0.01);
            Assert.AreEqual(sourceBalance - payment.Amount * 1.0777 / 3, sourceBalanceAfter, 0.01);
            Assert.AreEqual(destinationBalance + payment.Amount * 1.0777 / 3, destinationBalanceAfter, 0.01);
        }
        public async Task Consume(ConsumeContext <SearchByAccountRequest> context)
        {
            try
            {
                BankAccount account = await checkingAccountRepository.GetAccountById(context.Message.AccountId);


                await context.RespondAsync <SearchByAccountResponse>(new SearchByAccountResponse()
                {
                    Status = true, Account = account
                });
            }
            catch (Exception ex)
            {
                await context.RespondAsync <SearchByAccountResponse>(new SearchByAccountResponse()
                {
                    Status = false, Message = ex.Message
                });
            }
        }