Exemple #1
0
        public async Task <ActionResult> Create(CustomerViewModel model)
        {
            if (ModelState.IsValid)
            {
                Customer customer = Extensions.CustomerExtentions.ToCustomer(model);
                customer.UpdateDate = DateTime.Now.ToLongDateString();

                var result = await manager.CreateCustomerAsync(customer);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError("Customer is not created", result.Errors.First());
                return(View(model));
            }
            return(View(model));
        }
        public async Task <ActionResult> AddressAndPayment(FormCollection values)
        {
            var order = new Order();

            TryUpdateModel(order);
            try
            {
                if (!string.IsNullOrWhiteSpace(this.HttpContext.User.Identity.Name))
                {
                    order.UserName = User.Identity.Name;
                    order.Email    = User.Identity.Name;
                    order.SaveInfo = false;
                    //credit card info
                    order.Experation = DateTime.Now;
                    order.CcType     = "00000000000";
                    order.CreditCard = "Master";
                    var customer = customerManager.GetCustomers().SingleOrDefault(c => c.UserName == User.Identity.Name);
                    if (customer != null)
                    {
                        order.CustomerId = customer.Id;
                        order            = CreateOrder(order);
                        customer.Orders.Add(order);
                        await customerManager.UpdateCustomerAsync(customer);
                    }
                    else
                    {
                        customer = new Customer
                        {
                            FirstName   = order.FirstName,
                            LastName    = order.LastName,
                            Address     = order.Address,
                            Country     = order.Country,
                            City        = order.City,
                            PhoneNumber = order.Phone,
                            Region      = order.Region,
                            PostalCode  = order.PostalCode,
                            UserId      = User.Identity.GetUserId(),
                            UserName    = User.Identity.GetUserName()
                        };
                        await customerManager.CreateCustomerAsync(customer);

                        order.CustomerId = customer.Id;
                        order            = CreateOrder(order);
                        customer.Orders.Add(order);
                        await customerManager.UpdateCustomerAsync(customer);
                    }
                }
                else //Guest create customer and order
                {
                    if (order.SaveInfo == true)
                    {
                        Customer newCustomer = new Customer
                        {
                            FirstName   = order.FirstName,
                            LastName    = order.LastName,
                            Address     = order.Address,
                            Country     = order.Country,
                            City        = order.City,
                            PhoneNumber = order.Phone,
                            Region      = order.Region,
                            PostalCode  = order.PostalCode
                        };
                        await customerManager.CreateCustomerAsync(newCustomer);

                        order.CustomerId = customerManager.FindCustomerBy(c => c.LastName == newCustomer.LastName &&
                                                                          c.PhoneNumber == newCustomer.PhoneNumber).FirstOrDefault().Id;
                        order.Experation = DateTime.Now;
                        order.CcType     = "00000000000";
                        order.CreditCard = "Master";
                        order            = CreateOrder(order);
                        newCustomer.Orders.Add(order);
                        await customerManager.UpdateCustomerAsync(newCustomer);
                    }
                    else
                    {
                        order = CreateOrder(order);
                    }
                }
                // CheckoutController.SendOrderMessage(order.FirstName, "New Order: " + order.Id, order.ToString(order), appConfig.OrderEmail);

                return(RedirectToAction("Complete",
                                        new { id = order.Id }));
            }
            catch
            {
                //Invalid - redisplay with errors
                return(View(order));
            }
        }