public virtual async Task <IActionResult> ChoosePlan() { try { Logger.LogInformation("Getting user."); var user = await UserDbServiceHelper.GetAppUserByIdAsync(UserDbService, UserInContextHelper.GetCurrentUserId(HttpContext)); var planStartId = user.GetPlanId(); ViewBag.PrePlan = planStartId > 0 ? true : false; Logger.LogInformation($"Got user. User ID '{user.Id}', existing plan/plan start ID '{planStartId}'."); //Dev plans are only included if ip is privileged and user is admin var ipIsPriviledged = IPAddressHelper.IsCurrentUserIpPrivileged(HttpContext, Settings); var isAdmin = user.IsAdmin;; Logger.LogInformation($"User IP is priviledged : '{ipIsPriviledged}'."); Logger.LogInformation($"User is admin : '{isAdmin}'."); bool includeDev = ipIsPriviledged && isAdmin; Logger.LogInformation($"Dev plans are being included : '{includeDev}'"); Logger.LogInformation("Listing plans now."); var plans = PlanReader.GetAvailableUpgrades(planStartId, includeDev); Logger.LogInformation($"Total '{plans.Count}' have been listed."); Logger.LogInformation($"Choose plan view name is {Views.Shopify.ChoosePlan}."); return(View(Views.Shopify.ChoosePlan, plans)); } catch (Exception ex) { Logger.LogWarning("Error occurred while executing ChoosePlan())"); LogGenericError(ex); throw ex; } }
public virtual async Task <IActionResult> SelectedPlan(int planId) { using (Logger.BeginScope(new { PlanId = planId })) { try { Logger.LogInformation("Getting user"); var user = await UserDbServiceHelper.GetAppUserByIdAsync(UserDbService, UserInContextHelper.GetCurrentUserId(HttpContext)); string domain = user.MyShopifyDomain; string token = user.ShopifyAccessToken; /*user plan id = 0 means that customer is new*/ int userPlanId = user.GetPlanId(); Logger.LogInformation($"Got user.User ID '{user.Id}', domain '{user.MyShopifyDomain}', token '{user.ShopifyAccessToken}' and Plan Id '{user.PlanId}'."); //privileged ip holders can downgrade or upgrade plan, others are upgrade only var validUpgrade = planId >= userPlanId; var priviledgedUser = IPAddressHelper.IsCurrentUserIpPrivileged(HttpContext, Settings); Logger.LogInformation($"Selected is a valid upgrade : {validUpgrade}"); Logger.LogInformation($"Selector's IP is priviledged: {priviledgedUser}"); if (validUpgrade || priviledgedUser) { Logger.LogInformation("Plan selection is approved."); var plan = PlanReader[planId]; if (plan != null && plan.Id > 0) { Logger.LogInformation($"Found plan for the selected ID. Plan Name '{plan.Name}'."); var charge = new ShopifyRecurringChargeObject() { Name = plan.Name, Price = plan.Price, TrialDays = plan.TrialDays, Test = plan.IsTest, ReturnUrl = ShopifyUrlHelper.GetChargeResultHandlerUrl(Settings), }; try { Logger.LogInformation("Creating recurring charge via api for selected plan."); charge = await ShopifyAPI.CreateRecurringChargeAsync(domain, token, charge); Logger.LogInformation($"Successfully created recurring charge. Redirecting to confirmation URL '{charge.ConfirmationUrl}'."); return(Redirect(charge.ConfirmationUrl)); } catch (Exception ex) { Logger.LogError(ex, $"Failed creating recurring charge for the selected plan.Redirecting to '{SHOPIFY_ACTIONS.ChoosePlan.ToString()}' action."); WebMsg.AddTempDanger(this, "Could not create a recurring charge record/confirmation url via shopify api. Please try again.", false, false); return(RedirectToAction(SHOPIFY_ACTIONS.ChoosePlan.ToString(), Settings.GetShopifyControllerName())); } } } //if we are here then it is an invalid plan Logger.LogWarning($"Selection is not approved.Redirecting to '{SHOPIFY_ACTIONS.ChoosePlan.ToString()}' adction"); WebMsg.AddTempDanger(this, "Invalid Plan Selected", false, false); return(RedirectToAction(SHOPIFY_ACTIONS.ChoosePlan.ToString(), Settings.GetShopifyControllerName())); } catch (Exception ex) { Logger.LogWarning("Error occurred while executing SelectedPlan())"); LogGenericError(ex); throw ex; } } }