public ViewResult Checkout(CART cart, SHIPPING_DETAILS shippingDetails) { if (cart.Lines.Count() == 0) { ModelState.AddModelError("", "Извините, ваша корзина пуста!"); } if (ModelState.IsValid) { orderProcessor.ProcessOrder(cart, shippingDetails); orderProcessor.SaveOrder(cart, shippingDetails); ViewBag.NameApp = this.repository.NameApp(); cart.Clear(); return(View("Completed")); } else { return(View(shippingDetails)); } }
public void Cannot_Checkout_Empty_Cart() { // Arrange - create a mock order processor Mock <IOrderProcessor> mock = new Mock <IOrderProcessor>(); // Arrange - create an empty cart CART cart = new CART(); // Arrange - create shipping details SHIPPING_DETAILS shippingDetails = new SHIPPING_DETAILS(); // Arrange - create an instance of the controller CartController 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 <SHIPPING_DETAILS>()), Times.Never()); // Assert - check that the method is returning the default view Assert.AreEqual("", result.ViewName); // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public void SaveOrder(CART cart, SHIPPING_DETAILS shippingInfo) { RS_ORDER ordr = new RS_ORDER(); ordr.Surname = shippingInfo.Surname; ordr.Name = shippingInfo.Name; ordr.Phone = shippingInfo.Phone; ordr.Email = shippingInfo.Email; ordr.Line1 = shippingInfo.Line1; ordr.Line2 = shippingInfo.Line2; ordr.Line3 = shippingInfo.Line3; ordr.City = shippingInfo.City; ordr.State = shippingInfo.State; ordr.Zip = shippingInfo.Zip; ordr.Country = shippingInfo.Country; ordr.GiftWrap = shippingInfo.GiftWrap; ordr.DateOrder = DateTime.Now; context.RS_ORDER.Add(ordr); context.SaveChanges(); long orderId = ordr.OrderId; if (orderId > 0) { foreach (CART_LINE crtLine in cart.Lines) { RS_ORDER_CONTENT orc = new RS_ORDER_CONTENT(); orc.OrderId = orderId; orc.ProductId = crtLine.Product.ProductId; orc.PriceId = crtLine.Product.PriceId; orc.Quantity = crtLine.Quantity; context.RS_ORDER_CONTENT.Add(orc); } context.SaveChanges(); } }
public void ProcessOrder(CART cart, SHIPPING_DETAILS 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() .AppendLine("Был отправлен новый заказ") .AppendLine("------------------------------------------------") .AppendLine("Предметы:"); foreach (var line in cart.Lines) { var subtotal = line.Product.PriceWithVat * line.Quantity; body.AppendFormat("{0} x {1} ({2}, {3}). Подитог: {4}; {5}", line.Quantity, line.Product.Name, line.Product.EquipmentName.ToLower(), line.Product.CategoryName.ToLower(), subtotal, Environment.NewLine); } body.AppendLine(""); body.AppendFormat("ОБЩАЯ СУММА: {0}", cart.ComputeTotalValue()); body.AppendLine("") .AppendLine("------------------------------------------------") .AppendLine("Отправить к:") .AppendLine(shippingInfo.Surname) .AppendLine(shippingInfo.Name) .AppendLine("Мобильный телефон:") .AppendLine(shippingInfo.Phone) .AppendLine("Электронная почта:") .AppendLine(shippingInfo.Email) .AppendLine(shippingInfo.Line1) .AppendLine(shippingInfo.Line2 ?? "") .AppendLine(shippingInfo.Line3 ?? "") .AppendLine(shippingInfo.City) .AppendLine(shippingInfo.State ?? "") .AppendLine(shippingInfo.Country) .AppendLine(shippingInfo.Zip) .AppendLine("------------------------------------------------") .AppendFormat("Упаковать в подарочную упаковку: {0}", shippingInfo.GiftWrap ? "да" : "нет"); MailMessage mailMessage = new MailMessage( emailSettings.MailFromAddress, // From emailSettings.MailToAddress, // To "Новый заказ!", // Subject body.ToString()); // Body if (emailSettings.WriteAsFile) { mailMessage.BodyEncoding = Encoding.Unicode; //Encoding.ASCII; } smtpClient.Send(mailMessage); } }