Exemplo n.º 1
0
        public async Task <IActionResult> AddFunds(CardTransactionViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var userId     = this.userWrapper.GetUserId(HttpContext.User);
                var userWallet = await this.walletService.RetrieveWallet(userId);

                var userCurrency = ((CurrencyOptions)userWallet.CurrencyId).ToString();

                var card = await this.cardService.GetCard(model.CardId);

                if (card.UserId == userId)
                {
                    await this.transactionService.AddDepositTransaction
                        (userId, card.Id, model.Amount, $"Deposited {model.Amount} {userCurrency} with card ending in {card.CardNumber.Substring(12)}");

                    await this.cardService.Deposit(model.CardId, model.Amount);

                    TempData["SuccessfullDeposit"] = $"Succesfully deposited {model.Amount} {userCurrency}";
                    return(this.RedirectToAction("index", "wallet"));
                }
            }

            TempData["FailedDeposit"] = "Failed to deposit. Please try again with a different card";
            return(this.RedirectToAction("index", "wallet"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> WithdrawFunds(CardTransactionViewModel model)
        {
            if (this.ModelState.IsValid)
            {
                var userId     = this.userWrapper.GetUserId(HttpContext.User);
                var userWallet = await this.walletService.RetrieveWallet(userId);

                var userCurrency = ((CurrencyOptions)userWallet.CurrencyId).ToString();

                var card = await this.cardService.GetCard(model.CardId);

                if (card.UserId == userId)
                {
                    try
                    {
                        await this.transactionService.AddWithdrawTransaction
                            (userId, card.Id, model.Amount, $"Withdrew {model.Amount} {userCurrency} with card ending in {card.CardNumber.Substring(12)}");

                        await this.cardService.Withdraw(model.CardId, model.Amount);

                        TempData["SuccesfullWithdraw"] = $"Succesfully withdrew {model.Amount} {userCurrency}";
                        return(this.RedirectToAction("index", "wallet"));
                    }
                    catch (InsufficientFundsException exception)
                    {
                        TempData["FailedWithdraw"] = exception.Message;
                    }
                }
            }
            if (TempData["FailedWithdraw"] == null)
            {
                TempData["FailedWithdraw"] = "Failed to withdraw. Please try again with a different card";
            }
            return(this.RedirectToAction("index", "wallet"));
        }
        public async Task ReturnErrorTempDataIfCardNotBelongingToUser()
        {
            var invalidModel = new CardTransactionViewModel();

            var cardService   = this.SetupCardService();
            var transService  = new Mock <ITransactionService>();
            var userWrapper   = new Mock <IUserWrapper>();
            var walletService = this.SetupWalletSerivce();

            var controller = this.ControllerSetup(walletService.Object, userWrapper.Object, cardService.Object, transService.Object);
            await controller.WithdrawFunds(invalidModel);

            Assert.IsNotNull(controller.TempData["FailedWithdraw"]);
            Assert.AreEqual("Failed to withdraw. Please try again with a different card", controller.TempData["FailedWithdraw"]);
        }
Exemplo n.º 4
0
        public async Task ReturnErrorTempDataIfInvalidModel()
        {
            var invalidModel = new CardTransactionViewModel();

            var cardService   = new Mock <ICardService>();
            var transService  = new Mock <ITransactionService>();
            var userWrapper   = this.SetupUserWrapper();
            var walletService = new Mock <IWalletService>();

            var controller = this.ControllerSetup(walletService.Object, userWrapper.Object, cardService.Object, transService.Object);

            controller.ModelState.AddModelError("error", "error");
            await controller.AddFunds(invalidModel);

            Assert.IsNotNull(controller.TempData["FailedDeposit"]);
            Assert.AreEqual("Failed to deposit. Please try again with a different card", controller.TempData["FailedDeposit"]);
        }
Exemplo n.º 5
0
        public ActionResult CardPayments(Guid Id, [FromForm] CardTransactionsListViewModel model)
        {
            string userId = userManager.GetUserId(User);

            try
            {
                if (model == null)
                {
                    model = new CardTransactionsListViewModel();
                }
                var customer     = customerService.GetCustomerFromUserId(userId);
                var bankAccounts = accountsService.GetCustomerBankAccounts(userId);
                var card         = cardService.GetCardByCardId(Id);
                model.OwnerName    = card.OwnerName;
                model.SerialNumber = card.SerialNumber;
                model.CardId       = Id;


                var cardTransactions = cardService.GetFilteredCardTransactions(card.Id, model.SearchBy, model.TransactionType);
                List <CardTransactionViewModel> cardTransactionViewModel = new List <CardTransactionViewModel>();
                foreach (var transaction in cardTransactions)
                {
                    CardTransactionViewModel temp = new CardTransactionViewModel();

                    temp.Amount          = transaction.Transaction.Amount;
                    temp.Name            = transaction.Transaction.ExternalName;
                    temp.TransactionType = transaction.TransactionType.ToString();
                    temp.DateTime        = transaction.Transaction.Time;
                    cardTransactionViewModel.Add(temp);
                }


                model.CardTransactions = cardTransactionViewModel;


                return(View(model));
            }
            catch (Exception e)
            {
                logger.LogError("Unable to retrieve card payments {ExceptionMessage}", e.Message);
                logger.LogDebug("Unable to retrieve card payments {@Exception}", e);
                return(BadRequest("Unable to process your request"));
            }
        }
        public async Task RedirectCorrectlyIfCardNotBelongingToUser()
        {
            var invalidModel = new CardTransactionViewModel();

            var cardService   = this.SetupCardService();
            var transService  = new Mock <ITransactionService>();
            var userWrapper   = new Mock <IUserWrapper>();
            var walletService = this.SetupWalletSerivce();

            var controller = this.ControllerSetup(walletService.Object, userWrapper.Object, cardService.Object, transService.Object);
            var result     = await controller.WithdrawFunds(invalidModel);

            Assert.IsInstanceOfType(result, typeof(RedirectToActionResult));

            var redirectResult = (RedirectToActionResult)result;

            Assert.AreEqual("index", redirectResult.ActionName);
            Assert.AreEqual("wallet", redirectResult.ControllerName);
        }
        public async Task SetCorrectTempDataIfInsufficientFundsExceptionCaught()
        {
            var invalidModel = new CardTransactionViewModel();

            var cardService  = this.SetupCardService();
            var transService = new Mock <ITransactionService>();

            transService.Setup(ts
                               => ts.AddWithdrawTransaction(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <double>(), It.IsAny <string>()))
            .Throws(new InsufficientFundsException("Insufficient funds"));

            var userWrapper   = this.SetupUserWrapper();
            var walletService = this.SetupWalletSerivce();

            var controller = this.ControllerSetup(walletService.Object, userWrapper.Object, cardService.Object, transService.Object);
            await controller.WithdrawFunds(invalidModel);

            Assert.IsNotNull(controller.TempData["FailedWithdraw"]);
            Assert.AreEqual("Insufficient funds", controller.TempData["FailedWithdraw"]);
        }
        public async Task RedirectCorrectlyIfInvalidModel()
        {
            var invalidModel = new CardTransactionViewModel();

            var cardService   = new Mock <ICardService>();
            var transService  = new Mock <ITransactionService>();
            var userWrapper   = this.SetupUserWrapper();
            var walletService = new Mock <IWalletService>();

            var controller = this.ControllerSetup(walletService.Object, userWrapper.Object, cardService.Object, transService.Object);

            controller.ModelState.AddModelError("error", "error");
            var result = await controller.WithdrawFunds(invalidModel);

            Assert.IsInstanceOfType(result, typeof(RedirectToActionResult));

            var redirectResult = (RedirectToActionResult)result;

            Assert.AreEqual("index", redirectResult.ActionName);
            Assert.AreEqual("wallet", redirectResult.ControllerName);
        }