From b7e8852250123c56f2f11da65eabf60b9001db56 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 25 Jul 2017 09:04:22 -0400 Subject: [PATCH] disable premium membership --- src/Billing/Controllers/StripeController.cs | 19 +++++++++++++++---- src/Core/Services/IUserService.cs | 1 + .../Services/Implementations/UserService.cs | 11 +++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Billing/Controllers/StripeController.cs b/src/Billing/Controllers/StripeController.cs index 90dd60e08..7b60a47b9 100644 --- a/src/Billing/Controllers/StripeController.cs +++ b/src/Billing/Controllers/StripeController.cs @@ -14,15 +14,18 @@ namespace Bit.Billing.Controllers private readonly BillingSettings _billingSettings; private readonly IHostingEnvironment _hostingEnvironment; private readonly IOrganizationService _organizationService; + private readonly IUserService _userService; public StripeController( IOptions billingSettings, IHostingEnvironment hostingEnvironment, - IOrganizationService organizationService) + IOrganizationService organizationService, + IUserService userService) { _billingSettings = billingSettings?.Value; _hostingEnvironment = hostingEnvironment; _organizationService = organizationService; + _userService = userService; } [HttpPost("webhook")] @@ -48,10 +51,18 @@ namespace Bit.Billing.Controllers { var subscription = Mapper.MapFromJson(parsedEvent.Data.Object.ToString()) as StripeSubscription; - if(subscription?.Status == "canceled" && (subscription.Metadata?.ContainsKey("organizationId") ?? false)) + if(subscription?.Status == "canceled") { - var orgIdGuid = new Guid(subscription.Metadata["organizationId"]); - await _organizationService.DisableAsync(orgIdGuid); + if(subscription.Metadata?.ContainsKey("organizationId") ?? false) + { + var orgIdGuid = new Guid(subscription.Metadata["organizationId"]); + await _organizationService.DisableAsync(orgIdGuid); + } + else if(subscription.Metadata?.ContainsKey("userId") ?? false) + { + var userIdGuid = new Guid(subscription.Metadata["userId"]); + await _userService.DisablePremiumAsync(userIdGuid); + } } } else diff --git a/src/Core/Services/IUserService.cs b/src/Core/Services/IUserService.cs index 90edd5ac5..a31e965e6 100644 --- a/src/Core/Services/IUserService.cs +++ b/src/Core/Services/IUserService.cs @@ -43,5 +43,6 @@ namespace Bit.Core.Services Task ReplacePaymentMethodAsync(User user, string paymentToken); Task CancelPremiumAsync(User user, bool endOfPeriod = false); Task ReinstatePremiumAsync(User user); + Task DisablePremiumAsync(Guid userId); } } diff --git a/src/Core/Services/Implementations/UserService.cs b/src/Core/Services/Implementations/UserService.cs index dccaa0f07..5cda8568c 100644 --- a/src/Core/Services/Implementations/UserService.cs +++ b/src/Core/Services/Implementations/UserService.cs @@ -610,6 +610,17 @@ namespace Bit.Core.Services await BillingHelpers.ReinstateSubscriptionAsync(user); } + public async Task DisablePremiumAsync(Guid userId) + { + var user = await _userRepository.GetByIdAsync(userId); + if(user != null && user.Premium) + { + user.Premium = false; + user.RevisionDate = DateTime.UtcNow; + await _userRepository.ReplaceAsync(user); + } + } + private async Task UpdatePasswordHash(User user, string newPassword, bool validatePassword = true) { if(validatePassword)