public async Task <IActionResult> Checkout(UserCheckoutCartViewModel model) { if (!ModelState.IsValid) { TempData.AddErrorMessage(string.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)))); return(View(model)); } try { var cartItems = await GetCurrentUserCartItems(); var purchaseProduct = cartItems .Select(c => new PurchaseProduct { Id = c.Id, PriceAfterDiscount = c.PriceAfterDiscount, }) .ToList(); await _purchaseService.AddPurchase(purchaseProduct, User.Identity.Name, model.Address); } catch (InvalidOperationException ex) { TempData.AddErrorMessage(ex.Message); return(RedirectToAction(nameof(UsersController.Checkout), UsersControllerName)); } TempData.AddSuccessMessage(TempMessages.ThankYouPurchase); return(Redirect(Home)); }
public void Deposit_WithValidInput_ShouldIncrementUserAccountBalance() { const string currentUserName = "******"; var fakeSession = new Dictionary <string, object>(); var controller = InstanciateWithMocks(); PrepareController(controller, currentUserName, fakeSession); var products = new Fixture().CreateMany <ProductModel>().ToList(); var model = new UserCheckoutCartViewModel { CartProducts = products, CreditCardId = "4485504638364536", TotalCost = products.Sum(p => p.Price), Address = "Test adress", }; }
public async Task Checkout_WithCreditCardIdButWithoutExistingCreditCards_ShouldNotThrow() { const string currentUserName = "******"; var fakeSession = new Dictionary <string, object>(); var controller = InstanciateWithMocks(); PrepareController(controller, currentUserName, fakeSession); var products = new Fixture().CreateMany <ProductModel>().ToList(); var model = new UserCheckoutCartViewModel { CartProducts = products, CreditCardId = "4485504638364536", TotalCost = products.Sum(p => p.Price), Address = "Test adress", }; await controller.Checkout(model); }
public async Task <IActionResult> Checkout() { var creditCards = (await _userService.PaymentMethods(User.Identity.Name)) .Select(u => new SelectListItem { Text = CreditCardHelper.ReplaceCreditCardUI(u.CreditCardNumber), Value = u.Id.ToString(), }) .ToList(); var cartProducts = await GetCurrentUserCartItems(); var user = await _userManager.FindByNameAsync(User.Identity.Name); var model = new UserCheckoutCartViewModel { CartProducts = cartProducts, CreditCards = creditCards, TotalCost = cartProducts.Sum(p => p.Price), PhoneNumber = user.PhoneNumber, }; return(View(model)); }