public ActionResult ChangePlan(ChangePlanModel model)
        {
            model.PageMessage = new PageMessageModel();
            if (ModelState.IsValid)
            {
                try
                {
                    ProfileCommon profile = ProfileCommon.GetUserProfile(User.Identity.Name);
                    //get all the subscriptions of a Customer and 
                    //select the current Subscription
                    var subscriptions = _dynabicBillingGateway.Subscription.GetSubscriptionsOfCustomerByReferenceId(Config.MySiteSubdomain, profile.CustomerReferenceId.ToString());
                    var subscription = subscriptions.Where(o => o.ProductId == profile.CurrentPlanId).FirstOrDefault();

                    // get the pricing plan of the selected product
                    var product = _dynabicBillingGateway.Products.GetProductById(model.Plans.SelectedPlan.ToString());
                    if (product == null || product.PricingPlans == null || product.PricingPlans.Count == 0)
                    {
                        throw new ApplicationException("Product or product's pricing plan is not found");
                    }

                    // upgrade the subscription
                    _dynabicBillingGateway.Subscription.UpgradeDowngradeSubscriptionProduct(subscription.Id.ToString(), product.PricingPlans[0].Id.ToString(), "false", "false");

                    //update the value in Profile with the new value
                    profile.CurrentPlanId = model.Plans.SelectedPlan;

                    //set the Success message
                    TempData["PageMessage"] = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Success,
                        Message = "Your subscription has been successfuly updated."
                    };

                    return RedirectToAction("ChangePlan", "Account");
                }
                catch
                {
                    //provide an error message in case something went wrong
                    model.PageMessage = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Error,
                        Message = "Something went wrong. Please try again later."
                    };
                }
            }
            model.Plans.MyPlans = model.Plans.GetAllPlans(_dynabicBillingGateway);
            if (model.PageMessage == null)
                model.PageMessage = new PageMessageModel();
            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult AccountInfo(AccountInfoModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //get the currently logged in user
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);

                    //if it is not the same Email as before
                    if (!currentUser.Email.Equals(model.Email))
                    {
                        //check to see if there is another user with the same email address 
                        //and throw an exception in case it does
                        var userName = Membership.GetUserNameByEmail(model.Email);
                        if (!String.IsNullOrEmpty(userName) && !currentUser.UserName.Equals(userName))
                            throw new ApplicationException("There's another user with the same Email address. Please provide a different Email address.");

                        //set the Email and update the user
                        currentUser.Email = model.Email;
                        Membership.UpdateUser(currentUser);
                    }

                    //set the Success message
                    TempData["PageMessage"] = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Success,
                        Message = "User " + User.Identity.Name + " has been updated successfully!"
                    };

                    return RedirectToAction("AccountInfo", "Account");
                }
                catch (Exception ex)
                {
                    //provide an error message in case of an Exception
                    model.PageMessage = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Error,
                        Message = ex.Message
                    };
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult PaymentInfo(PaymentInfoModel model, string cancel)
        {
            if (cancel != null)
            {
                return (RedirectToAction("Index", "Home"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        //Credit Card
                        CreditCardRequest creditCard = new CreditCardRequest()
                        {
                            Cvv = model.CreditCard.Cvv,
                            ExpirationDate = model.CreditCard.ExpirationDate,
                            FirstNameOnCard = model.CreditCard.FirstNameOnCard,
                            LastNameOnCard = model.CreditCard.LastNameOnCard,
                            Number = model.CreditCard.Number
                        };

                        //Billing Address
                        AddressRequest billingAddress = new AddressRequest()
                        {

                            Address1 = model.BillingAddress.BillingAddress1,
                            City = model.BillingAddress.BillingCity,
                            Country = model.BillingAddress.BillingCountry,
                            FirstName = model.BillingAddress.BillingFirstName,
                            LastName = model.BillingAddress.BillingLastName,
                            StateProvince = model.BillingAddress.BillingProvince,
                            ZipPostalCode = model.BillingAddress.BillingZipPostalCode
                        };

                        ProfileCommon profile = ProfileCommon.GetUserProfile(User.Identity.Name);

                        // add or update the Credit card
                        if (model.CreditCard.Id <= 0)
                        {
                            var customer = _dynabicBillingGateway.Customer.GetCustomerByReferenceId(Config.MySiteSubdomain, profile.CustomerReferenceId);
                            _dynabicBillingGateway.Customer.AddCreditCard(customer.Id.ToString(), creditCard);
                        }
                        else
                        {
                            _dynabicBillingGateway.Customer.UpdateCreditCardByCustomerReferenceId(Config.MySiteSubdomain,
                                profile.CustomerReferenceId,
                                model.CreditCard.Id.ToString(),
                                creditCard);
                        }

                        // add or update the Billing Address
                        if (model.BillingAddress.BillingAddressId <= 0)
                        {
                            var customer = _dynabicBillingGateway.Customer.GetCustomerByReferenceId(Config.MySiteSubdomain, profile.CustomerReferenceId);
                            _dynabicBillingGateway.Customer.AddBillingAddress(customer.Id.ToString(), billingAddress);
                        }
                        else
                        {
                            _dynabicBillingGateway.Customer.UpdateBillingAddressByCustomerReferenceId(Config.MySiteSubdomain,
                                profile.CustomerReferenceId,
                                model.BillingAddress.BillingAddressId.ToString(),
                                billingAddress);
                        }

                        //set the Success message
                        TempData["PageMessage"] = new PageMessageModel
                        {
                            Type = PageMessageModel.MessageType.Success,
                            Message = "Your Payment details have been updated successfully."
                        };

                        return RedirectToAction("PaymentInfo", "Account");
                    }
                    catch
                    {
                        //provide an error message in case something went wrong
                        model.PageMessage = new PageMessageModel
                        {
                            Type = PageMessageModel.MessageType.Error,
                            Message = "Something went wrong. Please try again later."
                        };
                    }

                }
            }
            if (model.PageMessage == null)
                model.PageMessage = new PageMessageModel();
            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {

                // ChangePassword will throw an exception rather
                // than return false in certain failure scenarios.
                bool changePasswordSucceeded;
                try
                {
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);
                    changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
                }
                catch (Exception)
                {
                    changePasswordSucceeded = false;
                }

                if (changePasswordSucceeded)
                {
                    //set the Success message
                    TempData["PageMessage"] = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Success,
                        Message = "Your password has been changed successfully."
                    };

                    return RedirectToAction("ChangePassword");
                }
                else
                {
                    //provide an error message in case of an Exception
                    model.PageMessage = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Error,
                        Message = "The current password is incorrect or the new password is invalid."
                    };
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }