public IActionResult Delete(CartPageViewModel vm) { var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var foundShoppingProduct = _shoppingCartData.FindCartProductById(vm.ShoppingCart.ProductId, userId); _shoppingCartData.Delete(foundShoppingProduct); return(RedirectToAction("Index", "Cart")); }
public async Task <IActionResult> SaveForLater(int productId) { var vm = new CartModel(); var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (_shoppingCartData.CheckIsExistedInList(productId, userId)) { return(Json(new { success = false, responseText = "The attached file is not supported." })); } else { var foundShoppingProduct = _shoppingCartData.FindCartProductById(productId, userId); Product foundProduct = _productData.FindProductById(productId); _shoppingCartData.Delete(foundShoppingProduct); SaveForLater foundSaveForLater = _shoppingCartData.SaveForLater(productId, userId); vm = await InitModel(userId); return(Json(vm)); } }
public async Task <IActionResult> PlaceOrder(CheckoutPageViewModel vm) { //Find UserId var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //Find User Object var currentUser = await _userManager.FindByIdAsync(userId); //Find all CartItem and ready to process checkout IEnumerable <ShoppingCart> allSavedProducts = _shoppingCartData.GetAllByUser(currentUser); //Find all Product Detail by viewing cart List <Product> allSavedProduct = new List <Product>(); foreach (var item in allSavedProducts) { //Change Item.Product qty to user's intended purchase qty item.Product.Quantity = item.Qty; //add modified-Qty Product to list of products allSavedProduct.Add(item.Product); } ShoppingOrder newOrder = new ShoppingOrder(); newOrder.User = currentUser; newOrder.OrderAddress = vm.ShippingAddress; _checkoutData.SaveOrder(newOrder); foreach (var product in allSavedProduct) { OrderItem Item = new OrderItem(); Item.CurrentPrice = product.Price; Item.ProductId = product.ProductId; Item.Qty = product.Quantity; Item.ShoppingOrderId = newOrder.OrderId; //Modify Inventory of Product Qty product.Quantity -= Item.Qty; if (product.Quantity == 0) { //If user bought all stocks, change property of availiablity to false product.IsAvailiable = false; } _productData.EditQty(product); _checkoutData.SaveOrderItem(Item); } //Delete all item in shopping cart foreach (var product in allSavedProducts) { _shoppingCartData.Delete(product); } var myCharge = new StripeChargeCreateOptions(); // always set these properties int total = (int)vm.Total; myCharge.Amount = total * 100;; myCharge.Currency = "usd"; // set this if you want to myCharge.Description = "Charge it like it's hot"; myCharge.SourceTokenOrExistingSourceId = vm.StripeToken; // set this property if using a customer - this MUST be set if you are using an existing source! // (not required) set this to false if you don't want to capture the charge yet - requires you call capture later myCharge.Capture = true; var StripeKey = Environment.GetEnvironmentVariable("StripeSecretKey"); var chargeService = new StripeChargeService(StripeKey); StripeCharge stripeCharge = chargeService.Create(myCharge); return(RedirectToAction("Index", "Cart")); }