mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 23:52:50 -05:00
[euvr] Separate Billing Payment/History APIs (#1932)
* [euvr] Separate Billing Payment/History APIs * Formatting * Created AccountsBillingController // Deprecated GetBilling // Simplified PaymentService helpers * Formatting
This commit is contained in:
54
src/Api/Controllers/AccountsBillingController.cs
Normal file
54
src/Api/Controllers/AccountsBillingController.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Api.Models.Response;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
[Route("accounts/billing")]
|
||||
[Authorize("Application")]
|
||||
public class AccountsBillingController : Controller
|
||||
{
|
||||
private readonly IPaymentService _paymentService;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public AccountsBillingController(
|
||||
IPaymentService paymentService,
|
||||
IUserService userService)
|
||||
{
|
||||
_paymentService = paymentService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet("history")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<BillingHistoryResponseModel> GetBillingHistory()
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
if (user == null)
|
||||
{
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
var billingInfo = await _paymentService.GetBillingHistoryAsync(user);
|
||||
return new BillingHistoryResponseModel(billingInfo);
|
||||
}
|
||||
|
||||
[HttpGet("payment-method")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<BillingPaymentResponseModel> GetPaymentMethod()
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
if (user == null)
|
||||
{
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
var billingInfo = await _paymentService.GetBillingBalanceAndSourceAsync(user);
|
||||
return new BillingPaymentResponseModel(billingInfo);
|
||||
}
|
||||
}
|
||||
}
|
@ -626,6 +626,7 @@ namespace Bit.Api.Controllers
|
||||
};
|
||||
}
|
||||
|
||||
[Obsolete("2022-04-01 Use separate Billing History/Payment APIs, left for backwards compatability with older clients")]
|
||||
[HttpGet("billing")]
|
||||
[SelfHosted(NotSelfHostedOnly = true)]
|
||||
public async Task<BillingResponseModel> GetBilling()
|
||||
|
19
src/Api/Models/Response/BillingHistoryResponseModel.cs
Normal file
19
src/Api/Models/Response/BillingHistoryResponseModel.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Models.Business;
|
||||
|
||||
namespace Bit.Api.Models.Response
|
||||
{
|
||||
public class BillingHistoryResponseModel : ResponseModel
|
||||
{
|
||||
public BillingHistoryResponseModel(BillingInfo billing)
|
||||
: base("billingHistory")
|
||||
{
|
||||
Transactions = billing.Transactions?.Select(t => new BillingTransaction(t));
|
||||
Invoices = billing.Invoices?.Select(i => new BillingInvoice(i));
|
||||
}
|
||||
public IEnumerable<BillingInvoice> Invoices { get; set; }
|
||||
public IEnumerable<BillingTransaction> Transactions { get; set; }
|
||||
}
|
||||
}
|
18
src/Api/Models/Response/BillingPaymentResponseModel.cs
Normal file
18
src/Api/Models/Response/BillingPaymentResponseModel.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Models.Business;
|
||||
|
||||
namespace Bit.Api.Models.Response
|
||||
{
|
||||
public class BillingPaymentResponseModel : ResponseModel
|
||||
{
|
||||
public BillingPaymentResponseModel(BillingInfo billing)
|
||||
: base("billingPayment")
|
||||
{
|
||||
Balance = billing.Balance;
|
||||
PaymentSource = billing.PaymentSource != null ? new BillingSource(billing.PaymentSource) : null;
|
||||
}
|
||||
|
||||
public decimal Balance { get; set; }
|
||||
public BillingSource PaymentSource { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user