public IActionResult EditQty(ManagerProductViewModel viewModel)
 {
     if (viewModel.Product.Quantity >= 0)
     {
         _ProductData.EditQty(viewModel.Product);
     }
     return(Json(new { qty = viewModel.Product.Quantity, message = "success" }));
 }
Exemple #2
0
        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"));
        }