Пример #1
0
        public void ProcessOrder(Cart cart, ShippingDetails shippingInfo)
        {
            using (var smtpClient = new SmtpClient())
            {
                smtpClient.EnableSsl = this.emailSettings.UseSsl;
                smtpClient.Host = this.emailSettings.ServerName;
                smtpClient.Port = this.emailSettings.ServerPort;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(this.emailSettings.Username, this.emailSettings.Password);
                if (this.emailSettings.WriteAsFile)
                {
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
                    smtpClient.PickupDirectoryLocation = this.emailSettings.FileLocation;
                    smtpClient.EnableSsl = false;
                }

                StringBuilder body =
                    new StringBuilder().AppendLine("A new order has been submitted").AppendLine("---").AppendLine(
                        "Items:");
                foreach (CartLine line in cart.Lines)
                {
                    decimal subtotal = line.Product.Price * line.Quantity;
                    body.AppendFormat("{0} x {1} (subtotal: {2:c}", 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 ?? string.Empty).AppendLine(shippingInfo.Line3 ?? string.Empty).AppendLine(
                            shippingInfo.City).AppendLine(shippingInfo.State ?? string.Empty).AppendLine(
                                shippingInfo.Country).AppendLine(shippingInfo.Zip).AppendLine("---").AppendFormat(
                                    "Gift wrap: {0}", shippingInfo.GiftWrap ? "Yes" : "No");
                var mailMessage = new MailMessage(
                    this.emailSettings.MailFromAddress,
                    // From
                    this.emailSettings.MailToAddress,
                    // To
                    "New order submitted!",
                    // Subject
                    body.ToString()); // Body
                if (this.emailSettings.WriteAsFile)
                {
                    mailMessage.BodyEncoding = Encoding.ASCII;
                }

                smtpClient.Send(mailMessage);
            }
        }
Пример #2
0
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (cart.Lines.Count() == 0)
            {
                this.ModelState.AddModelError(string.Empty, "Sorry, your cart is empty!");
            }

            if (this.ModelState.IsValid)
            {
                this.orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return this.View("Completed");
            }
            else
            {
                return this.View(shippingDetails);
            }
        }
Пример #3
0
        public void CannotCheckoutEmptyCart()
        {
            // Arrange - create a mock order processor
            var mock = new Mock<IOrderProcessor>();

            // Arrange - create an empty cart
            var cart = new Cart();

            // Arrange - create shipping details
            var shippingDetails = new ShippingDetails();

            // Arrange - create an instance of the controller
            var target = new CartController(null, mock.Object);

            // Act
            ViewResult result = target.Checkout(cart, shippingDetails);

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());

            // Assert - check that the method is returning the default view
            Assert.AreEqual(string.Empty, result.ViewName);

            // Assert - check that we are passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }