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 ChangePlan() { ProfileCommon profile = ProfileCommon.GetUserProfile(User.Identity.Name); try { var creditCard = _dynabicBillingGateway.Customer.GetFirstCreditCardForCustomerByReferenceId(Config.MySiteSubdomain, profile.CustomerReferenceId); } catch (NotFoundException) { return View("FillPaymentInfoWarning", new PageMessageModel { Message = "You must set your Payment details before changing a Plan.", Type = PageMessageModel.MessageType.Warning }); } ChangePlanModel model = new ChangePlanModel(_dynabicBillingGateway, profile.CurrentPlanId); model.Plans.SelectedPlan = profile.CurrentPlanId; model.PageMessage = TempData["PageMessage"] as PageMessageModel ?? new PageMessageModel(); return View(model); }