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;
                //    emailSettings.EnableSsl = false;
                //}

                StringBuilder body = new StringBuilder()
                    .AppendLine("A new order has been submitted")
                    .AppendLine("----")
                    .AppendLine("Items: ");
                foreach (var line in cart.Lines)
                {
                    var subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotal: {2:c})\n",
                        line.Quantity,
                        line.Product.Name,
                        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;
                //}
                smtpClient.Send(mailMessage);

            }
        }
 public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
 {
     Product product = _repository.Products.FirstOrDefault(p => p.ProductId == productId);
     if (product != null)
     {
         cart.RemoveLine(product);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
 public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
 {
     Product product = _repository.Products.FirstOrDefault(p => p.ProductId == productId);
     if (product != null)
     {
         cart.AddItem(product, 1);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
 public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
 {
     if (!cart.Lines.Any())
     {
         ModelState.AddModelError("", "Sorry, your cart is empty!");
     }
     if (!ModelState.IsValid) return View(shippingDetails);
     _orderProcessor.ProcessOrder(cart, shippingDetails);
     cart.Clear();
     return View("Completed");
 }
예제 #5
0
 private Cart GetCart()
 {
     Cart cart = (Cart)Session["Cart"];   // cart = new Cart(); if it is not null
     if (cart == null)
     {
         cart = new Cart();         //make a new cart object
         Session["Cart"] = cart;    //store the new cart into session, a Session could store any type of object
         //Session variables are stored on the web server by default, and are kept for a lifetime of a session
         //available across all pages, but only for a single session
         //like single-user global data
     }
     return cart;
 }
 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     Cart cart = null;
     if (controllerContext.HttpContext.Session != null)
     {
         cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     }
     if (cart != null) return cart;
     cart = new Cart();
     if (controllerContext.HttpContext.Session != null)
         controllerContext.HttpContext.Session[sessionKey] = cart;
     return 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);

                StringBuilder body = new StringBuilder()
                    .AppendLine("A new order has been submitted")
                    .AppendLine("---")
                    .AppendLine("Items:");
                foreach (var line in cart.lines)
                {
                    var subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotal: {2:c})\n",
                        line.Quantity,
                        line.Product.Name,
                        subtotal);
                }
                body.AppendFormat("Total order value: {0:c}",
                    cart.ComputeTotalValue())
                    .AppendLine("---")
                    .AppendLine("Ship to:")
                    .AppendLine(shippingInfo.Name)
                    .AppendLine(shippingInfo.Email)
                    .AppendLine(shippingInfo.Line1)
                    .AppendLine(shippingInfo.Line2 ?? "")
                    .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(new MailAddress(emailSettings.MailFromAddress).Address,
                    shippingInfo.Email,
                    "New order submitted!",
                    body.ToString());

                smtpClient.Send(mailMessage);
            }
        }
 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);
     }
 }
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
 public ViewResult Index(Cart cart, string returnUrl)
 {
     return View(new CartIndexViewModel { Cart = cart, ReturnUrl = returnUrl });
 }