Skip to content

Commit

Permalink
Cancel subscription when deleting account
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Feb 22, 2024
1 parent 9655a2e commit acb621d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Application/Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="6.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.10" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Stripe.net" Version="43.15.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.24.0" />
</ItemGroup>
Expand Down
25 changes: 24 additions & 1 deletion src/Application/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Stripe;

namespace Application.Services;

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Presentation/Controllers/WebHookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public async Task<IActionResult> Stripe()
await AddTierToCustomer(stripeEvent.Data.Object as Subscription);
break;
case Events.CustomerSubscriptionDeleted:
case Events.CustomerSubscriptionPendingUpdateExpired:
await RemoveTierFromCustomer(stripeEvent.Data.Object as Subscription);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/Presentation/Presentation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="Stripe.net" Version="43.9.0" />
<PackageReference Include="Stripe.net" Version="43.15.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.10">
Expand Down

0 comments on commit acb621d

Please sign in to comment.