mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 17:12:49 -05:00
[AC-2568] Added invoices and transaction history endpoints. Added cursor paging for each (#4692)
* Added invoices and transaction history endpoints. Added cursor paging for each * Removed try/catch since it's handled by middleware. Updated condition to use pattern matching * Added unit tests for PaymentHistoryService * Removed organizationId from account billing controller endpoints
This commit is contained in:
@ -0,0 +1,53 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Billing.Models;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.BitStripe;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.Billing.Services.Implementations;
|
||||
|
||||
public class PaymentHistoryService(
|
||||
IStripeAdapter stripeAdapter,
|
||||
ITransactionRepository transactionRepository,
|
||||
ILogger<PaymentHistoryService> logger) : IPaymentHistoryService
|
||||
{
|
||||
public async Task<IEnumerable<BillingHistoryInfo.BillingInvoice>> GetInvoiceHistoryAsync(
|
||||
ISubscriber subscriber,
|
||||
int pageSize = 5,
|
||||
string startAfter = null)
|
||||
{
|
||||
if (subscriber is not { GatewayCustomerId: not null, GatewaySubscriptionId: not null })
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var invoices = await stripeAdapter.InvoiceListAsync(new StripeInvoiceListOptions
|
||||
{
|
||||
Customer = subscriber.GatewayCustomerId,
|
||||
Subscription = subscriber.GatewaySubscriptionId,
|
||||
Limit = pageSize,
|
||||
StartingAfter = startAfter
|
||||
});
|
||||
|
||||
return invoices.Select(invoice => new BillingHistoryInfo.BillingInvoice(invoice));
|
||||
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<BillingHistoryInfo.BillingTransaction>> GetTransactionHistoryAsync(
|
||||
ISubscriber subscriber,
|
||||
int pageSize = 5,
|
||||
DateTime? startAfter = null)
|
||||
{
|
||||
var transactions = subscriber switch
|
||||
{
|
||||
User => await transactionRepository.GetManyByUserIdAsync(subscriber.Id, pageSize, startAfter),
|
||||
Organization => await transactionRepository.GetManyByOrganizationIdAsync(subscriber.Id, pageSize, startAfter),
|
||||
_ => null
|
||||
};
|
||||
|
||||
return transactions?.OrderByDescending(i => i.CreationDate)
|
||||
.Select(t => new BillingHistoryInfo.BillingTransaction(t));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user