public void Calculate_Cart_Total()
        {
            // Arrange - create some test products
            Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            // Arrange - create a new cart
            Cart target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 3);
            decimal result = target.ComputeTotalValue();

            // Assert
            Assert.AreEqual(result, 450M);
        }
        public int StoreOrderAndOrderItems(Cart cart)
        {
            Order order = new Order();
            //order.OrderID
            order.OrderDate = DateTime.Now;
            order.ShipDate = DateTime.Now;
            order.TotalOrder = cart.ComputeTotalValue();
            order.ProductCount = cart.ComputeNumItems();
            order.FirstName = string.Empty;
            order.LastName = string.Empty;
            order.Company = string.Empty;
            order.Email = string.Empty;
            order.StreetLine1 = string.Empty;
            order.StreetLine2 = string.Empty;
            order.StreetLine3 = string.Empty;
            order.City = string.Empty;
            order.PostalCode = string.Empty;
            order.County = string.Empty;
            order.Country = string.Empty;
            order.PaymentConfirmation = string.Empty;
            order.CreatedAt = DateTime.Now;
            order.UpdatedAt = DateTime.Now;
            order.BillToAddressID = 1;
            order.ShipToAddressID = 1;
            order.CustomerID = 1;

            orderRepository.SaveOrder(order);

            foreach (var line in cart.Lines)
            {
                OrderItem orderItem = new OrderItem();

                Product product = repository.Products
                 .FirstOrDefault(p => p.ProductID == line.Product.ProductID);

                //orderItem.OrderItemID
                orderItem.Name = line.Product.Name;
                orderItem.Description= line.Product.Description;
                orderItem.Price = line.Product.Price;
                orderItem.Category = line.Product.Category;
                orderItem.Special = line.Product.Special;
                orderItem.ImageData = line.Product.ImageData;
                orderItem.ImageMimeType = line.Product.ImageMimeType;
                orderItem.Seller = line.Product.Seller;
                orderItem.Buyer = line.Product.Buyer;
                orderItem.Quantity = line.Quantity;
                orderItem.OrderID = order.OrderID;
                orderItem.ProductID = product.ProductID;
                orderItem.CategoryID = product.CategoryID;
                orderItemRepository.SaveOrderItem(orderItem);

            }

            return order.OrderID;
        }
        // PayPal
        public ActionResult CheckoutWithPayPal(Cart cart)
        {
            if (cart.Lines.Count() == 0)
            {
                ModelState.AddModelError("", "Sorry, your cart is empty!");
                return View("Completed");
            }
            else
            {

                int orderID = StoreOrderAndOrderItems(cart);
                UserSessionData.OrderID = orderID;

                NVPAPICaller payPalCaller = new NVPAPICaller();
                payPalCaller.SetCredentials();

                string retMsg = "";
                string token = "";
                decimal amtVal = cart.ComputeTotalValue();
                string amt = amtVal.ToString();
                bool ret = payPalCaller.ShortcutExpressCheckout(cart, amt, ref token, ref retMsg);
                if (ret)
                {

                    UserSessionData.Token = token;
                    UserSessionData.PaymentAmount = amtVal;

                    return Redirect(retMsg);
                }
                else
                {
                    return View("Completed");
                }
            }
        }
        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()
                    .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}", 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(shippingInfo.PostalCode ?? "")
                    .AppendLine("---")
                    .AppendFormat("Gift wrap: {0}",
                        shippingInfo.GiftWrap ? "Yes" : "No");

                MailMessage mailMessage = new MailMessage(
                                       emailSettings.MailFromAddress,   // From
                                       emailSettings.MailToAddress,     // To
                                       "New order submitted!",          // Subject
                                       body.ToString());                // Body

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

                smtpClient.Send(mailMessage);
            }
        }