From 1d34c276e7f60034846d7d3628c7aea42941e9e0 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 11 Nov 2021 14:13:45 -0500 Subject: [PATCH] Add sponsorship validation to upcoming invoice webhook --- src/Billing/Controllers/StripeController.cs | 15 +++--- .../Models/Business/SubscriptionUpdate.cs | 46 ++++++++++++------- ...EnterpriseSponsorshipRevertingViewModel.cs | 2 +- src/Core/Services/IMailService.cs | 2 +- .../Implementations/HandlebarsMailService.cs | 8 ++-- .../OrganizationSponsorshipService.cs | 31 ++++++------- .../Implementations/StripePaymentService.cs | 4 +- 7 files changed, 58 insertions(+), 50 deletions(-) diff --git a/src/Billing/Controllers/StripeController.cs b/src/Billing/Controllers/StripeController.cs index ba6a12a6a..5adcb1db0 100644 --- a/src/Billing/Controllers/StripeController.cs +++ b/src/Billing/Controllers/StripeController.cs @@ -141,14 +141,6 @@ namespace Bit.Billing.Controllers { var newEndPeriod = subscription.CurrentPeriodEnd; - // sponsored org - if (IsSponsoredSubscription(subscription)) - { - var sponsorshipValid = await _organizationSponsorshipService - .ValidateSponsorshipAsync(ids.Item1.Value); - // TODO: How do we return from this? - } - await _organizationService.UpdateExpirationDateAsync(ids.Item1.Value, subscription.CurrentPeriodEnd); } @@ -177,6 +169,13 @@ namespace Bit.Billing.Controllers // org if (ids.Item1.HasValue) { + // sponsored org + if (IsSponsoredSubscription(subscription)) + { + await _organizationSponsorshipService + .ValidateSponsorshipAsync(ids.Item1.Value); + } + var org = await _organizationRepository.GetByIdAsync(ids.Item1.Value); if (org != null && OrgPlanForInvoiceNotifications(org)) { diff --git a/src/Core/Models/Business/SubscriptionUpdate.cs b/src/Core/Models/Business/SubscriptionUpdate.cs index 36646aef0..93991b78a 100644 --- a/src/Core/Models/Business/SubscriptionUpdate.cs +++ b/src/Core/Models/Business/SubscriptionUpdate.cs @@ -28,7 +28,7 @@ namespace Bit.Core.Models.Business } protected static SubscriptionItem SubscriptionItem(Subscription subscription, string planId) => - subscription.Items?.Data?.FirstOrDefault(i => i.Plan.Id == planId); + planId == null ? null : subscription.Items?.Data?.FirstOrDefault(i => i.Plan.Id == planId); } @@ -122,58 +122,70 @@ namespace Bit.Core.Models.Business public class SponsorOrganizationSubscriptionUpdate : SubscriptionUpdate { - private string _existingPlanStripeId; - private string _sponsoredPlanStripeId; - private bool _applySponsorship; + private readonly string _existingPlanStripeId; + private readonly string _sponsoredPlanStripeId; + private readonly bool _applySponsorship; protected override List PlanIds => new() { _existingPlanStripeId, _sponsoredPlanStripeId }; public SponsorOrganizationSubscriptionUpdate(StaticStore.Plan existingPlan, StaticStore.SponsoredPlan sponsoredPlan, bool applySponsorship) { _existingPlanStripeId = existingPlan.StripePlanId; - _sponsoredPlanStripeId = sponsoredPlan.StripePlanId; + _sponsoredPlanStripeId = sponsoredPlan?.StripePlanId; _applySponsorship = applySponsorship; } public override List RevertItemsOptions(Subscription subscription) { - return new() + var result = new List(); + if (AddStripeItem(subscription) != null) { - new SubscriptionItemOptions + result.Add(new SubscriptionItemOptions { Id = AddStripeItem(subscription)?.Id, Plan = AddStripePlanId, Quantity = 0, Deleted = true, - }, - new SubscriptionItemOptions + }); + } + + if (RemoveStripeItem(subscription) != null) + { + result.Add(new SubscriptionItemOptions { Id = RemoveStripeItem(subscription)?.Id, Plan = RemoveStripePlanId, Quantity = 1, Deleted = false, - }, - }; + }); + } + return result; } public override List UpgradeItemsOptions(Subscription subscription) { - return new() + var result = new List(); + if (RemoveStripeItem(subscription) != null) { - new SubscriptionItemOptions + result.Add(new SubscriptionItemOptions { Id = RemoveStripeItem(subscription)?.Id, Plan = RemoveStripePlanId, Quantity = 0, Deleted = true, - }, - new SubscriptionItemOptions + }); + } + + if (AddStripeItem(subscription) != null) + { + result.Add(new SubscriptionItemOptions { Id = AddStripeItem(subscription)?.Id, Plan = AddStripePlanId, Quantity = 1, Deleted = false, - }, - }; + }); + } + return result; } private string RemoveStripePlanId => _applySponsorship ? _existingPlanStripeId : _sponsoredPlanStripeId; diff --git a/src/Core/Models/Mail/FamiliesForEnterprise/FamiliesForEnterpriseSponsorshipRevertingViewModel.cs b/src/Core/Models/Mail/FamiliesForEnterprise/FamiliesForEnterpriseSponsorshipRevertingViewModel.cs index cef96099f..e8185000b 100644 --- a/src/Core/Models/Mail/FamiliesForEnterprise/FamiliesForEnterpriseSponsorshipRevertingViewModel.cs +++ b/src/Core/Models/Mail/FamiliesForEnterprise/FamiliesForEnterpriseSponsorshipRevertingViewModel.cs @@ -2,6 +2,6 @@ namespace Bit.Core.Models.Mail.FamiliesForEnterprise { public class FamiliesForEnterpriseSponsorshipRevertingViewModel : BaseMailModel { - + public string OrganizationName { get; set; } } } diff --git a/src/Core/Services/IMailService.cs b/src/Core/Services/IMailService.cs index 5e8ea8f3b..6c23ab1d0 100644 --- a/src/Core/Services/IMailService.cs +++ b/src/Core/Services/IMailService.cs @@ -53,7 +53,7 @@ namespace Bit.Core.Services Task SendFamiliesForEnterpriseOfferEmailAsync(string email, string organizationName, string token); Task SendFamiliesForEnterpriseRedeemedEmailsAsync(string familyUserEmail, string sponsorEmail, string sponsorOrgName); Task SendFamiliesForEnterpriseReconfirmationRequiredEmailAsync(string email); - Task SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(string email); + Task SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(string email, string familyOrgName); Task SendFamiliesForEnterpriseSponsorshipEndingEmailAsync(string email); } } diff --git a/src/Core/Services/Implementations/HandlebarsMailService.cs b/src/Core/Services/Implementations/HandlebarsMailService.cs index 301f89e1a..fdc2afe5d 100644 --- a/src/Core/Services/Implementations/HandlebarsMailService.cs +++ b/src/Core/Services/Implementations/HandlebarsMailService.cs @@ -801,7 +801,7 @@ namespace Bit.Core.Services var message = CreateDefaultMessage("A User Has Redeemeed Your Sponsorship", email); var model = new FamiliesForEnterpriseRedeemedToOrgUserViewModel { - OrganizationName = organizationName, + OrganizationName = CoreHelpers.SanitizeForEmail(organizationName, false), }; await AddMessageContentAsync(message, "FamiliesForEnterprise.FamiliesForEnterpriseRedeemedToOrgUser", model); message.Category = "FamilyForEnterpriseRedeemedToOrgUser"; @@ -821,13 +821,13 @@ namespace Bit.Core.Services await _mailDeliveryService.SendEmailAsync(message); } - public async Task SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(string email) + public async Task SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(string email, string familyOrgName) { // TODO: Complete emails - var message = CreateDefaultMessage("A Family Organization Sponsorship Is Reverting", email); + var message = CreateDefaultMessage($"{familyOrgName} Organization Sponsorship Is No Longer Valid", email); var model = new FamiliesForEnterpriseSponsorshipRevertingViewModel { - + OrganizationName = CoreHelpers.SanitizeForEmail(familyOrgName, false), }; await AddMessageContentAsync(message, "FamiliesForEnterprise.FamiliesForEnterpriseSponsorshipReverting", model); message.Category = "FamiliesForEnterpriseSponsorshipReverting"; diff --git a/src/Core/Services/Implementations/OrganizationSponsorshipService.cs b/src/Core/Services/Implementations/OrganizationSponsorshipService.cs index 2ab815e52..8dde09d9e 100644 --- a/src/Core/Services/Implementations/OrganizationSponsorshipService.cs +++ b/src/Core/Services/Implementations/OrganizationSponsorshipService.cs @@ -133,38 +133,30 @@ namespace Bit.Core.Services if (existingSponsorship == null) { - // TODO: null safe this method await RemoveSponsorshipAsync(sponsoredOrganization, null); - // TODO on fail, mark org as disabled. return false; } - var validated = true; - if (existingSponsorship.SponsoringOrganizationId == null || existingSponsorship.SponsoringOrganizationUserId == null) + if (existingSponsorship.SponsoringOrganizationId == null || existingSponsorship.SponsoringOrganizationUserId == null || existingSponsorship.PlanSponsorshipType == null) { await RemoveSponsorshipAsync(sponsoredOrganization, existingSponsorship); - validated = false; + return false; } + var sponsoredPlan = Utilities.StaticStore.GetSponsoredPlan(existingSponsorship.PlanSponsorshipType.Value); var sponsoringOrganization = await _organizationRepository .GetByIdAsync(existingSponsorship.SponsoringOrganizationId.Value); - if (!sponsoringOrganization.Enabled) + if (sponsoringOrganization == null) { await RemoveSponsorshipAsync(sponsoredOrganization, existingSponsorship); - validated = false; + return false; } - if (!validated && existingSponsorship.SponsoredOrganizationId != null) + var sponsoringOrgPlan = Utilities.StaticStore.GetPlan(sponsoringOrganization.PlanType); + if (!sponsoringOrganization.Enabled || sponsoredPlan.SponsoringProductType != sponsoringOrgPlan.Product) { - existingSponsorship.TimesRenewedWithoutValidation += 1; - existingSponsorship.SponsorshipLapsedDate ??= DateTime.UtcNow; - - await _organizationSponsorshipRepository.UpsertAsync(existingSponsorship); - if (existingSponsorship.TimesRenewedWithoutValidation >= 6) - { - sponsoredOrganization.Enabled = false; - await _organizationRepository.UpsertAsync(sponsoredOrganization); - } + await RemoveSponsorshipAsync(sponsoredOrganization, existingSponsorship); + return false; } return true; @@ -175,6 +167,10 @@ namespace Bit.Core.Services await _paymentService.RemoveOrganizationSponsorshipAsync(sponsoredOrganization, sponsorship); await _organizationRepository.UpsertAsync(sponsoredOrganization); + await _mailService.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync( + sponsoredOrganization.BillingEmailAddress(), + sponsoredOrganization.Name); + if (sponsorship == null) { return; @@ -197,6 +193,5 @@ namespace Bit.Core.Services await _organizationSponsorshipRepository.UpsertAsync(sponsorship); } } - } } diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index fcc55c3ca..7ca89b201 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -195,7 +195,9 @@ namespace Bit.Core.Services private async Task ChangeOrganizationSponsorship(Organization org, OrganizationSponsorship sponsorship, bool applySponsorship) { var existingPlan = Utilities.StaticStore.GetPlan(org.PlanType); - var sponsoredPlan = Utilities.StaticStore.GetSponsoredPlan(sponsorship.PlanSponsorshipType.Value); + var sponsoredPlan = sponsorship != null ? + Utilities.StaticStore.GetSponsoredPlan(sponsorship.PlanSponsorshipType.Value) : + null; var subscriptionUpdate = new SponsorOrganizationSubscriptionUpdate(existingPlan, sponsoredPlan, applySponsorship); await FinalizeSubscriptionChangeAsync(org, subscriptionUpdate, DateTime.UtcNow);