public IActionResult Index() { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); shoppingCartVM = new ShoppingCartVM() { orderHeader = new OrderHeader(), shoppingCarts = _unitOfWork.ShoppingCart.GetAll(s => s.ApplicationUserId == claim.Value, includeProperties: "Product") }; shoppingCartVM.orderHeader.OrderTotal = 0; shoppingCartVM.orderHeader.ApplicationUserId = claim.Value; shoppingCartVM.orderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(u => u.Id == claim.Value, includeProperties: "Company"); foreach (ShoppingCart cart in shoppingCartVM.shoppingCarts) { cart.Price = SD.GetPriceDependingOnQuantity(cart.Count, cart.Product.Price, cart.Product.Price50, cart.Product.Price100); shoppingCartVM.orderHeader.OrderTotal += (cart.Price * cart.Count); cart.Product.Description = SD.ConvertToRawHtml(cart.Product.Description ?? ""); if (cart.Product.Description.Length > 100) { cart.Product.Description = cart.Product.Description.Substring(0, 99) + "..."; } } return(View(shoppingCartVM)); }
public IActionResult Minus(int cartId) { var cartFromDb = _unitOfWork.ShoppingCart.GetFirstOrDefault(s => s.Id == cartId, includeProperties: "Product"); if (cartFromDb.Count == 1) { _unitOfWork.ShoppingCart.Remove(cartFromDb); var currentCount = HttpContext.Session.GetInt32(SD.Session_Cart_count) - 1; HttpContext.Session.SetInt32(SD.Session_Cart_count, currentCount ?? 0); _unitOfWork.Save(); if (currentCount == 0) { return(RedirectToAction(controllerName: "Home", actionName: "Index")); } } else { cartFromDb.Count -= 1; cartFromDb.Price = SD.GetPriceDependingOnQuantity(cartFromDb.Count, cartFromDb.Product.Price, cartFromDb.Product.Price50, cartFromDb.Product.Price100); _unitOfWork.Save(); } return(RedirectToAction(nameof(Index))); }
public IActionResult Summary() { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); shoppingCartVM = new ShoppingCartVM() { orderHeader = new OrderHeader(), shoppingCarts = _unitOfWork.ShoppingCart.GetAll(s => s.ApplicationUserId == claim.Value, includeProperties: "Product") }; shoppingCartVM.orderHeader.OrderTotal = 0; shoppingCartVM.orderHeader.ApplicationUserId = claim.Value; shoppingCartVM.orderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(u => u.Id == claim.Value, includeProperties: "Company"); foreach (ShoppingCart cart in shoppingCartVM.shoppingCarts) { cart.Price = SD.GetPriceDependingOnQuantity(cart.Count, cart.Product.Price, cart.Product.Price50, cart.Product.Price100); shoppingCartVM.orderHeader.OrderTotal += (cart.Price * cart.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 cartFromDb = _unitOfWork.ShoppingCart.GetFirstOrDefault(s => s.Id == cartId, includeProperties: "Product"); cartFromDb.Count += 1; cartFromDb.Price = SD.GetPriceDependingOnQuantity(cartFromDb.Count, cartFromDb.Product.Price, cartFromDb.Product.Price50, cartFromDb.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, includeProperties: "Company"); shoppingCartVM.shoppingCarts = _unitOfWork.ShoppingCart. GetAll(s => s.ApplicationUserId == claim.Value, includeProperties: "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.shoppingCarts) { item.Price = SD.GetPriceDependingOnQuantity(item.Count, item.Product.Price, item.Product.Price50, item.Product.Price100); OrderDetails orderDetails = new OrderDetails() { ProductId = item.ProductId, Cout = item.Count, Price = item.Price, OrderId = shoppingCartVM.orderHeader.Id, }; shoppingCartVM.orderHeader.OrderTotal += orderDetails.Cout * orderDetails.Price; _unitOfWork.OrderDetails.Add(orderDetails); } _unitOfWork.ShoppingCart.RemoveRange(shoppingCartVM.shoppingCarts); HttpContext.Session.SetInt32(SD.Session_Cart_count, 0); if (StripeToken == null) { shoppingCartVM.orderHeader.PaymentDueDate = DateTime.Now.AddDays(30); shoppingCartVM.orderHeader.PaymentStatus = SD.PaymentStatusDelayedPayment; shoppingCartVM.orderHeader.OrderStatus = SD.StatusApproved; } else { var options = new ChargeCreateOptions { Amount = Convert.ToInt32(shoppingCartVM.orderHeader.OrderTotal * 100), Currency = "EUR", Description = "Order ID : " + shoppingCartVM.orderHeader.Id, Source = StripeToken }; var service = new ChargeService(); Charge charge = service.Create(options); if (charge.BalanceTransactionId == null) { shoppingCartVM.orderHeader.PaymentStatus = SD.PaymentStatusRejected; } else { shoppingCartVM.orderHeader.TransactionId = charge.BalanceTransactionId; } 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 })); }