1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 00:22:50 -05:00

apis for subscription vs billing

This commit is contained in:
Kyle Spearrin
2019-02-18 15:40:47 -05:00
parent 5945c39b32
commit b036657d78
14 changed files with 353 additions and 340 deletions

View File

@ -469,6 +469,7 @@ namespace Bit.Api.Controllers
}
[HttpGet("billing")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<BillingResponseModel> GetBilling()
{
var user = await _userService.GetUserByPrincipalAsync(User);
@ -477,20 +478,33 @@ namespace Bit.Api.Controllers
throw new UnauthorizedAccessException();
}
var billingInfo = await _paymentService.GetBillingAsync(user);
return new BillingResponseModel(billingInfo);
}
[HttpGet("subscription")]
public async Task<SubscriptionResponseModel> GetSubscription()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
if(!_globalSettings.SelfHosted && user.Gateway != null)
{
var billingInfo = await _paymentService.GetBillingAsync(user);
var license = await _userService.GenerateLicenseAsync(user, billingInfo);
return new BillingResponseModel(user, billingInfo, license);
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(user);
var license = await _userService.GenerateLicenseAsync(user, subscriptionInfo);
return new SubscriptionResponseModel(user, subscriptionInfo, license);
}
else if(!_globalSettings.SelfHosted)
{
var license = await _userService.GenerateLicenseAsync(user);
return new BillingResponseModel(user, license);
return new SubscriptionResponseModel(user, license);
}
else
{
return new BillingResponseModel(user);
return new SubscriptionResponseModel(user);
}
}

View File

@ -10,8 +10,6 @@ using Bit.Core.Services;
using Bit.Core;
using Bit.Api.Utilities;
using Bit.Core.Models.Business;
using Stripe;
using Microsoft.Extensions.Options;
using Bit.Core.Utilities;
namespace Bit.Api.Controllers
@ -65,7 +63,27 @@ namespace Bit.Api.Controllers
}
[HttpGet("{id}/billing")]
public async Task<OrganizationBillingResponseModel> GetBilling(string id)
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<BillingResponseModel> GetBilling(string id)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
{
throw new NotFoundException();
}
var billingInfo = await _paymentService.GetBillingAsync(organization);
return new BillingResponseModel(billingInfo);
}
[HttpGet("{id}/subscription")]
public async Task<OrganizationSubscriptionResponseModel> GetSubscription(string id)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
@ -81,49 +99,19 @@ namespace Bit.Api.Controllers
if(!_globalSettings.SelfHosted && organization.Gateway != null)
{
var billingInfo = await _paymentService.GetBillingAsync(organization);
if(billingInfo == null)
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(organization);
if(subscriptionInfo == null)
{
throw new NotFoundException();
}
return new OrganizationBillingResponseModel(organization, billingInfo);
return new OrganizationSubscriptionResponseModel(organization, subscriptionInfo);
}
else
{
return new OrganizationBillingResponseModel(organization);
return new OrganizationSubscriptionResponseModel(organization);
}
}
[HttpGet("{id}/billing-invoice/{invoiceId}")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<IActionResult> GetBillingInvoice(string id, string invoiceId)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
if(organization == null)
{
throw new NotFoundException();
}
try
{
var invoice = await new InvoiceService().GetAsync(invoiceId);
if(invoice != null && invoice.CustomerId == organization.GatewayCustomerId &&
!string.IsNullOrWhiteSpace(invoice.HostedInvoiceUrl))
{
return new RedirectResult(invoice.HostedInvoiceUrl);
}
}
catch(StripeException) { }
throw new NotFoundException();
}
[HttpGet("{id}/license")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<OrganizationLicense> GetLicense(string id, [FromQuery]Guid installationId)