public ExchangeDTO ExchangeFromVirtualCard(string username, string virtualCardName, ExchangeDTO dto) { var account = accountService.GetAccountForUser(username); var vc = virtualCardService.GetVirtualCard(username, virtualCardName); if (!planService.CanExchange(account.PlanId, dto.Amount, false)) { var th = convertToTH(dto, "failed"); account.Transactions.Add(TransactionHistoryConverter.ToEntity(th)); accountService.Save(account); throw new Exception("Couldn't complete transaction. The desired sum is bigger than the max allowed for the current plan or you dont have any more transactions permitted"); } vc.Balance = vc.Balance - dto.Amount; if (vc.Balance < 0.00) { throw new Exception("not enough balance in the virtual card"); } var recipient = accountService.GetAccountForUser(dto.To); if (recipient == null) { throw new Exception("Couldn't find such a user"); } recipient.Balance += dto.Amount; var transaction = convertToTH(dto, "success"); transaction.Name = "Account"; var originTransaction = transaction.ShallowCopy(); originTransaction.TransactionCost *= -1; // we need to make the transaction cost negative since its a withdraw from the sender`s account originTransaction.Name = vc.CardNumber; originTransaction.AccountId = account.Id; transaction.AccountId = recipient.Id; historyService.Save(originTransaction); historyService.Save(transaction); planService.ExecuteTransaction(account.PlanId); virtualCardService.Save(vc); return(dto); }
public DepositDTO DepositToVirtualCard(string username, string cardName, DepositDTO depositDTO) { var account = accountService.GetAccountForUser(username); if (account == null) { return(null); } var vc = virtualCardRepository.GetByAccountIdAndName(account.Id, cardName); vc.Balance = vc.Balance + depositDTO.Deposit; virtualCardRepository.Save(vc); var th = convertToTH(depositDTO.Deposit, "deposit", "success", account.Currency, vc.CardNumber); th.AccountId = account.Id; transactionHistoryService.Save(th); return(depositDTO); }