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

APIs for premium. Billing helpers.

This commit is contained in:
Kyle Spearrin
2017-07-06 14:55:58 -04:00
parent 2afef85f85
commit d346ee5169
22 changed files with 789 additions and 313 deletions

View File

@ -10,6 +10,7 @@ using Bit.Core.Models.Table;
using Bit.Core.Enums;
using System.Linq;
using Bit.Core.Repositories;
using Bit.Core.Utilities;
namespace Bit.Api.Controllers
{
@ -339,5 +340,88 @@ namespace Bit.Api.Controllers
throw new BadRequestException(ModelState);
}
[HttpPost("premium")]
public async Task<ProfileResponseModel> PostPremium([FromBody]PremiumRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SignUpPremiumAsync(user, model.PaymentToken, model.AdditionalStorageGb.GetValueOrDefault(0));
return new ProfileResponseModel(user, null);
}
[HttpGet("billing")]
public async Task<BillingResponseModel> GetBilling()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
var billingInfo = await BillingHelpers.GetBillingAsync(user);
if(billingInfo == null)
{
throw new NotFoundException();
}
return new BillingResponseModel(billingInfo);
}
[HttpPut("payment")]
[HttpPost("payment")]
public async Task PutPayment([FromBody]PaymentRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken);
}
[HttpPut("storage")]
[HttpPost("storage")]
public async Task PutStorage([FromBody]StorageRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.AdjustStorageAsync(user, model.StroageGbAdjustment.Value);
}
[HttpPut("cancel-premium")]
[HttpPost("cancel-premium")]
public async Task PutCancel()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.CancelPremiumAsync(user, true);
}
[HttpPut("reinstate-premium")]
[HttpPost("reinstate-premium")]
public async Task PutReinstate()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.ReinstatePremiumAsync(user);
}
}
}

View File

@ -10,6 +10,7 @@ using Bit.Core.Services;
using Bit.Core;
using Microsoft.AspNetCore.Identity;
using Bit.Core.Models.Table;
using Bit.Core.Utilities;
namespace Bit.Api.Controllers
{
@ -73,7 +74,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException();
}
var billingInfo = await _organizationService.GetBillingAsync(organization);
var billingInfo = await BillingHelpers.GetBillingAsync(organization);
if(billingInfo == null)
{
throw new NotFoundException();
@ -130,7 +131,7 @@ namespace Bit.Api.Controllers
[HttpPut("{id}/payment")]
[HttpPost("{id}/payment")]
public async Task PutPayment(string id, [FromBody]OrganizationPaymentRequestModel model)
public async Task PutPayment(string id, [FromBody]PaymentRequestModel model)
{
var orgIdGuid = new Guid(id);
if(!_currentContext.OrganizationOwner(orgIdGuid))