mirror of
https://github.com/bitwarden/server
synced 2025-12-22 19:23:45 +00:00
[PM-21821] Provider portal takeover states (#6109)
* Add feature flag * Disable provider and schedule cancellation when subscription goes unpaid * Run dotnet format * Only set provider subscription cancel_at when subscription is going from paid to unpaid * Update tests
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Billing' " />
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\bitwarden_license\src\Commercial.Core\Commercial.Core.csproj" />
|
||||
<ProjectReference Include="..\SharedWeb\SharedWeb.csproj" />
|
||||
<ProjectReference Include="..\Core\Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#nullable disable
|
||||
|
||||
using Stripe;
|
||||
using Stripe.TestHelpers;
|
||||
|
||||
namespace Bit.Billing.Services;
|
||||
|
||||
@@ -98,4 +99,10 @@ public interface IStripeFacade
|
||||
string subscriptionId,
|
||||
RequestOptions requestOptions = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task<TestClock> GetTestClock(
|
||||
string testClockId,
|
||||
TestClockGetOptions testClockGetOptions = null,
|
||||
RequestOptions requestOptions = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
#nullable disable
|
||||
|
||||
using Stripe;
|
||||
using Stripe.TestHelpers;
|
||||
using CustomerService = Stripe.CustomerService;
|
||||
|
||||
namespace Bit.Billing.Services.Implementations;
|
||||
|
||||
@@ -14,6 +16,7 @@ public class StripeFacade : IStripeFacade
|
||||
private readonly PaymentMethodService _paymentMethodService = new();
|
||||
private readonly SubscriptionService _subscriptionService = new();
|
||||
private readonly DiscountService _discountService = new();
|
||||
private readonly TestClockService _testClockService = new();
|
||||
|
||||
public async Task<Charge> GetCharge(
|
||||
string chargeId,
|
||||
@@ -119,4 +122,11 @@ public class StripeFacade : IStripeFacade
|
||||
RequestOptions requestOptions = null,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
await _discountService.DeleteSubscriptionDiscountAsync(subscriptionId, requestOptions, cancellationToken);
|
||||
|
||||
public Task<TestClock> GetTestClock(
|
||||
string testClockId,
|
||||
TestClockGetOptions testClockGetOptions = null,
|
||||
RequestOptions requestOptions = null,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
_testClockService.GetAsync(testClockId, testClockGetOptions, requestOptions, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using Bit.Billing.Constants;
|
||||
using Bit.Billing.Jobs;
|
||||
using Bit.Core;
|
||||
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.AdminConsole.Services;
|
||||
using Bit.Core.Billing.Pricing;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Platform.Push;
|
||||
@@ -8,6 +11,7 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Quartz;
|
||||
using Stripe;
|
||||
using Stripe.TestHelpers;
|
||||
using Event = Stripe.Event;
|
||||
|
||||
namespace Bit.Billing.Services.Implementations;
|
||||
@@ -26,6 +30,10 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
private readonly IOrganizationEnableCommand _organizationEnableCommand;
|
||||
private readonly IOrganizationDisableCommand _organizationDisableCommand;
|
||||
private readonly IPricingClient _pricingClient;
|
||||
private readonly IFeatureService _featureService;
|
||||
private readonly IProviderRepository _providerRepository;
|
||||
private readonly IProviderService _providerService;
|
||||
private readonly ILogger<SubscriptionUpdatedHandler> _logger;
|
||||
|
||||
public SubscriptionUpdatedHandler(
|
||||
IStripeEventService stripeEventService,
|
||||
@@ -39,7 +47,11 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
ISchedulerFactory schedulerFactory,
|
||||
IOrganizationEnableCommand organizationEnableCommand,
|
||||
IOrganizationDisableCommand organizationDisableCommand,
|
||||
IPricingClient pricingClient)
|
||||
IPricingClient pricingClient,
|
||||
IFeatureService featureService,
|
||||
IProviderRepository providerRepository,
|
||||
IProviderService providerService,
|
||||
ILogger<SubscriptionUpdatedHandler> logger)
|
||||
{
|
||||
_stripeEventService = stripeEventService;
|
||||
_stripeEventUtilityService = stripeEventUtilityService;
|
||||
@@ -53,6 +65,10 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
_organizationEnableCommand = organizationEnableCommand;
|
||||
_organizationDisableCommand = organizationDisableCommand;
|
||||
_pricingClient = pricingClient;
|
||||
_featureService = featureService;
|
||||
_providerRepository = providerRepository;
|
||||
_providerService = providerService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,7 +77,7 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
/// <param name="parsedEvent"></param>
|
||||
public async Task HandleAsync(Event parsedEvent)
|
||||
{
|
||||
var subscription = await _stripeEventService.GetSubscription(parsedEvent, true, ["customer", "discounts", "latest_invoice"]);
|
||||
var subscription = await _stripeEventService.GetSubscription(parsedEvent, true, ["customer", "discounts", "latest_invoice", "test_clock"]);
|
||||
var (organizationId, userId, providerId) = _stripeEventUtilityService.GetIdsFromMetadata(subscription.Metadata);
|
||||
|
||||
switch (subscription.Status)
|
||||
@@ -77,6 +93,11 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
}
|
||||
break;
|
||||
}
|
||||
case StripeSubscriptionStatus.Unpaid or StripeSubscriptionStatus.IncompleteExpired when providerId.HasValue:
|
||||
{
|
||||
await HandleUnpaidProviderSubscriptionAsync(providerId.Value, parsedEvent, subscription);
|
||||
break;
|
||||
}
|
||||
case StripeSubscriptionStatus.Unpaid or StripeSubscriptionStatus.IncompleteExpired:
|
||||
{
|
||||
if (!userId.HasValue)
|
||||
@@ -238,4 +259,71 @@ public class SubscriptionUpdatedHandler : ISubscriptionUpdatedHandler
|
||||
|
||||
await scheduler.ScheduleJob(job, trigger);
|
||||
}
|
||||
|
||||
private async Task HandleUnpaidProviderSubscriptionAsync(
|
||||
Guid providerId,
|
||||
Event parsedEvent,
|
||||
Subscription subscription)
|
||||
{
|
||||
var providerPortalTakeover = _featureService.IsEnabled(FeatureFlagKeys.PM21821_ProviderPortalTakeover);
|
||||
|
||||
if (!providerPortalTakeover)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var provider = await _providerRepository.GetByIdAsync(providerId);
|
||||
if (provider == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
provider.Enabled = false;
|
||||
await _providerService.UpdateAsync(provider);
|
||||
|
||||
if (parsedEvent.Data.PreviousAttributes != null)
|
||||
{
|
||||
if (parsedEvent.Data.PreviousAttributes.ToObject<Subscription>() as Subscription is
|
||||
{
|
||||
Status:
|
||||
StripeSubscriptionStatus.Trialing or
|
||||
StripeSubscriptionStatus.Active or
|
||||
StripeSubscriptionStatus.PastDue
|
||||
} && subscription is
|
||||
{
|
||||
Status: StripeSubscriptionStatus.Unpaid,
|
||||
LatestInvoice.BillingReason: "subscription_cycle" or "subscription_create"
|
||||
})
|
||||
{
|
||||
if (subscription.TestClock != null)
|
||||
{
|
||||
await WaitForTestClockToAdvanceAsync(subscription.TestClock);
|
||||
}
|
||||
|
||||
var now = subscription.TestClock?.FrozenTime ?? DateTime.UtcNow;
|
||||
await _stripeFacade.UpdateSubscription(subscription.Id,
|
||||
new SubscriptionUpdateOptions { CancelAt = now.AddDays(7) });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_logger.LogError(exception, "An error occurred while trying to disable and schedule subscription cancellation for provider ({ProviderID})", providerId);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task WaitForTestClockToAdvanceAsync(TestClock testClock)
|
||||
{
|
||||
while (testClock.Status != "ready")
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(2));
|
||||
testClock = await _stripeFacade.GetTestClock(testClock.Id);
|
||||
if (testClock.Status == "internal_failure")
|
||||
{
|
||||
throw new Exception("Stripe Test Clock encountered an internal failure");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Globalization;
|
||||
using System.Net.Http.Headers;
|
||||
using Bit.Billing.Services;
|
||||
using Bit.Billing.Services.Implementations;
|
||||
using Bit.Commercial.Core.Utilities;
|
||||
using Bit.Core.Billing.Extensions;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.SecretsManager.Repositories;
|
||||
@@ -83,6 +84,7 @@ public class Startup
|
||||
services.AddDefaultServices(globalSettings);
|
||||
services.AddDistributedCache(globalSettings);
|
||||
services.AddBillingOperations();
|
||||
services.AddCommercialCoreServices();
|
||||
|
||||
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
||||
|
||||
@@ -159,6 +159,7 @@ public static class FeatureFlagKeys
|
||||
public const string PM21092_SetNonUSBusinessUseToReverseCharge = "pm-21092-set-non-us-business-use-to-reverse-charge";
|
||||
public const string PM21383_GetProviderPriceFromStripe = "pm-21383-get-provider-price-from-stripe";
|
||||
public const string PM21881_ManagePaymentDetailsOutsideCheckout = "pm-21881-manage-payment-details-outside-checkout";
|
||||
public const string PM21821_ProviderPortalTakeover = "pm-21821-provider-portal-takeover";
|
||||
|
||||
/* Key Management Team */
|
||||
public const string ReturnErrorOnExistingKeypair = "return-error-on-existing-keypair";
|
||||
|
||||
Reference in New Issue
Block a user