From b45ab59b60c3959360050fbb8d181a09b08e1502 Mon Sep 17 00:00:00 2001 From: Shawn Jackson Date: Fri, 28 Jun 2024 09:58:10 -0700 Subject: [PATCH] CU-8685awfkp fixing issue with new count plan upgrades for lapsed customers --- .../Services/ISubscriptionsService.cs | 2 +- Core/Resgrid.Services/SubscriptionsService.cs | 3 +- .../PublishProfiles/FolderProfile.pubxml | 8 +- .../Controllers/SubscriptionController.cs | 261 +++--------------- .../User/Views/Subscription/Index.cshtml | 14 +- 5 files changed, 54 insertions(+), 234 deletions(-) diff --git a/Core/Resgrid.Model/Services/ISubscriptionsService.cs b/Core/Resgrid.Model/Services/ISubscriptionsService.cs index ef1c1e2e..266ea452 100644 --- a/Core/Resgrid.Model/Services/ISubscriptionsService.cs +++ b/Core/Resgrid.Model/Services/ISubscriptionsService.cs @@ -246,7 +246,7 @@ Task CreateStripeSessionForCustomerPortal( Task SavePaymentEventAsync(PaymentProviderEvent providerEvent, CancellationToken cancellationToken = default(CancellationToken)); - Task CreateStripeSessionForSub(int departmentId, string stripeCustomerId, string stripePlanId, int planId, string email, string departmentName); + Task CreateStripeSessionForSub(int departmentId, string stripeCustomerId, string stripePlanId, int planId, string email, string departmentName, int count); Task ChangeActiveSubscriptionAsync(string stripeCustomerId, string stripePlanId); } diff --git a/Core/Resgrid.Services/SubscriptionsService.cs b/Core/Resgrid.Services/SubscriptionsService.cs index 8de55aa1..faa55529 100644 --- a/Core/Resgrid.Services/SubscriptionsService.cs +++ b/Core/Resgrid.Services/SubscriptionsService.cs @@ -1108,7 +1108,7 @@ public async Task CreateStripeSessionForCu return await _paymentProviderEventsRepository.SaveOrUpdateAsync(providerEvent, cancellationToken); } - public async Task CreateStripeSessionForSub(int departmentId, string stripeCustomerId, string stripePlanId, int planId, string email, string departmentName) + public async Task CreateStripeSessionForSub(int departmentId, string stripeCustomerId, string stripePlanId, int planId, string email, string departmentName, int count) { if (!String.IsNullOrWhiteSpace(Config.SystemBehaviorConfig.BillingApiBaseUrl) && !String.IsNullOrWhiteSpace(Config.ApiConfig.BackendInternalApikey)) { @@ -1120,6 +1120,7 @@ public async Task CreateStripeSessionForSub(in request.AddParameter("departmentId", departmentId, ParameterType.QueryString); request.AddParameter("stripePlanId", stripePlanId, ParameterType.QueryString); request.AddParameter("planId", planId, ParameterType.QueryString); + request.AddParameter("count", count, ParameterType.QueryString); request.AddParameter("email", email, ParameterType.QueryString, true); request.AddParameter("departmentName", departmentName, ParameterType.QueryString, true); diff --git a/Tools/Resgrid.Console/Properties/PublishProfiles/FolderProfile.pubxml b/Tools/Resgrid.Console/Properties/PublishProfiles/FolderProfile.pubxml index e7497bf0..ba708019 100644 --- a/Tools/Resgrid.Console/Properties/PublishProfiles/FolderProfile.pubxml +++ b/Tools/Resgrid.Console/Properties/PublishProfiles/FolderProfile.pubxml @@ -8,11 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. Any CPU bin\Release\netcoreapp3.1\publish\ FileSystem - netcoreapp3.1 - true - win-x64 - False - False - False + net8.0 + false \ No newline at end of file diff --git a/Web/Resgrid.WebCore/Areas/User/Controllers/SubscriptionController.cs b/Web/Resgrid.WebCore/Areas/User/Controllers/SubscriptionController.cs index 0d64fa6c..d1f9546a 100644 --- a/Web/Resgrid.WebCore/Areas/User/Controllers/SubscriptionController.cs +++ b/Web/Resgrid.WebCore/Areas/User/Controllers/SubscriptionController.cs @@ -67,13 +67,7 @@ public SubscriptionController(IDepartmentsService departmentsService, IUsersServ #endregion Private Members and Constructors [HttpGet] - -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task Index() { if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId)) @@ -231,12 +225,8 @@ public async Task Index() return View(model); } -#if (DEBUG || DOCKER) + [HttpGet] [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task UpdateBillingInfo() { if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId)) @@ -253,13 +243,7 @@ public async Task UpdateBillingInfo() } [HttpPost] - -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif [ValidateAntiForgeryToken] public async Task UpdateBillingInfo(IFormCollection form, CancellationToken cancellationToken) { @@ -331,12 +315,7 @@ public async Task LogStripeResponse(StripeResponseInput input, Ca } [HttpGet] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task ValidateCoupon(string couponCode) { var service = new CouponService(); @@ -358,12 +337,7 @@ public async Task ValidateCoupon(string couponCode) } [HttpGet] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task Cancel() { if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId)) @@ -393,12 +367,7 @@ public async Task StripeBillingInfoUpdateSuccess(string sessionId } [HttpPost] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task Cancel(CancelView model, CancellationToken cancellationToken) { if (!await _authorizationService.CanUserManageSubscriptionAsync(UserId, DepartmentId)) @@ -475,12 +444,8 @@ public async Task Cancel(CancelView model, CancellationToken canc return View(model); } -#if (DEBUG || DOCKER) + [HttpGet] [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task BuyAddon(string planAddonId) { var model = new BuyAddonView(); @@ -504,12 +469,8 @@ public async Task BuyAddon(string planAddonId) return View(model); } -#if (DEBUG || DOCKER) + [HttpGet] [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task ManagePTTAddon() { var model = new BuyAddonView(); @@ -529,70 +490,60 @@ public async Task ManagePTTAddon() model.Quantity = addon.TotalQuantity; } -/* - if (addons != null && addons.Count > 0) - model.CurrentPaymentAddon = addons.FirstOrDefault(); + /* + if (addons != null && addons.Count > 0) + model.CurrentPaymentAddon = addons.FirstOrDefault(); - var planAddons = await _subscriptionsService.GetCurrentPlanAddonsForDepartmentFromStripeAsync(DepartmentId); + var planAddons = await _subscriptionsService.GetCurrentPlanAddonsForDepartmentFromStripeAsync(DepartmentId); - if (planAddons != null && planAddons.Any()) - { - foreach (var addon in planAddons) - { - if (!addon.IsCancelled) - model.Quantity += addon.Quantity; - } - } + if (planAddons != null && planAddons.Any()) + { + foreach (var addon in planAddons) + { + if (!addon.IsCancelled) + model.Quantity += addon.Quantity; + } + } - if (model.PlanAddon.PlanId.HasValue) - { - var plan = await _subscriptionsService.GetPlanByIdAsync(model.PlanAddon.PlanId.Value); - model.Frequency = ((PlanFrequency)plan.Frequency).ToString(); - } + if (model.PlanAddon.PlanId.HasValue) + { + var plan = await _subscriptionsService.GetPlanByIdAsync(model.PlanAddon.PlanId.Value); + model.Frequency = ((PlanFrequency)plan.Frequency).ToString(); + } - */ + */ return View(model); } [HttpPost] - #if (DEBUG || DOCKER) - [Authorize(Policy = ResgridResources.Department_Update)] - #else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] - #endif - public async Task ManagePTTAddon(BuyAddonView model) - { - try - { - var user = _usersService.GetUserById(UserId); + [Authorize(Policy = ResgridResources.Department_Update)] + public async Task ManagePTTAddon(BuyAddonView model) + { + try + { + var user = _usersService.GetUserById(UserId); - var addonPlan = await _subscriptionsService.GetPlanAddonByIdAsync(model.PlanAddonId); - var plan = await _subscriptionsService.GetPlanByIdAsync(addonPlan.PlanId.Value); + var addonPlan = await _subscriptionsService.GetPlanAddonByIdAsync(model.PlanAddonId); + var plan = await _subscriptionsService.GetPlanByIdAsync(addonPlan.PlanId.Value); - var result = await _subscriptionsService.AddAddonAddedToExistingSub(DepartmentId, plan, addonPlan); + var result = await _subscriptionsService.AddAddonAddedToExistingSub(DepartmentId, plan, addonPlan); - return RedirectToAction("PaymentComplete", "Subscription", new { Area = "User", planId = plan.PlanId }); - } - catch (Exception ex) - { - Logging.SendExceptionEmail(ex, "BuyNow", DepartmentId, UserName); + return RedirectToAction("PaymentComplete", "Subscription", new { Area = "User", planId = plan.PlanId }); + } + catch (Exception ex) + { + Logging.SendExceptionEmail(ex, "BuyNow", DepartmentId, UserName); - return RedirectToAction("PaymentFailed", "Subscription", - new { Area = "User", chargeId = "", errorMessage = ex.Message }); - } - } + return RedirectToAction("PaymentFailed", "Subscription", + new { Area = "User", chargeId = "", errorMessage = ex.Message }); + } + } [HttpPost] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task BuyAddon(BuyAddonView model, CancellationToken cancellationToken) { try @@ -639,12 +590,8 @@ public async Task BuyAddon(BuyAddonView model, CancellationToken } } -#if (DEBUG || DOCKER) + [HttpGet] [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task CancelAddon(int addonTypeId) { @@ -666,132 +613,15 @@ public async Task CancelAddon(int addonTypeId) } -#if (DEBUG || DOCKER) - [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif - public async Task Upgrade(int planId, int count) - { - if (!_subscriptionsService.ValidateUserSelectableBuyNowPlan(planId)) - Unauthorized(); - - var model = new BuyNowView(); - model.Plan = await _subscriptionsService.GetPlanByIdAsync(planId); - model.PlanId = model.Plan.PlanId; - model.Department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId); - model.StripeKey = Config.PaymentProviderConfig.GetStripeClientKey(); - model.Payment = await _subscriptionsService.GetCurrentPaymentForDepartmentAsync(DepartmentId); - model.Price = model.Plan.Cost; - model.Frequency = ((PlanFrequency)model.Plan.Frequency).ToString(); - - if (model.Payment != null) - { - if (model.Payment.Plan.PlanId != 1 && model.Payment.Plan.PlanId != 7 && model.Payment.Plan.PlanId != 8 && - model.Payment.Plan.PlanId != 6 && model.Plan.PlanId != model.Payment.PlanId && - _subscriptionsService.GetPossibleUpgradesForPlan(model.Payment.PlanId).Any(x => x == model.Plan.PlanId)) - { - model.UpgradePrice = await _subscriptionsService.GetAdjustedUpgradePriceAsync(model.Payment.PaymentId, model.Plan.PlanId); - model.Price = await _subscriptionsService.GetAdjustedUpgradePriceAsync(model.Payment.PaymentId, model.Plan.PlanId); - - if (model.Plan.Frequency != model.Payment.Plan.Frequency) - model.FrequencyChange = true; - else - model.FrequencyChange = false; - - model.Upgrade = true; - model.Price = model.UpgradePrice; - - if (model.FrequencyChange) - { - var billingUpdate = _subscriptionsService.CalculateCyclesTillFirstBill(model.UpgradePrice, model.Plan.Cost); - model.BillingCycles = billingUpdate.Item1; - model.UpgradePrice = billingUpdate.Item2; - model.NextBillingCycle = - TimeConverterHelper.TimeConverter(DateTime.UtcNow, model.Department).AddMonths(model.BillingCycles); - } - - } - else if (model.Payment.Plan.PlanId != 1 && model.Payment.Plan.PlanId != 7 && model.Payment.Plan.PlanId != 8 && - model.Payment.Plan.PlanId != 6 && model.Plan.PlanId != model.Payment.PlanId && - _subscriptionsService.GetPossibleDowngradesForPlan(model.Payment.PlanId).Any(x => x == model.Plan.PlanId)) - { - model.UpgradePrice = await _subscriptionsService.GetAdjustedUpgradePriceAsync(model.Payment.PaymentId, model.Plan.PlanId); - model.Price = await _subscriptionsService.GetAdjustedUpgradePriceAsync(model.Payment.PaymentId, model.Plan.PlanId); - - if (model.Plan.Frequency != model.Payment.Plan.Frequency) - model.FrequencyChange = true; - else - model.FrequencyChange = false; - - model.Upgrade = true; - model.Price = model.UpgradePrice; - - if (model.FrequencyChange) - { - var billingUpdate = _subscriptionsService.CalculateCyclesTillFirstBill(model.UpgradePrice, model.Plan.Cost); - model.BillingCycles = billingUpdate.Item1; - model.UpgradePrice = billingUpdate.Item2; - model.NextBillingCycle = - TimeConverterHelper.TimeConverter(DateTime.UtcNow, model.Department).AddMonths(model.BillingCycles); - } - } - - } - - return View(model); - } - - [HttpPost] - -#if (DEBUG || DOCKER) - [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif - [ValidateAntiForgeryToken] - public async Task Upgrade(BuyNowView model, CancellationToken cancellationToken) - { - try - { - var user = _usersService.GetUserById(UserId); - - // Sneaky sneaky, did you change the plan id in the form? - if (!_subscriptionsService.ValidateUserSelectableBuyNowPlan(model.PlanId)) - Unauthorized(); - - var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId); - var plan = await _subscriptionsService.GetPlanByIdAsync(model.PlanId); - var invoice = await _subscriptionsService.ChangeActiveSubscriptionAsync(stripeCustomerId, plan.ExternalId); - - return RedirectToAction("Processing", "Subscription", new { Area = "User", planId = plan.PlanId }); - - } - catch (Exception ex) - { - Logging.SendExceptionEmail(ex, "BuyNow", DepartmentId, UserName); - - return RedirectToAction("PaymentFailed", "Subscription", - new { Area = "User", chargeId = "", errorMessage = ex.Message }); - } - } - [HttpGet] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif - public async Task GetStripeSession(int id, CancellationToken cancellationToken) + public async Task GetStripeSession(int id, int count, CancellationToken cancellationToken) { var plan = await _subscriptionsService.GetPlanByIdAsync(id); var stripeCustomerId = await _departmentSettingsService.GetStripeCustomerIdForDepartmentAsync(DepartmentId); var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId); var user = _usersService.GetUserById(UserId); - var session = await _subscriptionsService.CreateStripeSessionForSub(DepartmentId, stripeCustomerId, plan.GetExternalKey(), plan.PlanId, user.Email, department.Name); + var session = await _subscriptionsService.CreateStripeSessionForSub(DepartmentId, stripeCustomerId, plan.GetExternalKey(), plan.PlanId, user.Email, department.Name, count); var subscription = await _subscriptionsService.GetActiveStripeSubscriptionAsync(session.CustomerId); bool hasActiveSub = false; @@ -802,16 +632,11 @@ public async Task GetStripeSession(int id, CancellationToken canc { SessionId = session, HasActiveSub = hasActiveSub - }); ; + }); } [HttpGet] -#if (DEBUG || DOCKER) [Authorize(Policy = ResgridResources.Department_Update)] -#else - //[RequireHttps] - [Authorize(Policy = ResgridResources.Department_Update)] -#endif public async Task GetStripeUpdate() { //var plan = await _subscriptionsService.GetPlanById(id); diff --git a/Web/Resgrid.WebCore/Areas/User/Views/Subscription/Index.cshtml b/Web/Resgrid.WebCore/Areas/User/Views/Subscription/Index.cshtml index 932ca834..298482b3 100644 --- a/Web/Resgrid.WebCore/Areas/User/Views/Subscription/Index.cshtml +++ b/Web/Resgrid.WebCore/Areas/User/Views/Subscription/Index.cshtml @@ -99,7 +99,7 @@ .total { border-bottom: 1px solid #7f8c8d; /*display: inline; - padding: 10px 5px;*/ + padding: 10px 5px;*/ position: relative; padding-bottom: 20px; } @@ -140,8 +140,8 @@ border: 1px solid #eeeeee; border-radius: 4px; /*-moz-box-shadow: 0 5px 5px 0 #ccc; - -webkit-box-shadow: 0 5px 5px 0 #ccc; - box-shadow: 0 5px 5px 0 #ccc;*/ + -webkit-box-shadow: 0 5px 5px 0 #ccc; + box-shadow: 0 5px 5px 0 #ccc;*/ } .form-group { @@ -462,7 +462,7 @@ @section Scripts { - +