2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Api.Models.Response;
|
2022-04-04 11:40:28 -05:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-04-04 11:40:28 -05:00
|
|
|
|
[Route("accounts/billing")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class AccountsBillingController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IPaymentService _paymentService;
|
|
|
|
|
|
private readonly IUserService _userService;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-04-04 11:40:28 -05:00
|
|
|
|
public AccountsBillingController(
|
|
|
|
|
|
IPaymentService paymentService,
|
|
|
|
|
|
IUserService userService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_paymentService = paymentService;
|
|
|
|
|
|
_userService = userService;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2022-04-04 11:40:28 -05:00
|
|
|
|
|
|
|
|
|
|
[HttpGet("history")]
|
|
|
|
|
|
[SelfHosted(NotSelfHostedOnly = true)]
|
|
|
|
|
|
public async Task<BillingHistoryResponseModel> GetBillingHistory()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-04-04 11:40:28 -05:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UnauthorizedAccessException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var billingInfo = await _paymentService.GetBillingHistoryAsync(user);
|
|
|
|
|
|
return new BillingHistoryResponseModel(billingInfo);
|
|
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2022-04-04 11:40:28 -05:00
|
|
|
|
[HttpGet("payment-method")]
|
|
|
|
|
|
[SelfHosted(NotSelfHostedOnly = true)]
|
|
|
|
|
|
public async Task<BillingPaymentResponseModel> GetPaymentMethod()
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2022-04-04 11:40:28 -05:00
|
|
|
|
var user = await _userService.GetUserByPrincipalAsync(User);
|
|
|
|
|
|
if (user == null)
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2022-04-04 11:40:28 -05:00
|
|
|
|
throw new UnauthorizedAccessException();
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2022-04-04 11:40:28 -05:00
|
|
|
|
var billingInfo = await _paymentService.GetBillingBalanceAndSourceAsync(user);
|
|
|
|
|
|
return new BillingPaymentResponseModel(billingInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|