public ActionResult CheckoutPost(CheckoutPageModel model)
        {
            var orders = checkoutprivate(model);
            ConfirmacaoPageModel confirmacaoPageModel = new ConfirmacaoPageModel();
            this.BindBasePageModel(confirmacaoPageModel);
            confirmacaoPageModel.Orders = orders;

            return View("Confirmacao", confirmacaoPageModel);
        }
        public ActionResult Checkout(string id)
        {
            //supondo "camisa-polo-estampa-verde" como Id,
            //buscar o id da pagina atraves do slug de produto, atribuir o produtoId ao viewBag e redirecionar para a view apropriada.
            //levando em consideracao o store
            //Descobre

            CheckoutPageModel pageModel = new CheckoutPageModel();

            this.BindBasePageModel(pageModel);

            string cartId = GetOrCreateCartId();
            pageModel.CartId = cartId;
            ShoppingCart cart = GetCart(cartId);
            pageModel.Cart = cart;

            Checkout checkout = new Checkout();
            pageModel.Checkout = checkout;

            pageModel.Checkout.CheckoutType = 1;

            if ((LoggedUser!=null) && (!string.IsNullOrEmpty(LoggedUser.UserId)))
            {
                pageModel.Checkout.User = customerAPIClient.GetLoggedCustomerById(LoggedUser.UserId);
                if (pageModel.Checkout.User.Addresses != null && pageModel.Checkout.User.Addresses.Count > 0)
                {
                    pageModel.Checkout.ShippingAddress = pageModel.Checkout.User.Addresses.Where(o => o.MainAddress).FirstOrDefault();
                }
                pageModel.Checkout.CheckoutType = Convert.ToInt32(CheckoutType.RegisteredUser);
                pageModel.Checkout.ReturningCustomer = true;
                pageModel.Checkout.CheckoutType = 0;
                pageModel.Checkout.AntiFraud = new AntiFraud();
                pageModel.Checkout.AntiFraud.DocumentNumber = pageModel.Checkout.User.DocumentNumber;
                pageModel.Checkout.AntiFraud.BirthDate = pageModel.Checkout.User.Birthdate;
            }
            else
            {

            }

            return View(pageModel);
        }
        private List<Order> checkoutprivate(CheckoutPageModel model)
        {
            var checkoutToPost = new Checkout();
            User newUser = null;

            checkoutToPost.CartId = GetOrCreateCartId();
            checkoutToPost.CheckoutType = model.Checkout.CheckoutType;
            checkoutToPost.SkipPaymentIntegration = model.Checkout.SkipPaymentIntegration;

            checkoutToPost.PaymentData = model.Checkout.PaymentData;
            if (model.Checkout.PaymentData != null && model.Checkout.PaymentData.CreditCard != null)
            {
                checkoutToPost.PaymentData.PaymentMethod = PaymentMethod.CREDIT_CARD;
            }

            if ((LoggedUser != null) && (!string.IsNullOrEmpty(LoggedUser.UserId)))
            {
                //Checkout e cria a conta
                if (model.Checkout.CheckoutType == 1)
                {
                    model.Checkout.ShippingAddress.RecipientName = model.Checkout.User.FirstName + " " + model.Checkout.User.LastName;

                    model.Checkout.User.Addresses = new List<Address>();
                    model.Checkout.User.Addresses.Add(model.Checkout.ShippingAddress);
                    model.Checkout.User.Addresses[0].MainAddress = true;
                    newUser = model.Checkout.User;

                    //hard para o ecommerce.
                    newUser.Username = newUser.Email;
                    var retUser = customerAPIClient.CreateCustomer(newUser);
                    newUser.Username = retUser.Username;
                    newUser.UserId = retUser.UserId;

                    checkoutToPost.User = newUser;

                    checkoutToPost.ShippingAddress = model.Checkout.ShippingAddress;
                }
                else
                {
                    //CHECKOUT ANONIMO
                    checkoutToPost.User = new VIKomet.SDK.Entities.UserManagement.User();
                    checkoutToPost.User.FirstName = model.Checkout.ShippingAddress.RecipientName;
                    checkoutToPost.User.Email = model.Checkout.AnonymousEmailAddress;
                    checkoutToPost.ShippingAddress = model.Checkout.ShippingAddress;
                    checkoutToPost.AnonymousEmailAddress = model.Checkout.AnonymousEmailAddress;
                }
            }
            else
            {

               VIKomet.SDK.Entities.UserManagement.User user = LoggedUser;

                checkoutToPost.User = user;
                checkoutToPost.User.PhoneNumber1 = model.Checkout.User.PhoneNumber1;
                checkoutToPost.User.DocumentNumber = model.Checkout.User.DocumentNumber;
                checkoutToPost.User.FormattedBirthdate = model.Checkout.User.FormattedBirthdate;

                checkoutToPost.ShippingAddress = model.Checkout.ShippingAddress;

                if (checkoutToPost.ShippingAddress != null && string.IsNullOrEmpty(checkoutToPost.ShippingAddress.RecipientName))
                {
                    checkoutToPost.ShippingAddress.RecipientName = user.FullName;
                }
            }

            checkoutToPost.AntiFraud = model.Checkout.AntiFraud;
            checkoutToPost.BillingAddress = model.Checkout.BillingAddress;

            //TODO: A tela deve tratar o checkbox do usuario usando o mesmo endereco para shipping e billing
            if (checkoutToPost.BillingAddress == null)
            {
                checkoutToPost.BillingAddress = checkoutToPost.ShippingAddress;
            }

            if ((LoggedUser!=null) && (!string.IsNullOrEmpty(LoggedUser.UserId)))
            {
                checkoutToPost.ReturningCustomer = true;
            }

            if ((LoggedUser != null) && (!string.IsNullOrEmpty(LoggedUser.UserId)))
            {
                //Checkout e cria a conta
                if (model.Checkout.CheckoutType == 1)
                {
                    string access_token = base.authorizationClient.GetPasswordToken(newUser, HttpContext.Application["AccountId"].ToString(), HttpContext.Application["SecretKey"].ToString());
                    this.LoggedUserAccessTokenInContext = access_token;
                    // FormsAuthentication.SetAuthCookie(access_token, true);
                }
            }

            var orders = base.orderManagementAPIClient.Purchase(checkoutToPost);

            return orders;
        }
        public ActionResult CheckoutPostAJAX(CheckoutPageModel model)
        {
            string ordersIds = "";

            try
            {
                var orders = checkoutprivate(model);

                foreach (var i in orders)
                {
                    ordersIds += (i.ItemId + ", ");
                }

                ordersIds = ordersIds.Substring(0, ordersIds.Length - 2);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return Json(new ControllerResponse() { ExceptionMessage = msg, HasErrors = true, RedirectUrl = "/checkout" });
            }

            return Json(new ControllerResponse() { ExceptionMessage = null, HasErrors = false, RedirectUrl = "/confirmacao?Orders=" + ordersIds });
        }