-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AC-1904] Implement endpoint to retrieve Provider subscription (#3921)
* Refactor Core.Billing prior to adding new logic * Add ProviderBillingQueries.GetSubscriptionData * Add ProviderBillingController.GetSubscriptionAsync
- Loading branch information
1 parent
46dba15
commit ffd988e
Showing
31 changed files
with
786 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Bit.Api.Billing.Models; | ||
using Bit.Core; | ||
using Bit.Core.Billing.Queries; | ||
using Bit.Core.Context; | ||
using Bit.Core.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Bit.Api.Billing.Controllers; | ||
|
||
[Route("providers/{providerId:guid}/billing")] | ||
[Authorize("Application")] | ||
public class ProviderBillingController( | ||
ICurrentContext currentContext, | ||
IFeatureService featureService, | ||
IProviderBillingQueries providerBillingQueries) : Controller | ||
{ | ||
[HttpGet("subscription")] | ||
public async Task<IResult> GetSubscriptionAsync([FromRoute] Guid providerId) | ||
{ | ||
if (!featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling)) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
if (!currentContext.ProviderProviderAdmin(providerId)) | ||
{ | ||
return TypedResults.Unauthorized(); | ||
} | ||
|
||
var subscriptionData = await providerBillingQueries.GetSubscriptionData(providerId); | ||
|
||
if (subscriptionData == null) | ||
{ | ||
return TypedResults.NotFound(); | ||
} | ||
|
||
var (providerPlans, subscription) = subscriptionData; | ||
|
||
var providerSubscriptionDTO = ProviderSubscriptionDTO.From(providerPlans, subscription); | ||
|
||
return TypedResults.Ok(providerSubscriptionDTO); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Bit.Core.Billing.Models; | ||
using Bit.Core.Utilities; | ||
using Stripe; | ||
|
||
namespace Bit.Api.Billing.Models; | ||
|
||
public record ProviderSubscriptionDTO( | ||
string Status, | ||
DateTime CurrentPeriodEndDate, | ||
decimal? DiscountPercentage, | ||
IEnumerable<ProviderPlanDTO> Plans) | ||
{ | ||
private const string _annualCadence = "Annual"; | ||
private const string _monthlyCadence = "Monthly"; | ||
|
||
public static ProviderSubscriptionDTO From( | ||
IEnumerable<ConfiguredProviderPlan> providerPlans, | ||
Subscription subscription) | ||
{ | ||
var providerPlansDTO = providerPlans | ||
.Select(providerPlan => | ||
{ | ||
var plan = StaticStore.GetPlan(providerPlan.PlanType); | ||
var cost = (providerPlan.SeatMinimum + providerPlan.PurchasedSeats) * plan.PasswordManager.SeatPrice; | ||
var cadence = plan.IsAnnual ? _annualCadence : _monthlyCadence; | ||
return new ProviderPlanDTO( | ||
plan.Name, | ||
providerPlan.SeatMinimum, | ||
providerPlan.PurchasedSeats, | ||
cost, | ||
cadence); | ||
}); | ||
|
||
return new ProviderSubscriptionDTO( | ||
subscription.Status, | ||
subscription.CurrentPeriodEnd, | ||
subscription.Customer?.Discount?.Coupon?.PercentOff, | ||
providerPlansDTO); | ||
} | ||
} | ||
|
||
public record ProviderPlanDTO( | ||
string PlanName, | ||
int SeatMinimum, | ||
int PurchasedSeats, | ||
decimal Cost, | ||
string Cadence); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Bit.Core.Billing; | ||
|
||
public class BillingException( | ||
string clientFriendlyMessage, | ||
string internalMessage = null, | ||
Exception innerException = null) : Exception(internalMessage, innerException) | ||
{ | ||
public string ClientFriendlyMessage { get; set; } = clientFriendlyMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.