public ActionResult Register(RegisterModel model)
        {
            string message = "";

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                //if the registration process was successful,
                //add the newly registered user to your Customers and subscribe him to the Selected Plan
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    try
                    {
                        //get your Site data through Billing Gateway
                        var mySite = _dynabicBillingGateway.Sites.GetSiteBySubdomain(Config.MySiteSubdomain);

                        //get the selected plan
                        var selectedPlan = _dynabicBillingGateway.Products.GetProductById(model.Plans.SelectedPlan.ToString());

                        //generate a random Customer Reference Id
                        //CustomerReferenceId does not permit the usage of any other special characters except "-" and
                        //it isn't allowed to begin or end with this character nor it can be consecutive
                        Random r = new Random();
                        long value = (long)((r.NextDouble() * 2.0 - 1.0) * long.MaxValue);
                        string newCustomerReferenceId = string.Format("{0}-{1}", mySite.Id, Math.Abs(value));

                        //create a new CustomerRequest
                        CustomerRequest newCustomer = new CustomerRequest()
                        {
                            //this fields are required
                            FirstName = model.UserName,
                            LastName = model.UserName,
                            Email = model.Email,
                            ReferenceId = newCustomerReferenceId
                        };

                        //create a new Subscription Request
                        SubscriptionRequest newSubscription = new SubscriptionRequest()
                        {
                            Customer = newCustomer,
                            ProductId = selectedPlan.Id,
                            ProductPricingPlanId = selectedPlan.PricingPlans[0].Id,
                        };

                        //if the Credit Card is required at Signup 
                        //create a new Credit Card Request and add it to your Subscription
                        //isCreditCardAtSignupRequired may be "No", "Yes", "YesOptional"
                        if (selectedPlan.isCreditCardAtSignupRequired != BoolOptional.No)
                        {
                            CreditCardRequest newCreditCard = new CreditCardRequest()
                            {
                                Cvv = model.CreditCard.Cvv,
                                ExpirationDate = model.CreditCard.ExpirationDate,
                                FirstNameOnCard = model.CreditCard.FirstNameOnCard,
                                LastNameOnCard = model.CreditCard.LastNameOnCard,
                                Number = model.CreditCard.Number
                            };

                            newSubscription.CreditCard = newCreditCard;
                        }
                        //if the Billing Address is required at Signup 
                        //create a new Billing Address Request and add it to your Subscription
                        //isBillingAddressAtSignupRequired may be "No", "Yes", "YesOptional"
                        if (selectedPlan.isBillingAddressAtSignupRequired != BoolOptional.No)
                        {
                            AddressRequest billingAddress = new AddressRequest()
                            {
                                Address1 = model.BillingAddress.BillingAddress1,
                                City = model.BillingAddress.BillingCity,
                                Country = model.BillingAddress.BillingCountry,
                                StateProvince = model.BillingAddress.BillingProvince,
                                ZipPostalCode = model.BillingAddress.BillingZipPostalCode,
                                FirstName = model.BillingAddress.BillingFirstName,
                                LastName = model.BillingAddress.BillingLastName
                            };
                            newSubscription.BillingAddress = billingAddress;
                        }

                        //subscribe the newly created Customer to selected Plan
                        _dynabicBillingGateway.Subscription.AddSubscription(mySite.Subdomain, newSubscription);

                        //create a User Profile and save some data that we may need later
                        ProfileCommon profile = ProfileCommon.GetUserProfile(model.UserName);
                        profile.CurrentPlanId = model.Plans.SelectedPlan;
                        profile.CustomerReferenceId = newCustomerReferenceId;

                        //redirect to Home page 
                        return RedirectToAction("Index", "Home");
                    }
                    catch (Exception)
                    {
                        message = "Something went wrong. Please try again later.";
                    }
                }
                else
                {
                    message = ErrorCodeToString(createStatus);
                }
            }
            //provide an error message in case something went wrong
            model.PageMessage = new PageMessageModel
            {
                Type = PageMessageModel.MessageType.Error,
                Message = message
            };

            model.Plans.MyPlans = model.Plans.GetAllPlans(_dynabicBillingGateway);
            // If we got this far, something failed, redisplay form
            return View(model);
        }
 /// <summary>
 /// Updates a Subscription
 /// </summary>
 /// <param name="siteSubdomain">The subdomain of the Site</param>
 /// <param name="subscriptionId">The subscription id.</param>
 /// <param name="newSubscription">The Subscription to be updated, as a SubscriptionRequest object</param>
 /// <param name="format">The format of the Response</param>
 /// <returns>
 /// A SubscriptionResponse object that corresponds to the newly-updated Subscription
 /// </returns>
 public SubscriptionResponse UpdateSubscription(string siteSubdomain, string subscriptionId, SubscriptionRequest newSubscription, string format = ContentFormat.XML)
 {
     return _service.Put<SubscriptionRequest, SubscriptionResponse>(string.Format("{0}/{1}/{2}.{3}", _gatewayURL, siteSubdomain, subscriptionId, format), newSubscription);
 }
 /// <summary>
 /// Adds a new Subscription
 /// </summary>
 /// <param name="siteSubdomain"> The subdomain of the Site </param>
 /// <param name="newSubscription"> The new Subscription to be added, as a SubscriptionRequest object </param>
 /// <param name="format"> The format of the Response </param>
 /// <returns> A SubscriptionResponse object corresponding to the newly-inserted Subscription </returns>
 public SubscriptionResponse AddSubscription(string siteSubdomain, SubscriptionRequest newSubscription, string format = ContentFormat.XML)
 {
     return _service.Post<SubscriptionRequest, SubscriptionResponse>(string.Format("{0}/{1}.{2}", _gatewayURL, siteSubdomain, format), newSubscription);
 }
        private SubscriptionResponse AddTestSubscription(SiteResponse site)
        {
            var product = AddProduct(site.Id);

            var subscriptionItems = new SubscriptionItemRequestList();
            foreach (var item in product.PricingPlans[0].ProductItemsList)
            {
                subscriptionItems.Add(new SubscriptionItemRequest
                {
                    ProductItemId = item.Id,
                    Quantity = 5,
                    UpdateDescription = "Add new subscription",
                });
            }

            var subscriptionRequest = new SubscriptionRequest()
            {
                BillingAddress = CreateDafaultAddress(),
                CreditCard = CreateDefaultCreditCard(),
                Currency = CreateDefaultCurrency(),
                CurrentBallance = 100,
                Customer = CreateDefaultCustomer(),
                NextAssesment = DateTime.Now.AddDays(31),
                NextProduct = null,
                NextProductPricingPlan = null,
                ProductId = product.Id,
                ProductPricingPlanId = product.PricingPlans[0].Id,
                StartDate = DateTime.Now,
                SubscriptionItems = subscriptionItems,
            };
            return _gateway.Subscription.AddSubscription(site.Subdomain, subscriptionRequest);
        }