Esempio n. 1
0
        public DepositDTO TransferFromAccount(string username, string cardName, DepositDTO dto)
        {
            var account = accountService.GetAccountForUser(username);

            if (account == null)
            {
                return(null);
            }

            var vc = virtualCardRepository.GetByAccountIdAndName(account.Id, cardName);

            account.Balance = account.Balance - dto.Deposit;

            if (account.Balance < 0.00)
            {
                var thFailed = convertToTH(dto.Deposit, "deposit", "failed", account.Currency, vc.CardNumber);
                account.Transactions.Add(TransactionHistoryConverter.ToEntity(thFailed));
                accountService.Save(account);
                throw new Exception("You don't have that much money into your account!!!");
            }

            vc.Balance = vc.Balance + dto.Deposit;
            accountService.Save(account);
            virtualCardRepository.Save(vc);

            var th = convertToTH(dto.Deposit, "deposit", "success", account.Currency, vc.CardNumber);

            th.AccountId = account.Id;
            transactionHistoryService.Save(th);

            return(dto);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public ExchangeDTO ExchangeFromAccount(string username, ExchangeDTO dto)
        {
            var account = accountService.GetAccountForUser(username);

            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");
            }

            account.Balance = account.Balance - dto.Amount;

            if (account.Balance < 0.00)
            {
                throw new Exception("not enough balance in the account");
            }

            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             = "Account";

            account.Transactions.Add(TransactionHistoryConverter.ToEntity(originTransaction));
            recipient.Transactions.Add(TransactionHistoryConverter.ToEntity(transaction));

            accountService.Save(account);
            accountService.Save(recipient);

            return(dto);
        }
 public void Save(TransactionHistoryDTO dto)
 {
     historyRepository.Save(TransactionHistoryConverter.ToEntity(dto));
 }