Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AC-2379] Stripe webhook update provider status #4012

Closed
Expand Up @@ -650,6 +650,38 @@
await _applicationCacheService.DeleteProviderAbilityAsync(provider.Id);
}

public async Task EnableAsync(Guid providerId)
{
var provider = await _providerRepository.GetByIdAsync(providerId);

Check warning on line 655 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L654-L655

Added lines #L654 - L655 were not covered by tests

if (provider is not { Enabled: false, Gateway: not null })
{
return;

Check warning on line 659 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L658-L659

Added lines #L658 - L659 were not covered by tests
}

provider.Enabled = true;
provider.RevisionDate = DateTime.UtcNow;

Check warning on line 663 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L662-L663

Added lines #L662 - L663 were not covered by tests

await _providerRepository.ReplaceAsync(provider);
await _applicationCacheService.UpsertProviderAbilityAsync(provider);
}

Check warning on line 667 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L665-L667

Added lines #L665 - L667 were not covered by tests

public async Task DisableAsync(Guid providerId)
{
var provider = await _providerRepository.GetByIdAsync(providerId);

Check warning on line 671 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L670-L671

Added lines #L670 - L671 were not covered by tests

if (provider is not { Enabled: true })
{
return;

Check warning on line 675 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L674-L675

Added lines #L674 - L675 were not covered by tests
}

provider.Enabled = false;
provider.RevisionDate = DateTime.UtcNow;

Check warning on line 679 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L678-L679

Added lines #L678 - L679 were not covered by tests

await _providerRepository.ReplaceAsync(provider);
await _applicationCacheService.UpsertProviderAbilityAsync(provider);
}

Check warning on line 683 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L681-L683

Added lines #L681 - L683 were not covered by tests

private async Task SendInviteAsync(ProviderUser providerUser, Provider provider)
{
var nowMillis = CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow);
Expand Down
1 change: 1 addition & 0 deletions src/Billing/Billing.csproj
Expand Up @@ -7,6 +7,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>
Expand Down
36 changes: 24 additions & 12 deletions src/Billing/Controllers/StripeController.cs
Expand Up @@ -3,6 +3,7 @@
using Bit.Billing.Services;
using Bit.Core;
using Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Services;
using Bit.Core.Billing.Constants;
using Bit.Core.Context;
using Bit.Core.Enums;
Expand Down Expand Up @@ -55,6 +56,7 @@
private readonly IStripeEventService _stripeEventService;
private readonly IStripeFacade _stripeFacade;
private readonly IFeatureService _featureService;
private readonly IProviderService _providerService;

public StripeController(
GlobalSettings globalSettings,
Expand All @@ -74,7 +76,8 @@
ICurrentContext currentContext,
IStripeEventService stripeEventService,
IStripeFacade stripeFacade,
IFeatureService featureService)
IFeatureService featureService,
IProviderService providerService)

Check warning on line 80 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L79-L80

Added lines #L79 - L80 were not covered by tests
{
_billingSettings = billingSettings?.Value;
_hostingEnvironment = hostingEnvironment;
Expand Down Expand Up @@ -102,6 +105,7 @@
_stripeEventService = stripeEventService;
_stripeFacade = stripeFacade;
_featureService = featureService;
_providerService = providerService;

Check warning on line 108 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L108

Added line #L108 was not covered by tests
}

