public static void DeleteCustomerCreditCard(int customerID, CreditCardType type)
        {
            // If this is a new credit card, don't delete it - we have nothing to delete
            if (type == CreditCardType.New) return;

            // Save the a blank copy of the credit card
            // Passing a blank token will do the trick
            var request = new SetAccountCreditCardTokenRequest
            {
                CustomerID = customerID,

                CreditCardAccountType = (type == CreditCardType.Primary) ? AccountCreditCardType.Primary : AccountCreditCardType.Secondary,
                CreditCardToken = string.Empty,
                ExpirationMonth = 1,
                ExpirationYear = DateTime.Now.Year + 1,

                BillingName = string.Empty,
                BillingAddress = string.Empty,
                BillingCity = string.Empty,
                BillingState = string.Empty,
                BillingZip = string.Empty,
                BillingCountry = string.Empty
            };
            var response = Exigo.WebService().SetAccountCreditCardToken(request);
        }
        public ActionResult SubmitEnrollment()
        {
            var isRedirectPayment = PropertyBag.IsRedirectPayment;

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

                // Create the customer
                var customerRequest = new CreateCustomerRequest(PropertyBag.Customer);
                customerRequest.InsertEnrollerTree = true;
                customerRequest.InsertUnilevelTree = true;
                customerRequest.CustomerType = CustomerTypes.BrandPartner;
                customerRequest.EnrollerID = Identity.Owner.CustomerID;
                customerRequest.SponsorID = Identity.Owner.CustomerID;
                customerRequest.EntryDate = DateTime.Now;
                customerRequest.CustomerStatus = CustomerStatuses.Active;
                customerRequest.CanLogin = true;
                customerRequest.LoginName = PropertyBag.Customer.LoginName;
                customerRequest.Company = PropertyBag.Customer.PublicName;
                customerRequest.CurrencyCode = OrderConfiguration.CurrencyCode;
                customerRequest.PayableType = PayableType.Check;
                customerRequest.LoginPassword = PropertyBag.Customer.Password;
                customerRequest.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.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 + ").";
                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;
                shippingAddress.FirstName = PropertyBag.Customer.FirstName;
                shippingAddress.LastName = PropertyBag.Customer.LastName;
                shippingAddress.Phone = PropertyBag.Customer.PrimaryPhone;
                shippingAddress.Email = PropertyBag.Customer.Email;

                var orderItems = ShoppingCart.Items.Where(i => i.Type == ShoppingCartItemType.EnrollmentPack).ToList();

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

                if (isRedirectPayment)
                {
                    orderRequest.OrderStatus = OrderStatusType.Pending;
                }

                // 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 we are dealing with a test credit card, then we set the order as accepted to simulate an 'Accepted' order
                    if (!creditCard.IsTestCreditCard)
                    {
                        var chargeCCRequest = new ChargeCreditCardTokenRequest(creditCard);
                        apiRequests.Add(chargeCCRequest);

                        var saveCCRequest = new SetAccountCreditCardTokenRequest(creditCard);
                        apiRequests.Add(saveCCRequest);
                    }
                    else
                    {
                        orderRequest.OrderStatus = OrderStatusType.Shipped;
                    }
                }

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

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

                var newcustomerid = 0;
                var neworderid = 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;
                    }
                }

                // Update the customer web alias
                var propertyBagCustomer = PropertyBag.Customer;
                Task.Factory.StartNew(() =>
                {
                    var customerSiteRequest = new SetCustomerSiteRequest(propertyBagCustomer);
                    customerSiteRequest.CustomerID = newcustomerid;
                    customerSiteRequest.WebAlias = newcustomerid.ToString();
                    customerSiteRequest.FirstName = propertyBagCustomer.FirstName;
                    customerSiteRequest.LastName = propertyBagCustomer.LastName;
                    customerSiteRequest.Company = propertyBagCustomer.PublicName;
                    SetCustomerSiteResponse res = Exigo.WebService().SetCustomerSite(customerSiteRequest);
                });

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

                if (PropertyBag.Customer.IsOptedIn) {
                    Exigo.SendEmailVerification(newcustomerid, propertyBagCustomer.Email);

                }

                var selectedCountry = PropertyBag.ShippingAddress.Country;

                // handle redirect payments
                if (isRedirectPayment)
                {
                    var paymentProvider = PaymentService.GetPaymentProvider(selectedCountry);
                    var order = Exigo.GetCustomerOrders(new GetCustomerOrdersRequest()
                    {
                        CustomerID = newcustomerid,
                        OrderID = neworderid,
                        IncludeOrderDetails = true
                    }).FirstOrDefault();

                    if (paymentProvider.HandlerType == PaymentHandlerType.Remote)
                    {
                        paymentProvider.OrderConfiguration = OrderConfiguration;
                        paymentProvider.Order = order;
                        paymentProvider.Order.ShipMethodID = PropertyBag.ShipMethodID;

                    }

                    var billingAddress = new Address()
                    {
                        AddressType = AddressType.Other,
                        Address1 = order.Recipient.Address1,
                        Address2 = order.Recipient.Address2,
                        City = order.Recipient.City,
                        State = order.Recipient.State,
                        Zip = order.Recipient.Zip,
                        Country = order.Recipient.Country
                    };

                    // Get the request data
                    var paymentRequest = paymentProvider.GetPaymentRequest(new PaymentRequestArgs() { ReturnUrl = PaymentRedirectURL, BillingName = order.Recipient.FullName, BillingAddress = billingAddress, WebAlias = Identity.Owner.WebAlias });

                    // Handle the request
                    var postPaymentRequest = paymentRequest as POSTPaymentRequest;
                    if (postPaymentRequest != null)
                    {
                        return new JsonNetResult(new
                        {
                            success = true,
                            redirectForm = postPaymentRequest.RequestForm
                        });
                    }
                    else
                    {
                        return new JsonNetResult(new
                        {
                            success = false,
                        });
                    }

                    //if (paymentProvider.HandlerType == PaymentHandlerType.Remote)
                    //{
                    //    //Exigo.PropertyBags.Delete(PropertyBag);
                    //    Exigo.PropertyBags.Delete(ShoppingCart);

                    //    paymentProvider.OrderConfiguration = OrderConfiguration;
                    //    paymentProvider.Order = order;
                    //    paymentProvider.Order.ShipMethodID = PropertyBag.ShipMethodID;

                    //    // Get the request data
                    //    var paymentRequest = paymentProvider.GetPaymentRequest(new PaymentRequestArgs() { ReturnUrl = PaymentRedirectURL, WebAlias = Identity.Owner.WebAlias, BillingAddress = PropertyBag.Customer.MainAddress });

                    //    // Handle the request
                    //    var postPaymentRequest = paymentRequest as POSTPaymentRequest;
                    //    if (postPaymentRequest != null)
                    //    {
                    //        Exigo.PropertyBags.Delete(PropertyBag);

                    //        return new JsonNetResult(new
                    //        {
                    //            success = true,
                    //            redirectForm = postPaymentRequest.RequestForm
                    //        });
                    //    }
                    //}

                    //    return new JsonNetResult(new
                    //    {
                    //    success = false,
                    //    message = "redirect failed"
                    //});
                }

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

                return new JsonNetResult(new { token = token, success = true });
            }
            catch (Exception ex)
            {
                return new JsonNetResult(new { message = ex.Message, success = false });
            }
        }
        public static CreditCard SetCustomerCreditCard(int customerID, CreditCard card, CreditCardType type)
        {
            // New credit cards
            if (type == CreditCardType.New)
            {
                return SaveNewCustomerCreditCard(customerID, card);
            }

            // Validate that we have a token
            var token = card.GetToken();
            if (token.IsNullOrEmpty()) return card;

            // Save the credit card
            var request = new SetAccountCreditCardTokenRequest
            {
                CustomerID = customerID,

                CreditCardAccountType = (card.Type == CreditCardType.Primary) ? AccountCreditCardType.Primary : AccountCreditCardType.Secondary,
                CreditCardToken = token,
                ExpirationMonth = card.ExpirationMonth,
                ExpirationYear = card.ExpirationYear,

                BillingName = card.NameOnCard,
                BillingAddress = card.BillingAddress.AddressDisplay,
                BillingCity = card.BillingAddress.City,
                BillingState = card.BillingAddress.State,
                BillingZip = card.BillingAddress.Zip,
                BillingCountry = card.BillingAddress.Country
            };
            var response = Exigo.WebService().SetAccountCreditCardToken(request);

            return card;
        }