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 Payment(PaymentViewPost paymentViewPost) { Amount = paymentViewPost.Amount; SourceAccountId = paymentViewPost.SourceAccountId; DestinationAccountId = paymentViewPost.DestinationAccountId; DateTime = DateTime.Now; NumberOfInstallments = paymentViewPost.NumberOfPortions; }