diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 66552a48a..6b466742c 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -518,12 +518,7 @@ namespace Bit.Core.Services { if(!string.IsNullOrWhiteSpace(organization.GatewaySubscriptionId)) { - var subscriptionService = new StripeSubscriptionService(); - var canceledSub = await subscriptionService.CancelAsync(organization.GatewaySubscriptionId, false); - if(!canceledSub.CanceledAt.HasValue) - { - throw new BadRequestException("Unable to cancel subscription."); - } + await _stripePaymentService.CancelSubscriptionAsync(organization, false); } await _organizationRepository.DeleteAsync(organization); diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index cd0ebec03..166ed01d8 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -191,15 +191,26 @@ namespace Bit.Core.Services throw new GatewayException("Subscription was not found."); } - if(sub.CanceledAt.HasValue) + if(sub.CanceledAt.HasValue || sub.Status == "cancelled") { - throw new GatewayException("Subscription is already canceled."); + // Already cancelled + return; } - var canceledSub = await subscriptionService.CancelAsync(sub.Id, endOfPeriod); - if(!canceledSub.CanceledAt.HasValue) + try { - throw new GatewayException("Unable to cancel subscription."); + var canceledSub = await subscriptionService.CancelAsync(sub.Id, endOfPeriod); + if(!canceledSub.CanceledAt.HasValue) + { + throw new GatewayException("Unable to cancel subscription."); + } + } + catch(StripeException e) + { + if(e.Message != $"No such subscription: {subscriber.GatewaySubscriptionId}") + { + throw e; + } } }