public async Task <TransactionCheckResult> CheckTransaction(string accFromName, string userFromName, string accToName, string userToName, decimal amount) { decimal commissionCoeff = 0; TransactionCheckResult result = new TransactionCheckResult() { IsParsed = true }; AccountEntity accEntityFrom = await GetAccountEntityAsync(accFromName, userFromName); AccountEntity accEntityTo = await GetAccountEntityAsync(accToName, userToName); result.CurrFrom = accEntityFrom.Currency; result.CurrTo = accEntityTo.Currency; if (accEntityFrom.IsCommission != true) { commissionCoeff = 0.02m; } Money moneyFrom = new Money(accEntityFrom.Amount, accEntityFrom.Currency.StrToCurrency()); if (!moneyFrom.IsEnoughMoney(new Money(amount, accEntityFrom.Currency.StrToCurrency()), new Money(amount * commissionCoeff, accEntityFrom.Currency.StrToCurrency()))) { result.IsEnough = false; result.SendAmount = accEntityFrom.Amount / (1m + commissionCoeff); } else { decimal exchangeCoeff = await _globalRates.GetRateAsync(accEntityFrom.Currency.StrToCurrency(), accEntityTo.Currency.StrToCurrency()); result.IsEnough = true; result.SendAmount = amount * (1m + commissionCoeff); result.Receive = amount * exchangeCoeff; result.Commission = amount * commissionCoeff; result.Rest = accEntityFrom.Amount - result.SendAmount; } return(result); }
public async Task <TransactionCheckResult> TransactionAsync(string accFromName, string userFromName, string accToName, string userToName, decimal amount) { TransactionCheckResult result = new TransactionCheckResult(); if (accFromName == accToName && userFromName == userToName) { result.IsSameAccs = true; return(result); } AccountEntity accEntityFrom = await GetAccountEntityAsync(accFromName, userFromName); AccountEntity accEntityTo = await GetAccountEntityAsync(accToName, userToName); Account accFrom = new Account(accEntityFrom, AccountCreationOptions.EmptyName); Account accTo = new Account(accEntityTo, AccountCreationOptions.EmptyName); decimal coefficient = await _globalRates.GetRateAsync(accFrom.Money.Curr, accTo.Money.Curr); accFrom.Money.AdjustExchangeRate(accFrom.Money.Curr, accTo.Money.Curr, coefficient); accTo.Money.AdjustExchangeRate(accFrom.Money.Curr, accTo.Money.Curr, coefficient); AccountManager accManager = await GetAccountManagerAsync(accEntityFrom, userFromName); AccountEntity commissionAccEntity = await GetCommissionAccount(); CommissionAccount commissionAcc = new CommissionAccount(commissionAccEntity, AccountCreationOptions.CurrencyOnly); commissionAcc.SubscribeToCommission(accManager); try { accManager.Transaction(accFrom, accTo, amount); } catch (Exception ex) { if (ex.Message == ExceptionMessages.TransactionInsufficientFunds()) { result.IsEnough = false; return(result); } else { throw new Exception(ex.Message); } } commissionAcc.UnsubscribeFromCommission(accManager); result.IsEnough = true; result.SendAmount = accEntityFrom.Amount - accFrom.Money.Amount; result.Commission = result.SendAmount - amount; result.Receive = accTo.Money.Amount - accEntityTo.Amount; result.CommissionOwner = commissionAccEntity.User.UserName; result.CommissionAccName = commissionAccEntity.AccountName; result.CurrFrom = accEntityFrom.Currency; result.CurrTo = accEntityTo.Currency; accEntityFrom.Amount = accFrom.Money.Amount; accEntityTo.Amount = accTo.Money.Amount; commissionAccEntity.Amount += commissionAcc.Money.Amount; DBContext.Accounts.Update(accEntityFrom); DBContext.Accounts.Update(accEntityTo); DBContext.Accounts.Update(commissionAccEntity); await DBContext.SaveChangesAsync(); return(result); }