Exemplo n.º 1
0
        public string ProcessOrder(PaymentForm paymentForm)
        {
            var cart     = _shoppingCartService.GetCart();
            var customer = GetCustomer();

            var orderEntity = new Orders()
            {
                Username           = customer.Uname,
                OrderDate          = DateTime.Now,
                FirstName          = customer.BillingAddress.FirstName,
                LastName           = customer.BillingAddress.LastName,
                Address1           = customer.BillingAddress.Address1,
                Address2           = (!string.IsNullOrEmpty(customer.BillingAddress.Address2) ? customer.BillingAddress.Address2: string.Empty),
                City               = customer.BillingAddress.City,
                State              = customer.BillingAddress.State,
                PostalCode         = customer.BillingAddress.PostalCode,
                Country            = customer.BillingAddress.Country,
                ShippingFirstName  = customer.ShippingAddress.FirstName,
                ShippingLastName   = customer.ShippingAddress.LastName,
                ShippingAddress1   = customer.ShippingAddress.Address1,
                ShippingAddress2   = (!string.IsNullOrEmpty(customer.ShippingAddress.Address2) ? customer.ShippingAddress.Address2: string.Empty),
                ShippingCity       = customer.ShippingAddress.City,
                ShippingState      = customer.ShippingAddress.State,
                ShippingPostalCode = customer.ShippingAddress.PostalCode,
                ShippingCountry    = customer.ShippingAddress.Country,
                Phone              = customer.Phone,
                Email              = customer.Email,
                ShipVia            = paymentForm.Cart.ShipVia,
                Last4              = paymentForm.Payment.CreditCardNumber.Substring(12, 4),
                ExpMo              = Convert.ToInt32(paymentForm.Payment.ExpMonth),
                ExpYear            = Convert.ToInt32(paymentForm.Payment.ExpYear),
                Ccv          = Convert.ToInt32(paymentForm.Payment.SecurityCode),
                SubTotal     = paymentForm.Cart.CartTotal,
                Tax          = paymentForm.Cart.Tax,
                Shipping     = paymentForm.Cart.ShipCost,
                Total        = Decimal.Round(paymentForm.Cart.CartTotal + paymentForm.Cart.Tax + paymentForm.Cart.ShipCost, 3),
                OrderStatus  = "CC Authorized",
                PaymentDate  = DateTime.Now,
                ShippingDate = DateTime.Parse("1/1/1901 12:00:00 AM"),
                PaymentType  = "Credit Card",
                OrderNumber  = DateTime.Today.Year.ToString() + (DateTime.Today.Month.ToString().Length == 1 ? "0" + DateTime.Today.Month.ToString() : DateTime.Today.Month.ToString()) + (DateTime.Today.Day.ToString().Length == 1 ? "0" + DateTime.Today.Day.ToString() : DateTime.Today.Day.ToString()) + new Random().Next(999999).ToString(),
            };

            _orderRepository.AddOrder(orderEntity);

            foreach (var cartItem in cart.CartItems)
            {
                var orderDetailsEntity = new OrderDetails()
                {
                    OrderNumber = orderEntity.OrderNumber,
                    ProductId   = cartItem.ProductId,
                    Quantity    = cartItem.Quantity,
                    UnitPrice   = cartItem.Price,
                    ProductName = cartItem.ProductName,
                    SizeName    = cartItem.SizeName,
                    ColorName   = cartItem.ColorName,
                    Sku         = cartItem.Sku
                };
                _orderRepository.AddOrderDetail(orderDetailsEntity);
            }

            //delete shopping cart
            _shoppingCartService.DeleteCart();

            return(orderEntity.OrderNumber);
        }