diff --git a/src/Application/Application.csproj b/src/Application/Application.csproj index 4e4c27e..f85a75e 100644 --- a/src/Application/Application.csproj +++ b/src/Application/Application.csproj @@ -19,6 +19,7 @@ + diff --git a/src/Application/Services/UserService.cs b/src/Application/Services/UserService.cs index cec318f..440e105 100644 --- a/src/Application/Services/UserService.cs +++ b/src/Application/Services/UserService.cs @@ -13,6 +13,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; +using Stripe; namespace Application.Services; @@ -109,8 +110,30 @@ public async Task DeleteUserAsync(string email) // ignored } + var customerId = user.CustomerId; _userRepository.Delete(user); - await _userRepository.SaveChangesAsync(); + var success = await _userRepository.SaveChangesAsync(); + + // Cancel a subscription if it exists + if (success > 0 && !customerId.IsNullOrEmpty()) + { + await CancelUserSubscription(customerId); + } + } + + private async Task CancelUserSubscription(string customerId) + { + var subscriptionService = new SubscriptionService(); + var options = new SubscriptionListOptions + { + Customer = customerId, + Status = "active" + }; + var subscriptions = await subscriptionService.ListAsync(options); + foreach (var subscription in subscriptions) + { + await subscriptionService.CancelAsync(subscription.Id); + } } public async Task PatchUserAsync(string email, diff --git a/src/Presentation/Controllers/WebHookController.cs b/src/Presentation/Controllers/WebHookController.cs index 9ec6a25..ef74461 100644 --- a/src/Presentation/Controllers/WebHookController.cs +++ b/src/Presentation/Controllers/WebHookController.cs @@ -51,6 +51,7 @@ public async Task Stripe() await AddTierToCustomer(stripeEvent.Data.Object as Subscription); break; case Events.CustomerSubscriptionDeleted: + case Events.CustomerSubscriptionPendingUpdateExpired: await RemoveTierFromCustomer(stripeEvent.Data.Object as Subscription); break; default: diff --git a/src/Presentation/Presentation.csproj b/src/Presentation/Presentation.csproj index b044deb..dfaa402 100644 --- a/src/Presentation/Presentation.csproj +++ b/src/Presentation/Presentation.csproj @@ -16,7 +16,7 @@ - +