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

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

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

            vc.Balance = vc.Balance - dto.Deposit;
            if (vc.Balance < 0.00)
            {
                var thFailed = convertToTH(dto.Deposit * -1, "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!!!");
            }
            virtualCardRepository.Save(vc);

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

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

            return(dto);
        }
        public async Task <decimal> WithdrawPocketAsync(WithdrawDTO request)
        {
            decimal result;

            try
            {
                var currencyAccount = DbContext.Holders
                                      .Include(nameof(PocketHolder.Accounts))
                                      .SingleOrDefault(CheckHolder(request))
                                      .Accounts
                                      .Find(acc => acc.Currency == request.Currency);

                if (currencyAccount == null)
                {
                    throw new AccountNotFoundException();
                }

                if (currencyAccount.Debit < request.Sum)
                {
                    throw new InsufficientFundException();
                }

                currencyAccount.Debit -= request.Sum;
                DbContext.Update(currencyAccount);
                await DbContext.SaveChangesAsync();

                result = currencyAccount.Debit;
            }
            catch (DbUpdateConcurrencyException ex)
            {
                throw new ConcurrencyException();
            }

            return(result);
        }
Esempio n. 3
0
        public async Task <IActionResult> WithdrawAccount(string accountNumber, [FromBody] WithdrawDTO request)
        {
            if (string.IsNullOrEmpty(accountNumber) ||
                accountNumber != request.MasterAccount)
            {
                return(BadRequest(request));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var result = await PocketService.WithdrawPocketAsync(request);

                return(new JsonResult(result));
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }
Esempio n. 4
0
 public IHttpActionResult WithdrawFromVitualCard(string username, string cardname, WithdrawDTO dto)
 {
     return(Json(_service.WithdrawFromVirtualCard(username, cardname, dto)));
 }
Esempio n. 5
0
 public string Deposit([FromBody] WithdrawDTO value)
 {
     return(value.Amount + " Deposited successfully");
 }
Esempio n. 6
0
 public string Withdrawal([FromBody] WithdrawDTO value)
 {
     return("Deposited successfully");
 }
Esempio n. 7
0
        public async Task <IActionResult> Withdraw([FromBody] WithdrawDTO model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ResponseMessage.Message("Bad request", errors: new { message = ModelState })));
            }

            var isValid = CurrencyConverter.ValidateCurrencyInput(model.TransactionCurrency);

            if (!isValid)
            {
                return(BadRequest(ResponseMessage.Message("Error", errors: new { message = "Invalid currency input" })));
            }

            var loggedInUser = await _userManager.GetUserAsync(User);

            if (loggedInUser == null)
            {
                return(NotFound(ResponseMessage.Message("Not found", errors: new { message = "Could not access user" })));
            }

            var loggedInUserRoles = await _userManager.GetRolesAsync(loggedInUser);

            Transaction transaction;

            if (loggedInUserRoles[0] == "Noob")
            {
                var wallets = await _walletRepository.GetWalletsByUserId(loggedInUser.Id);

                var wallet = wallets.ToList()[0];

                transaction = new Transaction
                {
                    TransactionId       = Guid.NewGuid().ToString(),
                    TransactionType     = "Debit",
                    WalletId            = wallet.WalletId,
                    TransactionStatus   = "pending",
                    Amount              = model.Amount,
                    TransactionCurrency = model.TransactionCurrency
                };

                try
                {
                    await _transactionRepository.AddTransaction(transaction);

                    return(Ok(ResponseMessage.Message($"Success! Transaction with id {transaction.TransactionId} created. Withdrawal approval pending")));
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                    return(BadRequest(ResponseMessage.Message("Bad request", errors: new { message = "Failed to add transaction" })));
                }
            }
            else
            {
                var wallets = await _walletRepository.GetWalletsByUserId(loggedInUser.Id);

                var wallet = wallets.FirstOrDefault(x => x.WalletCurrency == model.TransactionCurrency);

                string transactionCurrency = model.TransactionCurrency;
                if (wallet == null)
                {
                    try
                    {
                        var mainCurrencyDetail = await _userMainCurrencyRepository.GetMainCurrencyByUserId(loggedInUser.Id);

                        var mainCurrency = mainCurrencyDetail.MainCurrency;
                        wallet = await _walletRepository.GetWalletByWalletCurrency(mainCurrency);

                        transactionCurrency = mainCurrency;
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e.Message);
                        return(BadRequest(ResponseMessage.Message("Data access error", errors: new { message = "Could not access record from data source" })));
                    }
                }

                transaction = new Transaction
                {
                    TransactionId       = Guid.NewGuid().ToString(),
                    TransactionType     = "Debit",
                    WalletId            = wallet.WalletId,
                    Amount              = model.Amount,
                    TransactionCurrency = model.TransactionCurrency
                };

                try
                {
                    await _transactionRepository.AddTransaction(transaction);
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message);
                    return(BadRequest(ResponseMessage.Message("Bad request", errors: new { message = "Failed to add transaction" })));
                }

                try
                {
                    decimal amount;
                    try
                    {
                        amount = await CurrencyConverter.ConvertCurrency(model.TransactionCurrency, transactionCurrency, model.Amount);
                    }
                    catch (Exception e)
                    {
                        return(BadRequest(ResponseMessage.Message("Error", errors: new { message = e.Message })));
                    }

                    if (wallet.Balance >= amount)
                    {
                        wallet.Balance -= amount;

                        await _walletRepository.UpdateWallet(wallet);

                        return(Ok(ResponseMessage.Message("Success! Withdrawal successful")));
                    }
                    else
                    {
                        return(BadRequest(ResponseMessage.Message("Bad request", errors: new { message = "Insufficient funds" })));
                    }
                }
                catch (Exception e)
                {
                    await _transactionRepository.DeleteTransaction(transaction);

                    _logger.LogError(e.Message);
                    return(BadRequest(ResponseMessage.Message("Bad request", errors: new { message = "Failed to fund wallet" })));
                }
            }
        }