Skip to content

Fix Cancel Subscription failure by adding Stripe customer lookup fallback#1395

Merged
ardalis merged 2 commits intomainfrom
copilot/fix-cancel-subscription-error
Mar 4, 2026
Merged

Fix Cancel Subscription failure by adding Stripe customer lookup fallback#1395
ardalis merged 2 commits intomainfrom
copilot/fix-cancel-subscription-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

CancelSubscriptionAtPeriodEnd exclusively relied on finding an inactive Invitation record by email to resolve the Stripe subscription ID. Members without a matching inactive invitation (pre-invitation-system members, admin-provisioned accounts, cleaned-up records, or invitations with an empty PaymentHandlerSubscriptionId) would always throw, surfacing as "Attempt to cancel subscription failed."

Changes

  • StripePaymentHandlerSubscriptionService
    • Inject CustomerService (already registered in DI)
    • Extract GetSubscriptionIdForEmailAsync with two-stage resolution:
      1. Primary: existing invitation table lookup
      2. Fallback: Stripe customer lookup by email → active subscription, when invitation is missing or has no subscription ID
    • Switch Update/List calls to their async variants (UpdateAsync, ListAsync)
    • Log a warning when the fallback path is taken
private async Task<string> GetSubscriptionIdForEmailAsync(string customerEmail)
{
    var invite = await _invitationRepository.FirstOrDefaultAsync(
        new InactiveInvitationByEmailSpec(customerEmail));

    if (invite != null && !string.IsNullOrEmpty(invite.PaymentHandlerSubscriptionId))
        return invite.PaymentHandlerSubscriptionId;

    _logger.LogWarning($"No inactive invitation with a valid subscription ID found for {customerEmail}. Falling back to Stripe customer lookup.");

    var customers = await _customerService.ListAsync(new CustomerListOptions { Email = customerEmail.ToLower(), Limit = 1 });
    if (customers.Data.Count == 0) throw new InvitationNotFoundException(customerEmail);

    var subscriptions = await _subscriptionService.ListAsync(new SubscriptionListOptions
        { Customer = customers.Data[0].Id, Status = "active", Limit = 1 });
    if (subscriptions.Data.Count == 0) throw new InvitationNotFoundException(customerEmail);

    return subscriptions.Data[0].Id;
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.cdnjs.com
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet /usr/share/dotnet/sdk/10.0.102/MSBuild.dll /noautoresponse /nologo /nodemode:1 /nodeReuse:false /low:false _TOKEN&#34;; }; f (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet /usr/share/dotnet/sdk/10.0.102/MSBuild.dll /noautoresponse /nologo /nodemode:1 /nodeReuse:false /low:false ep/bin/linux-x64/rg (dns block)
    • Triggering command: /usr/bin/dotnet dotnet build tests/DevBetterWeb.Tests/DevBetterWeb.Tests.csproj (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Error: Cancel Subscription</issue_title>
<issue_description>In the member portal, under My Profile: Cancel Subscription, there's a client side error preventing the behavior from working properly.

Image</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: ardalis <782127+ardalis@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix client side error on cancel subscription feature Fix Cancel Subscription failure by adding Stripe customer lookup fallback Mar 4, 2026
@ardalis ardalis marked this pull request as ready for review March 4, 2026 21:46
@ardalis ardalis merged commit 6195e53 into main Mar 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Cancel Subscription

2 participants