public ActionResult Index()
        {
            _propertyBag  = ExigoDAL.PropertyBags.Delete(PropertyBag);
            _shoppingCart = ExigoDAL.PropertyBags.Delete(ShoppingCart);

            return(View());
        }
        /// <summary>
        /// Creates a view model for shopping carts with the base class' properties populated.
        /// This is used to avoid having to populate the party and other common data for each shopping view model.
        /// </summary>
        /// <typeparam name="T">The type of view model needed.</typeparam>
        /// <param name="propertyBag">The cart's property bag.</param>
        /// <returns>Your pre-populated view model, ready for further population.</returns>
        public static T Create <T>(EnrollmentPropertyBag propertyBag) where T : IEnrollmentViewModel
        {
            var viewModel = Activator.CreateInstance <T>();

            viewModel.PropertyBag = propertyBag;

            return(viewModel);
        }
 public EnrollmentLogicProvider(Controller controller, ShoppingCartItemsPropertyBag cart, EnrollmentPropertyBag propertyBag)
 {
     Controller  = controller;
     Cart        = cart;
     PropertyBag = propertyBag;
 }
        public ActionResult PersonalInfo(EnrollmentPropertyBag propertyBag)
        {
            var requestForm = Request.Form;

            var birthDay = Convert.ToInt32(requestForm["Customer.BirthDay"]);
            var birthMonth = Convert.ToInt32(requestForm["Customer.BirthMonth"]);
            var birthYear = Convert.ToInt32(requestForm["Customer.BirthYear"]);
            DateTime birthDate = new DateTime(birthYear, birthMonth, birthDay);

            propertyBag.Customer.BirthDate = birthDate;
            PropertyBag.Customer = propertyBag.Customer;
            PropertyBag.Customer.MainAddress = propertyBag.ShippingAddress;
            PropertyBag.Customer.MailingAddress = propertyBag.ShippingAddress;
            PropertyBag.Customer.LoginName = propertyBag.Customer.Email;
            PropertyBag.Customer.PublicName = propertyBag.Customer.PublicName;

            // Set up our shipping address
            var shippingAddress = propertyBag.ShippingAddress;
            shippingAddress.FirstName = propertyBag.Customer.FirstName;
            shippingAddress.LastName = propertyBag.Customer.LastName;
            shippingAddress.Email = propertyBag.Customer.Email;
            shippingAddress.Phone = propertyBag.Customer.PrimaryPhone;

            PropertyBag.Customer.IsOptedIn = propertyBag.Customer.IsOptedIn;
            PropertyBag.ShippingAddress = propertyBag.ShippingAddress;

            Exigo.PropertyBags.Update(PropertyBag);
            return LogicProvider.GetNextAction();
        }
        public ActionResult SubmitCheckout()
        {
            if (!PropertyBag.IsSubmitting)
            {
                PropertyBag.IsSubmitting = true;
                _propertyBag             = ExigoDAL.PropertyBags.Update(PropertyBag);

                try
                {
                    // Start creating the API requests
                    var apiRequests = new List <ApiRequest>();

                    // Create the customer
                    var customerRequest = new CreateCustomerRequest(PropertyBag.Customer)
                    {
                        InsertEnrollerTree = true,
                        EnrollerID         = PropertyBag.EnrollerID,
                        CustomerType       = CustomerTypes.Distributor,
                        EntryDate          = DateTime.Now.ToCST(),
                        CustomerStatus     = CustomerStatuses.Active,
                        CanLogin           = true,
                        DefaultWarehouseID = OrderConfiguration.WarehouseID,
                        Notes = "Distributor was entered by Distributor #{0}. Created by the API Enrollment at ".FormatWith(Identity.Owner.CustomerID) + HttpContext.Request.Url.Host + HttpContext.Request.Url.LocalPath + " on " + DateTime.Now.ToCST().ToString("dddd, MMMM d, yyyy h:mmtt") + " CST at IP " + Common.GlobalUtilities.GetClientIP() + " using " + HttpContext.Request.Browser.Browser + " " + HttpContext.Request.Browser.Version + " (" + HttpContext.Request.Browser.Platform + ")."
                    };

                    ///IF Unilevel Place in Sponsor Tree Now, Binary will be placed post transactional envelope
                    if (!GlobalSettings.Exigo.UseBinary)
                    {
                        customerRequest.InsertUnilevelTree = true;
                        customerRequest.SponsorID          = customerRequest.EnrollerID;
                    }
                    apiRequests.Add(customerRequest);


                    // Set a few variables up for our shippping address, order/auto order items and the default auto order payment type
                    var shippingAddress      = PropertyBag.ShippingAddress;
                    var orderItems           = ShoppingCart.Items.Where(i => i.Type == ShoppingCartItemType.Order || i.Type == ShoppingCartItemType.EnrollmentPack).ToList();
                    var autoOrderItems       = ShoppingCart.Items.Where(i => i.Type == ShoppingCartItemType.AutoOrder || i.Type == ShoppingCartItemType.EnrollmentAutoOrderPack).ToList();
                    var autoOrderPaymentType = AutoOrderPaymentType.PrimaryCreditCard;

                    // Create initial order
                    var orderRequest = new CreateOrderRequest(OrderConfiguration, PropertyBag.ShipMethodID, orderItems, shippingAddress);

                    // Add order request now if we need to do any testing with the accepted functionality
                    apiRequests.Add(orderRequest);

                    if (PropertyBag.PaymentMethod.CanBeParsedAs <BankAccount>())
                    {
                        var bankAccount = PropertyBag.PaymentMethod.As <BankAccount>();
                        if (bankAccount.Type == ExigoService.BankAccountType.New)
                        {
                            apiRequests.Add(new DebitBankAccountRequest(bankAccount));
                        }
                        else
                        {
                            apiRequests.Add(new DebitBankAccountOnFileRequest(bankAccount));
                        }
                    }

                    // Create subscription autoorder if an autoorder has been chosen
                    if (autoOrderItems != null && autoOrderItems.Count() > 0)
                    {
                        var autoOrderRequest = new CreateAutoOrderRequest(AutoOrderConfiguration, autoOrderPaymentType, DateTime.Now.AddMonths(1).ToCST(), PropertyBag.ShipMethodID, autoOrderItems, shippingAddress);
                        autoOrderRequest.Frequency = FrequencyType.Monthly;
                        apiRequests.Add(autoOrderRequest);
                    }

                    // Create customer site
                    var customerSiteRequest = new SetCustomerSiteRequest(PropertyBag.Customer);
                    apiRequests.Add(customerSiteRequest);

                    // Add the new credit card to the customer's record and charge it for the current order
                    if (PropertyBag.PaymentMethod.CanBeParsedAs <CreditCard>())
                    {
                        var creditCard = PropertyBag.PaymentMethod.As <CreditCard>();

                        if (!creditCard.IsTestCreditCard && !Request.IsLocal)
                        {
                            var saveCCRequest = new SetAccountCreditCardTokenRequest(creditCard);
                            apiRequests.Add(saveCCRequest);

                            var chargeCCRequest = new ChargeCreditCardTokenRequest(creditCard);
                            apiRequests.Add(chargeCCRequest);
                        }
                        else
                        {
                            orderRequest.OrderStatus = GlobalUtilities.GetDefaultOrderStatusType();
                        }
                    }

                    // Process the transaction
                    var transaction = new TransactionalRequest {
                        TransactionRequests = apiRequests.ToArray()
                    };
                    var response = ExigoDAL.WebService().ProcessTransaction(transaction);

                    var newcustomerid  = 0;
                    var neworderid     = 0;
                    var newautoorderid = 0;

                    if (response.Result.Status == ResultStatus.Success)
                    {
                        foreach (var apiresponse in response.TransactionResponses)
                        {
                            if (apiresponse.CanBeParsedAs <CreateCustomerResponse>())
                            {
                                newcustomerid = apiresponse.As <CreateCustomerResponse>().CustomerID;
                            }
                            if (apiresponse.CanBeParsedAs <CreateOrderResponse>())
                            {
                                neworderid = apiresponse.As <CreateOrderResponse>().OrderID;
                            }
                            if (apiresponse.CanBeParsedAs <CreateAutoOrderResponse>())
                            {
                                newautoorderid = apiresponse.As <CreateAutoOrderResponse>().AutoOrderID;
                            }
                        }
                    }

                    ///If Binary Place Node Now
                    if (GlobalSettings.Exigo.UseBinary)
                    {
                        var placeNodeRequest = new PlaceBinaryNodeRequest
                        {
                            CustomerID    = newcustomerid,
                            ToParentID    = PropertyBag.EnrollerID,
                            PlacementType = BinaryPlacementType.EvenFill
                        };
                        ExigoDAL.WebService().PlaceBinaryNode(placeNodeRequest);
                    }

                    PropertyBag.NewCustomerID  = newcustomerid;
                    PropertyBag.NewOrderID     = neworderid;
                    PropertyBag.NewAutoOrderID = newautoorderid;
                    _propertyBag = ExigoDAL.PropertyBags.Update(PropertyBag);

                    // If the transaction was successful, then send the customer an email that will allow them to confirm thier opt in choice
                    if (PropertyBag.Customer.IsOptedIn)
                    {
                        ExigoDAL.SendEmailVerification(newcustomerid, PropertyBag.Customer.Email);
                    }

                    var token = Security.Encrypt(new
                    {
                        CustomerID  = newcustomerid,
                        OrderID     = neworderid,
                        AutoOrderID = newautoorderid
                    });

                    // Enrollment complete, now delete the Property Bag
                    ExigoDAL.PropertyBags.Delete(PropertyBag);
                    ExigoDAL.PropertyBags.Delete(ShoppingCart);

                    return(new JsonNetResult(new
                    {
                        success = true,
                        token = token
                    }));
                }
                catch (Exception exception)
                {
                    PropertyBag.OrderException = exception.Message;
                    PropertyBag.IsSubmitting   = false;
                    _propertyBag = ExigoDAL.PropertyBags.Update(PropertyBag);

                    return(new JsonNetResult(new
                    {
                        success = false,
                        message = exception.Message
                    }));
                }
            }
            else
            {
                if (PropertyBag.NewCustomerID > 0)
                {
                    var token = Security.Encrypt(new
                    {
                        CustomerID  = PropertyBag.NewCustomerID,
                        OrderID     = PropertyBag.NewOrderID,
                        AutoOrderID = PropertyBag.NewAutoOrderID
                    });

                    return(new JsonNetResult(new
                    {
                        success = true,
                        token = token
                    }));
                }
                else
                {
                    return(new JsonNetResult(new
                    {
                        success = false,
                        message = Resources.Common.YourOrderIsSubmitting
                    }));
                }
            }
        }
 public EnrollmentLogicProvider(Controller controller, ShoppingCartItemsPropertyBag cart, EnrollmentPropertyBag propertyBag)
 {
     Controller = controller;
     Cart = cart;
     PropertyBag = propertyBag;
 }