//validate cart in case of invalid return to check cart again or pay
        //in case of error make order cancel
        public async Task <IActionResult> CheckOut()
        {
            int initCount = cartServices.GetCartList(UserName).Count;
            int resCount  = orderServices.ValidatingCartBeforAddingToCart(UserName);

            if (initCount != resCount)
            {
                return(RedirectToAction(nameof(Index), "Cart"));
            }

            var order = orderServices.GetOrder(UserName);
            int count = orderServices.AddProductToCart(UserName);


            var totalPrice = orderServices.GetOrder(UserName)?.AmountBuy;

            if (!totalPrice.HasValue)
            {
                return(View("Error"));
            }
            if (initCount != orderServices.GetOrder(UserName, null, true).OrderDetails.Count)
            {
                if (orderServices.CancelingOpenOrder(UserName))
                {
                    return(View("Error", new ErrorViewModel()
                    {
                        RequestId = "Something wrong"
                    }));
                }

                return(View("Error"));
            }

            var payment = await new Payment(totalPrice.Value)
                          .PaymentRequest("متن تست موقع خرید",
                                          Url.Action(nameof(CheckOutCallback), "Orders", new { amount = totalPrice }, Request.Scheme));

            if (payment.Status == 100)
            {
                if (orderServices.AddAuthorityToOrder(UserName, payment.Authority))
                {
                    return((IActionResult)Redirect(payment.Link));
                }
                orderServices.CancelingOpenOrder(UserName);
                return(BadRequest("خطا در پرداخت"));
            }
            else
            {
                orderServices.CancelingOpenOrder(UserName);
                return(BadRequest($"خطا در پرداخت. کد خطا:{payment.Status}"));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Index()
        {
            string tempCartId             = httpContextAccessor.HttpContext.Request.Cookies["tempCartId"];
            List <TempCartItem> tempCarts = cartServices.GetTempCartList(tempCartId);

            if (tempCarts != null)
            {
                foreach (TempCartItem item in tempCarts)
                {
                    cartServices.addToCart(UserName, item.Product.Id, item.Quantity);
                }
                await cartServices.DeleteTempCart(tempCartId);
            }
            List <CartItem>   cart      = cartServices.GetCartList(UserName);
            CartListViewModel viewModel = new CartListViewModel();

            if (cart != null)
            {
                viewModel.CartItems = cart
                                      .Select(x => new CartViewModel()
                {
                    Id           = x.Id,
                    ImageAddress = x.Product.PictureAddress,
                    Price        = x.Product.Price,
                    Quantity     = x.Quantity,
                    Title        = x.Product.Title
                }).ToList();
                viewModel.TotalPrice = cart.Sum(x => x.Quantity * x.Product.Price);
            }
            else
            {
                viewModel = new CartListViewModel()
                {
                    CartItems  = new List <CartViewModel>(),
                    TotalPrice = 0
                };
            }
            return(View(viewModel));
        }
        public IActionResult GetProductOfOpenCart()
        {
            CartListViewModel result = new CartListViewModel();

            if (UserName == null || UserName == "")
            {
                string tempCartId = httpContextAccessor.HttpContext.Request.Cookies["tempCartId"];

                var cart = cartServices.GetTempCartList(tempCartId);
                if (cart != null)
                {
                    result.CartItems =
                        cart.Where(c => !c.DeletedDate.HasValue &&
                                   !c.Product.DisableDate.HasValue &&
                                   !c.Product.RemoveDate.HasValue)
                        .Select(x => new CartViewModel()
                    {
                        Id           = x.Id,
                        ImageAddress = x.Product.PictureAddress,
                        Price        = x.Product.Price,
                        Quantity     = x.Quantity,
                        Title        = x.Product.Title
                    }).ToList();
                    result.TotalPrice = result.CartItems.Sum(x => x.Price * x.Quantity);
                }
                else
                {
                    result = new CartListViewModel()
                    {
                        CartItems  = new List <CartViewModel>(),
                        TotalPrice = 0
                    };
                }
            }
            else
            {
                var cart = cartServices.GetCartList(UserName);


                if (cart != null)
                {
                    result.CartItems =
                        cart.Where(c => !c.DeletedDate.HasValue &&
                                   !c.RemoveDate.HasValue &&
                                   !c.Product.DisableDate.HasValue &&
                                   !c.Product.RemoveDate.HasValue)
                        .Select(x => new CartViewModel()
                    {
                        Id           = x.Id,
                        ImageAddress = x.Product.PictureAddress,
                        Price        = x.Product.Price,
                        Quantity     = x.Quantity,
                        Title        = x.Product.Title
                    }).ToList();
                    result.TotalPrice = result.CartItems.Sum(x => x.Price * x.Quantity);
                }
                else
                {
                    result = new CartListViewModel()
                    {
                        CartItems  = new List <CartViewModel>(),
                        TotalPrice = 0
                    };
                }
            }
            return(Json(result));
        }