Ejemplo n.º 1
0
 // GET: Cart
 public ActionResult Index(Cart cart,string returnUrl)
 {
     return View(new CartIndexViewModel { 
         ReturnUrl=returnUrl,
         Cart=cart
     });
 }
        public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (var smtpClient = new SmtpClient())
            {
                smtpClient.EnableSsl = emailSettings.UseSsl;
                smtpClient.Host = emailSettings.ServerName;
                smtpClient.Port = emailSettings.ServerPort;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(emailSettings.Username, emailSettings.Password);

                if (emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = emailSettings.FileLocation;
                    smtpClient.EnableSsl = false;
                }

                StringBuilder body = new StringBuilder()
                   .Append("A new order has been submitted")
                   .AppendLine("----")
                   .AppendLine("Items:");

                foreach (var line in cart.Lines)
                {
                    var subTotal = line.Fruit.Price * line.Quantity;
                    body.AppendFormat("{0}x{1} (subtotal:2:c)", line.Quantity,
                      line.Fruit.Price,
                      subTotal);
                }

                body.AppendFormat("Total order value:{0:c}", cart.ComputeTotalValue())
                    .AppendLine("----")
                    .AppendLine("Ship to:")
                    .AppendLine(shippingInfo.Name)
                    .AppendLine(shippingInfo.Line1 ?? "")
                    .AppendLine(shippingInfo.Line2 ?? "")
                    .AppendLine(shippingInfo.Line3 ?? "")
                    .AppendLine(shippingInfo.City ?? "")
                    .AppendLine(shippingInfo.State ?? "")
                    .AppendLine(shippingInfo.Country)
                    .AppendLine(shippingInfo.Zip)
                    .AppendLine("---")
                    .AppendFormat("Gift wrap:{0}", shippingInfo.GiftWrap ? "Yes" : "No");

                MailMessage mailMessage = new MailMessage(
                       emailSettings.MailFromAddress,
                       emailSettings.MailToAddress,
                       "New Order Submitted",
                       body.ToString());

                if (emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.ASCII;
                }

                /// how to send email correctly???

                smtpClient.Send(mailMessage);
            }
        }
Ejemplo n.º 3
0
 private Cart GetCart()
 {
     Cart cart = (Cart)Session["Cart"];
     if (cart==null)
     {
         cart = new Cart();
         Session["Cart"] = cart;
     }
     return cart;
 }
Ejemplo n.º 4
0
 public RedirectToRouteResult RemoveFromCart(Cart cart, int fruitId, string returnUrl)
 {
     Fruit fruit = null;
     using (FarmstoreEntities dbContext = new FarmstoreEntities())
     {
         fruit = dbContext.Fruits.SingleOrDefault(f => f.Id == fruitId);
     }
     if (fruit != null)
     {
         cart.RemoveLine(fruit);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
Ejemplo n.º 5
0
        public RedirectToRouteResult UpdateCart(Cart cart, int id, int quantity, string returnUrl)
        {
            Fruit fruit = null;
            using (FarmstoreEntities dbContext = new FarmstoreEntities())
            {
                fruit = dbContext.Fruits.SingleOrDefault(f => f.Id == id);

                if (quantity>0)
                {
                    cart.UpdateItem(fruit, quantity);
                }
                else 
                {
                    cart.RemoveLine(fruit);   
                }
            }
            
            return RedirectToAction("Index", new { returnUrl });
        }
Ejemplo n.º 6
0
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     //get the cart from the session
     Cart cart = null;
     if (controllerContext.HttpContext.Session != null)
     {
         cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     }
     //create the cart if there was not one in the session data
     if (cart == null)
     {
         cart = new Cart();
         if (controllerContext.HttpContext.Session != null)
         {
             controllerContext.HttpContext.Session[sessionKey] = cart;
         }
     }
     //return the cart
     return cart;
 }
Ejemplo n.º 7
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            
            if (cart.Lines.Count() == 0)
            {
                ModelState.AddModelError("", "Sorry, your cart is empty");
            }
            if (ModelState.IsValid)
            {
                //orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");

            }
            else
            {
                return View(shippingDetails);
            }
        }
Ejemplo n.º 8
0
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }