mirror of
https://github.com/bitwarden/server
synced 2026-01-15 06:53:26 +00:00
* Implement bank account hosted URL verification with webhook handling notification * Fix tests * Run dotnet format * Remove unused VerifyBankAccount operation * Stephon's feedback * Removing unused test * TEMP: Add logging for deployment check * Run dotnet format * fix test * Revert "fix test" This reverts commitb8743ab3b5. * Revert "Run dotnet format" This reverts commit5c861b0b72. * Revert "TEMP: Add logging for deployment check" This reverts commit0a88acd6a1. * Resolve GetPaymentMethodQuery order of operations
78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.AdminConsole.Entities.Provider;
|
|
using Bit.Core.AdminConsole.Repositories;
|
|
using Bit.Core.Billing.Caches;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Services;
|
|
using OneOf;
|
|
using Stripe;
|
|
using Event = Stripe.Event;
|
|
|
|
namespace Bit.Billing.Services.Implementations;
|
|
|
|
public class SetupIntentSucceededHandler(
|
|
IOrganizationRepository organizationRepository,
|
|
IProviderRepository providerRepository,
|
|
IPushNotificationAdapter pushNotificationAdapter,
|
|
ISetupIntentCache setupIntentCache,
|
|
IStripeAdapter stripeAdapter,
|
|
IStripeEventService stripeEventService) : ISetupIntentSucceededHandler
|
|
{
|
|
public async Task HandleAsync(Event parsedEvent)
|
|
{
|
|
var setupIntent = await stripeEventService.GetSetupIntent(
|
|
parsedEvent,
|
|
true,
|
|
["payment_method"]);
|
|
|
|
if (setupIntent is not
|
|
{
|
|
PaymentMethod.UsBankAccount: not null
|
|
})
|
|
{
|
|
return;
|
|
}
|
|
|
|
var subscriberId = await setupIntentCache.GetSubscriberIdForSetupIntent(setupIntent.Id);
|
|
if (subscriberId == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var organization = await organizationRepository.GetByIdAsync(subscriberId.Value);
|
|
var provider = await providerRepository.GetByIdAsync(subscriberId.Value);
|
|
|
|
OneOf<Organization, Provider> entity = organization != null ? organization : provider!;
|
|
await SetPaymentMethodAsync(entity, setupIntent.PaymentMethod);
|
|
}
|
|
|
|
private async Task SetPaymentMethodAsync(
|
|
OneOf<Organization, Provider> subscriber,
|
|
PaymentMethod paymentMethod)
|
|
{
|
|
var customerId = subscriber.Match(
|
|
organization => organization.GatewayCustomerId,
|
|
provider => provider.GatewayCustomerId);
|
|
|
|
if (string.IsNullOrEmpty(customerId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
await stripeAdapter.PaymentMethodAttachAsync(paymentMethod.Id,
|
|
new PaymentMethodAttachOptions { Customer = customerId });
|
|
|
|
await stripeAdapter.CustomerUpdateAsync(customerId, new CustomerUpdateOptions
|
|
{
|
|
InvoiceSettings = new CustomerInvoiceSettingsOptions
|
|
{
|
|
DefaultPaymentMethod = paymentMethod.Id
|
|
}
|
|
});
|
|
|
|
await subscriber.Match(
|
|
async organization => await pushNotificationAdapter.NotifyBankAccountVerifiedAsync(organization),
|
|
async provider => await pushNotificationAdapter.NotifyBankAccountVerifiedAsync(provider));
|
|
}
|
|
}
|