From c8022920982a5a5dd6c3982fd38456183b6e8343 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 10 Aug 2017 09:12:55 -0400 Subject: [PATCH] handle cases when sub is already cancelled --- .../Implementations/OrganizationService.cs | 7 +------ .../Implementations/StripePaymentService.cs | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 66552a48a6..6b466742c8 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 cd0ebec038..166ed01d8d 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; + } } }