[HttpPost("webhook")]
Expand Down Expand Up @@ -224,13 +228,8 @@
await _organizationService.DisableAsync(organizationId.Value, subscription.CurrentPeriodEnd);
break;
}
case StripeSubscriptionStatus.Unpaid or StripeSubscriptionStatus.IncompleteExpired:
case StripeSubscriptionStatus.Unpaid or StripeSubscriptionStatus.IncompleteExpired when userId.HasValue:
{
if (!userId.HasValue)
{
break;
}

if (subscription.Status is StripeSubscriptionStatus.Unpaid &&
subscription.Items.Any(i => i.Price.Id is PremiumPlanId or PremiumPlanIdAppStore))
{
Expand All @@ -240,19 +239,27 @@

await _userService.DisablePremiumAsync(userId.Value, subscription.CurrentPeriodEnd);

break;

Check warning on line 242 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L242

Added line #L242 was not covered by tests
}
case StripeSubscriptionStatus.Unpaid or StripeSubscriptionStatus.IncompleteExpired when providerId.HasValue:
{
await _providerService.DisableAsync(providerId.Value);

Check warning on line 246 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L245-L246

Added lines #L245 - L246 were not covered by tests
break;
}
case StripeSubscriptionStatus.Active when organizationId.HasValue:
{
await _organizationService.EnableAsync(organizationId.Value);
break;
}
case StripeSubscriptionStatus.Active:
case StripeSubscriptionStatus.Active when userId.HasValue:
{
if (userId.HasValue)
{
await _userService.EnablePremiumAsync(userId.Value, subscription.CurrentPeriodEnd);
}
await _userService.EnablePremiumAsync(userId.Value, subscription.CurrentPeriodEnd);

Check warning on line 256 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L256

Added line #L256 was not covered by tests

break;

Check warning on line 258 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L258

Added line #L258 was not covered by tests
}
case StripeSubscriptionStatus.Active when providerId.HasValue:
{
await _providerService.EnableAsync(providerId.Value);

Check warning on line 262 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L261-L262

Added lines #L261 - L262 were not covered by tests

break;
}
Expand All @@ -272,6 +279,7 @@
{
await _userService.UpdatePremiumExpirationAsync(userId.Value, subscription.CurrentPeriodEnd);
}
// No need to update the expiration date for providers as they don't have one
}

/// <summary>
Expand Down Expand Up @@ -355,6 +363,10 @@
{
await _userService.DisablePremiumAsync(userId.Value, subscription.CurrentPeriodEnd);
}
else if (providerId.HasValue)
{
await _providerService.DisableAsync(providerId.Value);
}

Check warning on line 369 in src/Billing/Controllers/StripeController.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Controllers/StripeController.cs#L367-L369

Added lines #L367 - L369 were not covered by tests
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Billing/Startup.cs
@@ -1,6 +1,7 @@
using System.Globalization;
using Bit.Billing.Services;
using Bit.Billing.Services.Implementations;
using Bit.Commercial.Core.Utilities;
using Bit.Core.Context;
using Bit.Core.SecretsManager.Repositories;
using Bit.Core.SecretsManager.Repositories.Noop;
Expand Down Expand Up @@ -66,6 +67,12 @@
// TODO: no longer be required - see PM-1880
services.AddScoped<IServiceAccountRepository, NoopServiceAccountRepository>();

#if OSS
services.AddOosServices();
#else
services.AddCommercialCoreServices();

Check warning on line 73 in src/Billing/Startup.cs

View check run for this annotation

Codecov / codecov/patch

src/Billing/Startup.cs#L73

Added line #L73 was not covered by tests
#endif

// Mvc
services.AddMvc(config =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Core/AdminConsole/Entities/Provider/Provider.cs
Expand Up @@ -29,7 +29,7 @@ public class Provider : ITableObject<Guid>, ISubscriber
public ProviderType Type { get; set; }
public bool Enabled { get; set; } = true;
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; set; } = DateTime.UtcNow;
public GatewayType? Gateway { get; set; }
public string GatewayCustomerId { get; set; }
public string GatewaySubscriptionId { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Core/AdminConsole/Services/IProviderService.cs
Expand Up @@ -29,5 +29,7 @@ public interface IProviderService
Task InitiateDeleteAsync(Provider provider, string providerAdminEmail);
Task DeleteAsync(Provider provider, string token);
Task DeleteAsync(Provider provider);
Task EnableAsync(Guid providerId);
Task DisableAsync(Guid providerId);
}

Expand Up @@ -38,4 +38,6 @@
public Task InitiateDeleteAsync(Provider provider, string providerAdminEmail) => throw new NotImplementedException();
public Task DeleteAsync(Provider provider, string token) => throw new NotImplementedException();
public Task DeleteAsync(Provider provider) => throw new NotImplementedException();
public Task EnableAsync(Guid providerId) => throw new NotImplementedException();
public Task DisableAsync(Guid providerId) => throw new NotImplementedException();

Check warning on line 42 in src/Core/AdminConsole/Services/NoopImplementations/NoopProviderService.cs

View check run for this annotation

Codecov / codecov/patch

src/Core/AdminConsole/Services/NoopImplementations/NoopProviderService.cs#L41-L42

Added lines #L41 - L42 were not covered by tests
}