Skip to content

Commit

Permalink
changes for restarting subscription
Browse files Browse the repository at this point in the history
Signed-off-by: Cy Okeke <[email protected]>
  • Loading branch information
cyprain-okeke committed Dec 20, 2024
1 parent 11325c4 commit 6ada37d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
37 changes: 36 additions & 1 deletion src/Api/Billing/Controllers/OrganizationBillingController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#nullable enable
using Bit.Api.AdminConsole.Models.Request.Organizations;
using Bit.Api.Billing.Models.Requests;
using Bit.Api.Billing.Models.Responses;
using Bit.Core;
using Bit.Core.Billing.Models.Sales;
using Bit.Core.Billing.Services;
using Bit.Core.Context;
using Bit.Core.Repositories;
Expand All @@ -21,7 +23,8 @@ public class OrganizationBillingController(
IOrganizationRepository organizationRepository,
IPaymentService paymentService,
ISubscriberService subscriberService,
IPaymentHistoryService paymentHistoryService) : BaseBillingController
IPaymentHistoryService paymentHistoryService,
IUserService userService) : BaseBillingController
{
[HttpGet("metadata")]
public async Task<IResult> GetMetadataAsync([FromRoute] Guid organizationId)
Expand Down Expand Up @@ -278,4 +281,36 @@ public async Task<IResult> UpdateTaxInformationAsync(

return TypedResults.Ok();
}

[HttpPost("restart-subscription")]
public async Task<IResult> RestartSubscriptionAsync([FromRoute] Guid organizationId, [FromBody] OrganizationCreateRequestModel model)
{
var user = await userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}

if (!featureService.IsEnabled(FeatureFlagKeys.AC2476_DeprecateStripeSourcesAPI))
{
return Error.NotFound();
}

if (!await currentContext.EditPaymentMethods(organizationId))
{
return Error.Unauthorized();
}

var organization = await organizationRepository.GetByIdAsync(organizationId);

if (organization == null)
{
return Error.NotFound();
}
var organizationSignup = model.ToOrganizationSignup(user);
var sale = OrganizationSale.From(organization, organizationSignup);
await organizationBillingService.Finalize(sale);

return TypedResults.Ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ public record OrganizationMetadataResponse(
bool IsManaged,
bool IsOnSecretsManagerStandalone,
bool IsSubscriptionUnpaid,
bool HasSubscription)
bool HasSubscription,
bool IsSubscriptionCanceled)
{
public static OrganizationMetadataResponse From(OrganizationMetadata metadata)
=> new(
metadata.IsEligibleForSelfHost,
metadata.IsManaged,
metadata.IsOnSecretsManagerStandalone,
metadata.IsSubscriptionUnpaid,
metadata.HasSubscription);
metadata.HasSubscription,
metadata.IsSubscriptionCanceled);
}
3 changes: 2 additions & 1 deletion src/Core/Billing/Models/OrganizationMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public record OrganizationMetadata(
bool IsManaged,
bool IsOnSecretsManagerStandalone,
bool IsSubscriptionUnpaid,
bool HasSubscription);
bool HasSubscription,
bool IsSubscriptionCanceled);
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class OrganizationBillingService(
IOrganizationRepository organizationRepository,
ISetupIntentCache setupIntentCache,
IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : IOrganizationBillingService
ISubscriberService subscriberService,
IPaymentService paymentService,

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Run tests

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Quality scan

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (win-x64)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (osx-x64)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (linux-x64)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Setup, ./util)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (MsSqlMigratorUtility, ./util, true)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Api, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Notifications, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (EventsProcessor, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Events, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Identity, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Icons, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Sso, ./bitwarden_license/src, true)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Scim, ./bitwarden_license/src, true)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Billing, ./src)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Admin, ./src, true)

Parameter 'paymentService' is unread.

Check warning on line 32 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Upload

Parameter 'paymentService' is unread.
IFeatureService featureService) : IOrganizationBillingService

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Run tests

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Quality scan

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (win-x64)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (osx-x64)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build MSSQL migrator utility (linux-x64)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Setup, ./util)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (MsSqlMigratorUtility, ./util, true)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Api, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Notifications, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (EventsProcessor, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Events, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Identity, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Icons, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Sso, ./bitwarden_license/src, true)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Scim, ./bitwarden_license/src, true)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Billing, ./src)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Build artifacts (Admin, ./src, true)

Parameter 'featureService' is unread.

Check warning on line 33 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Upload

Parameter 'featureService' is unread.
{
public async Task Finalize(OrganizationSale sale)
{
Expand Down Expand Up @@ -68,7 +70,7 @@ public async Task Finalize(OrganizationSale sale)
if (string.IsNullOrWhiteSpace(organization.GatewaySubscriptionId))
{
return new OrganizationMetadata(isEligibleForSelfHost, isManaged, false,
false, false);
false, false, false);
}

var customer = await subscriberService.GetCustomer(organization,
Expand All @@ -77,10 +79,11 @@ public async Task Finalize(OrganizationSale sale)
var subscription = await subscriberService.GetSubscription(organization);
var isOnSecretsManagerStandalone = IsOnSecretsManagerStandalone(organization, customer, subscription);
var isSubscriptionUnpaid = IsSubscriptionUnpaid(subscription);
var isSubscriptionCanceled = IsSubscriptionCanceled(subscription);
var hasSubscription = true;

return new OrganizationMetadata(isEligibleForSelfHost, isManaged, isOnSecretsManagerStandalone,
isSubscriptionUnpaid, hasSubscription);
isSubscriptionUnpaid, hasSubscription, isSubscriptionCanceled);
}

public async Task UpdatePaymentMethod(
Expand Down Expand Up @@ -393,6 +396,16 @@ private static bool IsSubscriptionUnpaid(Subscription subscription)
return subscription.Status == "unpaid";
}

private static bool IsSubscriptionCanceled(Subscription subscription)
{
if (subscription == null)
{
return false;
}

return subscription.Status == "canceled";
}


#endregion
}

0 comments on commit 6ada37d

Please sign in to comment.