mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 21:18:13 -05:00

* Changes ensures provider_id is handled and stored for Braintree. Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * refactoring of the stripeController class Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Move the constant variables to utility class Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Adding comments to the methods Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Add more comments to describe the method Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Add the providerId changes Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Add the missing providerId Signed-off-by: Cy Okeke <cokeke@bitwarden.com> * Fix the IsSponsoredSubscription bug Signed-off-by: Cy Okeke <cokeke@bitwarden.com> --------- Signed-off-by: Cy Okeke <cokeke@bitwarden.com>
61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
using Bit.Core.Context;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Tools.Enums;
|
|
using Bit.Core.Tools.Models.Business;
|
|
using Bit.Core.Tools.Services;
|
|
using Event = Stripe.Event;
|
|
|
|
namespace Bit.Billing.Services.Implementations;
|
|
|
|
public class CustomerUpdatedHandler : ICustomerUpdatedHandler
|
|
{
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
private readonly IReferenceEventService _referenceEventService;
|
|
private readonly ICurrentContext _currentContext;
|
|
private readonly IStripeEventService _stripeEventService;
|
|
private readonly IStripeEventUtilityService _stripeEventUtilityService;
|
|
|
|
public CustomerUpdatedHandler(
|
|
IOrganizationRepository organizationRepository,
|
|
IReferenceEventService referenceEventService,
|
|
ICurrentContext currentContext,
|
|
IStripeEventService stripeEventService,
|
|
IStripeEventUtilityService stripeEventUtilityService)
|
|
{
|
|
_organizationRepository = organizationRepository;
|
|
_referenceEventService = referenceEventService;
|
|
_currentContext = currentContext;
|
|
_stripeEventService = stripeEventService;
|
|
_stripeEventUtilityService = stripeEventUtilityService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles the <see cref="HandledStripeWebhook.CustomerUpdated"/> event type from Stripe.
|
|
/// </summary>
|
|
/// <param name="parsedEvent"></param>
|
|
public async Task HandleAsync(Event parsedEvent)
|
|
{
|
|
var customer = await _stripeEventService.GetCustomer(parsedEvent, true, ["subscriptions"]);
|
|
if (customer.Subscriptions == null || !customer.Subscriptions.Any())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var subscription = customer.Subscriptions.First();
|
|
|
|
var (organizationId, _, providerId) = _stripeEventUtilityService.GetIdsFromMetadata(subscription.Metadata);
|
|
|
|
if (!organizationId.HasValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var organization = await _organizationRepository.GetByIdAsync(organizationId.Value);
|
|
organization.BillingEmail = customer.Email;
|
|
await _organizationRepository.ReplaceAsync(organization);
|
|
|
|
await _referenceEventService.RaiseEventAsync(
|
|
new ReferenceEvent(ReferenceEventType.OrganizationEditedInStripe, organization, _currentContext));
|
|
}
|
|
}
|