@@ -14,34 +14,73 @@ namespace DevBetterWeb.Infrastructure.PaymentHandler.StripePaymentHandler;
1414public class StripePaymentHandlerSubscriptionService : IPaymentHandlerSubscription
1515{
1616 private readonly SubscriptionService _subscriptionService ;
17+ private readonly CustomerService _customerService ;
1718 private readonly IPaymentHandlerSubscriptionCreationService _paymentHandlerSubscriptionCreationService ;
1819 private readonly IRepository < Invitation > _invitationRepository ;
1920 private readonly IAppLogger < StripePaymentHandlerSubscriptionService > _logger ;
2021
2122 public StripePaymentHandlerSubscriptionService ( SubscriptionService subscriptionService ,
23+ CustomerService customerService ,
2224 IPaymentHandlerSubscriptionCreationService paymentHandlerSubscriptionCreationService ,
2325 IRepository < Invitation > invitationRepository ,
2426 IAppLogger < StripePaymentHandlerSubscriptionService > logger )
2527 {
2628 _subscriptionService = subscriptionService ;
29+ _customerService = customerService ;
2730 _paymentHandlerSubscriptionCreationService = paymentHandlerSubscriptionCreationService ;
2831 _invitationRepository = invitationRepository ;
2932 _logger = logger ;
3033 }
3134
3235 public async Task CancelSubscriptionAtPeriodEnd ( string customerEmail )
3336 {
34- var spec = new InactiveInvitationByEmailSpec ( customerEmail ) ;
35- var invite = await _invitationRepository . FirstOrDefaultAsync ( spec ) ;
36- if ( invite is null ) throw new InvitationNotFoundException ( customerEmail ) ;
37- var subscriptionId = invite . PaymentHandlerSubscriptionId ;
37+ var subscriptionId = await GetSubscriptionIdForEmailAsync ( customerEmail ) ;
3838
3939 var subscriptionCancelOptions = new SubscriptionUpdateOptions
4040 {
4141 CancelAtPeriodEnd = true ,
4242 } ;
4343
44- _subscriptionService . Update ( subscriptionId , subscriptionCancelOptions ) ;
44+ await _subscriptionService . UpdateAsync ( subscriptionId , subscriptionCancelOptions ) ;
45+ }
46+
47+ private async Task < string > GetSubscriptionIdForEmailAsync ( string customerEmail )
48+ {
49+ var spec = new InactiveInvitationByEmailSpec ( customerEmail ) ;
50+ var invite = await _invitationRepository . FirstOrDefaultAsync ( spec ) ;
51+
52+ if ( invite != null && ! string . IsNullOrEmpty ( invite . PaymentHandlerSubscriptionId ) )
53+ {
54+ return invite . PaymentHandlerSubscriptionId ;
55+ }
56+
57+ _logger . LogWarning ( $ "No inactive invitation with a valid subscription ID found for { customerEmail } . Falling back to Stripe customer lookup.") ;
58+
59+ var customerListOptions = new CustomerListOptions
60+ {
61+ Email = customerEmail . ToLower ( ) ,
62+ Limit = 1 ,
63+ } ;
64+ var customers = await _customerService . ListAsync ( customerListOptions ) ;
65+ if ( customers . Data . Count == 0 )
66+ {
67+ throw new InvitationNotFoundException ( customerEmail ) ;
68+ }
69+
70+ var customerId = customers . Data [ 0 ] . Id ;
71+ var subscriptionListOptions = new SubscriptionListOptions
72+ {
73+ Customer = customerId ,
74+ Status = "active" ,
75+ Limit = 1 ,
76+ } ;
77+ var subscriptions = await _subscriptionService . ListAsync ( subscriptionListOptions ) ;
78+ if ( subscriptions . Data . Count == 0 )
79+ {
80+ throw new InvitationNotFoundException ( customerEmail ) ;
81+ }
82+
83+ return subscriptions . Data [ 0 ] . Id ;
4584 }
4685
4786 public IPaymentHandlerSubscriptionDTO CreateSubscription ( string customerId , string priceId , string paymentMethodId )
0 commit comments