public IActionResult Index()
        {
            var ClaimsIdentity = (ClaimsIdentity)User.Identity;
            var Claim          = ClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            ShoppingCartVM = new ShoppingCartVM()
            {
                OrderHeader = new Models.OrderHeader(),
                ListCart    = _unitOfWork.ShoppingCart.GetAll(U => U.ApplicationUserId == Claim.Value, includproperties: "Product"),
            };
            ShoppingCartVM.OrderHeader.OrderTotal      = 0;
            ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser
                                                         .GetFirstOrDefault(u => u.Id == Claim.Value, includproperties: "Company");

            foreach (var list in ShoppingCartVM.ListCart)
            {
                list.Price = SD.GetPriceBasedOnQunatity(list.Count, list.Product.Price, list.Product.Price50, list.Product.Price100);
                ShoppingCartVM.OrderHeader.OrderTotal += (list.Price * list.Count);
                list.Product.Description = SD.ConvertToRawHtml(list.Product.Description);
                if (list.Product.Description.Length > 100)
                {
                    list.Product.Description = list.Product.Description.Substring(0, 99) + ".....";
                }
            }

            return(View(ShoppingCartVM));
        }
        public IActionResult Summary()
        {
            var ClaimsIdentity = (ClaimsIdentity)User.Identity;
            var Claim          = ClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            ShoppingCartVM = new ShoppingCartVM()
            {
                OrderHeader = new Models.OrderHeader(),
                ListCart    = _unitOfWork.ShoppingCart.GetAll(o => o.ApplicationUserId == Claim.Value, includproperties: "Product")
            };


            ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(u =>
                                                                                                       u.Id == Claim.Value,
                                                                                                       includproperties: "Company");

            foreach (var list in ShoppingCartVM.ListCart)
            {
                list.Price = SD.GetPriceBasedOnQunatity(list.Count, list.Product.Price, list.Product.Price50, list.Product.Price100);
                ShoppingCartVM.OrderHeader.OrderTotal += (list.Price * list.Count);
            }
            ShoppingCartVM.OrderHeader.Name          = ShoppingCartVM.OrderHeader.ApplicationUser.Name;
            ShoppingCartVM.OrderHeader.PhoneNumber   = ShoppingCartVM.OrderHeader.ApplicationUser.PhoneNumber;
            ShoppingCartVM.OrderHeader.StreetAddress = ShoppingCartVM.OrderHeader.ApplicationUser.StreetAddress;
            ShoppingCartVM.OrderHeader.City          = ShoppingCartVM.OrderHeader.ApplicationUser.City;
            ShoppingCartVM.OrderHeader.State         = ShoppingCartVM.OrderHeader.ApplicationUser.State;
            ShoppingCartVM.OrderHeader.PostalCode    = ShoppingCartVM.OrderHeader.ApplicationUser.PostalCode;

            return(View(ShoppingCartVM));
        }
        public IActionResult Plus(int cartId)
        {
            var cart = _unitOfWork.ShoppingCart.GetFirstOrDefault(u => u.Id == cartId, includproperties: "Product");

            cart.Count += 1;
            cart.Price  = SD.GetPriceBasedOnQunatity(cart.Count, cart.Product.Price, cart.Product.Price50, cart.Product.Price100);
            _unitOfWork.Save();
            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult Minus(int cartId)
        {
            var cart = _unitOfWork.ShoppingCart.GetFirstOrDefault(u => u.Id == cartId, includproperties: "Product");

            if (cart.Count == 1)
            {
                var cnt = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == cart.ApplicationUserId).ToList().Count();
                _unitOfWork.ShoppingCart.Remove(cart);
                _unitOfWork.Save();
                HttpContext.Session.SetInt32(SD.ssShoppingCart, cnt - 1);
            }
            else
            {
                cart.Count -= 1;
                cart.Price  = SD.GetPriceBasedOnQunatity(cart.Count, cart.Product.Price, cart.Product.Price50, cart.Product.Price100);
                _unitOfWork.Save();
            }
            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult SummaryPost(string stripeToken)
        {
            var ClaimsIdentity = (ClaimsIdentity)User.Identity;
            var Claim          = ClaimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

            ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(u =>
                                                                                                       u.Id == Claim.Value,
                                                                                                       includproperties: "Company");
            ShoppingCartVM.ListCart = _unitOfWork.ShoppingCart.GetAll(o =>
                                                                      o.ApplicationUserId == Claim.Value,
                                                                      includproperties: "Product");

            ShoppingCartVM.OrderHeader.PaymentStatus     = SD.PaymentStatusPending;
            ShoppingCartVM.OrderHeader.OrderStatus       = SD.StatusPending;
            ShoppingCartVM.OrderHeader.ApplicationUserId = Claim.Value;
            ShoppingCartVM.OrderHeader.OrderDate         = DateTime.Now;

            _unitOfWork.OrderHeader.Add(ShoppingCartVM.OrderHeader);
            _unitOfWork.Save();


            foreach (var item in ShoppingCartVM.ListCart)
            {
                item.Price = SD.GetPriceBasedOnQunatity(item.Count, item.Product.Price, item.Product.Price50, item.Product.Price100);

                OrderDetails orderDetails = new OrderDetails()
                {
                    ProductId = item.ProductId,
                    OrderId   = ShoppingCartVM.OrderHeader.Id,
                    Price     = item.Price,
                    Count     = item.Count
                };
                ShoppingCartVM.OrderHeader.OrderTotal += orderDetails.Count * orderDetails.Price;
                _unitOfWork.OrderDetails.Add(orderDetails);
            }
            _unitOfWork.ShoppingCart.RemoveRange(ShoppingCartVM.ListCart);
            _unitOfWork.Save();
            HttpContext.Session.SetInt32(SD.ssShoppingCart, 0);

            if (stripeToken == null)
            {
                //order will be created for delayed for authroized company
                ShoppingCartVM.OrderHeader.PaymentDueDate = DateTime.Now.AddDays(30);
                ShoppingCartVM.OrderHeader.PaymentStatus  = SD.PaymentStatusDelayedpayment;
                ShoppingCartVM.OrderHeader.OrderStatus    = SD.StatusApproved;
            }
            else
            {
                //process the payment
                var Options = new ChargeCreateOptions
                {
                    Amount      = Convert.ToInt32(ShoppingCartVM.OrderHeader.OrderTotal * 100),
                    Currency    = "usd",
                    Description = "order ID : " + ShoppingCartVM.OrderHeader.Id,
                    Source      = stripeToken
                };

                var Service = new ChargeService();

                Charge charge = Service.Create(Options);

                if (charge.Id == null)
                {
                    ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusRejected;
                }
                else
                {
                    ShoppingCartVM.OrderHeader.TransactionId = charge.Id;
                }
                if (charge.Status.ToLower() == "succeeded")
                {
                    ShoppingCartVM.OrderHeader.PaymentStatus = SD.PaymentStatusApproved;
                    ShoppingCartVM.OrderHeader.OrderStatus   = SD.StatusApproved;
                    ShoppingCartVM.OrderHeader.PaymentDate   = DateTime.Now;
                }
            }
            _unitOfWork.Save();
            return(RedirectToAction("OrderConfirmation", "Cart", new { id = ShoppingCartVM.OrderHeader.Id }));
        }