public IActionResult Index() { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); ShoppingCartVM = new ShoppingCartVM { OrderHeader = new OrderHeader(), ListCart = _unitOfWork.ShoppingCart.GetAll(sc => sc.ApplicationUserId == claim.Value, includeProperties: "Product") }; ShoppingCartVM.OrderHeader.OrderTotal = 0; ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser.GetFirstOrDefault(au => au.Id == claim.Value, includeProperties: "Company"); foreach (var list in ShoppingCartVM.ListCart) { list.Price = SD.GetPriceBaseOnQuantity(list.Count, list.Product.Price, list.Product.Price50, list.Product.Price100); ShoppingCartVM.OrderHeader.OrderTotal += (list.Price * list.Price); 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(x => x.ApplicationUserId == claim.Value, includeProperties: "Product") }; ShoppingCartVM.OrderHeader.ApplicationUser = _unitOfWork.ApplicationUser .GetFirstOrDefault(x => x.Id == claim.Value, includeProperties: "Company"); foreach (var list in ShoppingCartVM.ListCart) { list.Price = SD.GetPriceBaseOnQuantity(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(c => c.Id == cartId, includeProperties: "Product"); cart.Count += 1; cart.Price = SD.GetPriceBaseOnQuantity(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(c => c.Id == cartId, includeProperties: "Product"); if (cart.Count == 1) { _unitOfWork.ShoppingCart.Remove(cart); _unitOfWork.Save(); var count = _unitOfWork.ShoppingCart.GetAll(u => u.ApplicationUserId == cart.ApplicationUserId).Count(); HttpContext.Session.SetInt32(SD.ssShoppingCart, count); } else { cart.Count -= 1; cart.Price = SD.GetPriceBaseOnQuantity(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(x => x.Id == claim.Value, includeProperties: "Company"); ShoppingCartVM.ListCart = _unitOfWork.ShoppingCart.GetAll(x => x.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.ListCart) { item.Price = SD.GetPriceBaseOnQuantity(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) { 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.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 })); }