public async Task <IActionResult> MakeOrder(decimal price, string Ip, string returnurl = "") { if (returnurl == "") { var user = await GetCurrentUserAsync(); var userId = user?.Id; List <ShoppingCartItem> cartItems = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); var userAccount = await _context.Users.FindAsync(userId); var accessToken = await _payULogic.GetAccessTokenAsync(); var payUResponse = await _payULogic.GeneratePayLink(userAccount, price, cartItems, Ip, accessToken); var jsonPayU = JsonConvert.DeserializeObject <StatusModel>(payUResponse); var orderId = jsonPayU.orderId; var uri = jsonPayU.redirectUri; await SaveOrderToDatabase(orderId, cartItems, user, price); return(Redirect(uri)); } else { return(RedirectToAction("Index")); } }
public IActionResult Buy(int id, string size, int number) { if (SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart") == null) { List <ShoppingCartItem> cart = new List <ShoppingCartItem>(); var productModel = _context.Products.Where(x => x.Id == id).Include(x => x.Photos).First(); cart.Add(new ShoppingCartItem { Product = productModel, Quantity = number, Size = (SizeOfPruduct)Enum.Parse(typeof(SizeOfPruduct), size) }); SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } else { List <ShoppingCartItem> cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); int index = ifExist(id, size); if (index != -1) { cart[index].Quantity++; } else { var productModel = _context.Products.Where(x => x.Id == id).Include(x => x.Photos).First(); cart.Add(new ShoppingCartItem { Product = productModel, Quantity = number, Size = (SizeOfPruduct)Enum.Parse(typeof(SizeOfPruduct), size) }); } SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } return(RedirectToAction("Index")); }
public IActionResult Remove(int id) { List <ShoppingCartItem> cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); int index = ifExist(id); cart.RemoveAt(index); SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); return(RedirectToAction("Index")); }
public IActionResult Index() { var cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); if (cart != null) { ViewBag.cart = cart; ViewBag.total = cart.Sum(item => item.Product.Price * item.Quantity); } return(View()); }
private int ifExist(int id, string size = null) { List <ShoppingCartItem> cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); for (int i = 0; i < cart.Count; i++) { if (size != null) { if (cart[i].Size.Equals(size) && cart[i].Product.Id.Equals(id)) { return(i); } } else if (cart[i].Product.Id.Equals(id)) { return(i); } } return(-1); }