From 376d58b267a2276da28aa58d23b0b549ff80e809 Mon Sep 17 00:00:00 2001 From: Alex Morask Date: Tue, 22 Apr 2025 09:25:29 -0400 Subject: [PATCH] Revert "Only automatically set collection method for MSP (#5680)" This reverts commit d63df4acf28b2dec9e15d545685dc4a386cf8db5. --- .../PaymentMethodAttachedHandler.cs | 62 ++++++++----------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/src/Billing/Services/Implementations/PaymentMethodAttachedHandler.cs b/src/Billing/Services/Implementations/PaymentMethodAttachedHandler.cs index 97bb29c35d..c46429412f 100644 --- a/src/Billing/Services/Implementations/PaymentMethodAttachedHandler.cs +++ b/src/Billing/Services/Implementations/PaymentMethodAttachedHandler.cs @@ -1,7 +1,5 @@ using Bit.Billing.Constants; using Bit.Core; -using Bit.Core.AdminConsole.Enums.Provider; -using Bit.Core.AdminConsole.Repositories; using Bit.Core.Billing.Constants; using Bit.Core.Billing.Extensions; using Bit.Core.Services; @@ -17,22 +15,19 @@ public class PaymentMethodAttachedHandler : IPaymentMethodAttachedHandler private readonly IStripeFacade _stripeFacade; private readonly IStripeEventUtilityService _stripeEventUtilityService; private readonly IFeatureService _featureService; - private readonly IProviderRepository _providerRepository; public PaymentMethodAttachedHandler( ILogger logger, IStripeEventService stripeEventService, IStripeFacade stripeFacade, IStripeEventUtilityService stripeEventUtilityService, - IFeatureService featureService, - IProviderRepository providerRepository) + IFeatureService featureService) { _logger = logger; _stripeEventService = stripeEventService; _stripeFacade = stripeFacade; _stripeEventUtilityService = stripeEventUtilityService; _featureService = featureService; - _providerRepository = providerRepository; } public async Task HandleAsync(Event parsedEvent) @@ -73,50 +68,43 @@ public class PaymentMethodAttachedHandler : IPaymentMethodAttachedHandler * If we have an invoiced provider subscription where the customer hasn't been marked as invoice-approved, * we need to try and set the default payment method and update the collection method to be "charge_automatically". */ - if (invoicedProviderSubscription != null && - !customer.ApprovedToPayByInvoice() && - Guid.TryParse(invoicedProviderSubscription.Metadata[StripeConstants.MetadataKeys.ProviderId], out var providerId)) + if (invoicedProviderSubscription != null && !customer.ApprovedToPayByInvoice()) { - var provider = await _providerRepository.GetByIdAsync(providerId); - - if (provider is { Type: ProviderType.Msp }) + if (customer.InvoiceSettings.DefaultPaymentMethodId != paymentMethod.Id) { - if (customer.InvoiceSettings.DefaultPaymentMethodId != paymentMethod.Id) - { - try - { - await _stripeFacade.UpdateCustomer(customer.Id, - new CustomerUpdateOptions - { - InvoiceSettings = new CustomerInvoiceSettingsOptions - { - DefaultPaymentMethod = paymentMethod.Id - } - }); - } - catch (Exception exception) - { - _logger.LogWarning(exception, - "Failed to set customer's ({CustomerID}) default payment method during 'payment_method.attached' webhook", - customer.Id); - } - } - try { - await _stripeFacade.UpdateSubscription(invoicedProviderSubscription.Id, - new SubscriptionUpdateOptions + await _stripeFacade.UpdateCustomer(customer.Id, + new CustomerUpdateOptions { - CollectionMethod = StripeConstants.CollectionMethod.ChargeAutomatically + InvoiceSettings = new CustomerInvoiceSettingsOptions + { + DefaultPaymentMethod = paymentMethod.Id + } }); } catch (Exception exception) { _logger.LogWarning(exception, - "Failed to set subscription's ({SubscriptionID}) collection method to 'charge_automatically' during 'payment_method.attached' webhook", + "Failed to set customer's ({CustomerID}) default payment method during 'payment_method.attached' webhook", customer.Id); } } + + try + { + await _stripeFacade.UpdateSubscription(invoicedProviderSubscription.Id, + new SubscriptionUpdateOptions + { + CollectionMethod = StripeConstants.CollectionMethod.ChargeAutomatically + }); + } + catch (Exception exception) + { + _logger.LogWarning(exception, + "Failed to set subscription's ({SubscriptionID}) collection method to 'charge_automatically' during 'payment_method.attached' webhook", + customer.Id); + } } var unpaidSubscriptions = subscriptions?.Data.Where(subscription =>