1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

[feat] Allow CS to perform bulk actions on Stripe subscriptions from the Admin portal (#2116)

* [feat] Allow CS to perform bulk actions on Stripe subscriptions from the Admin portal

* [fix] An unrelated lint error
This commit is contained in:
Addison Beck
2022-07-13 10:04:58 -04:00
committed by GitHub
parent 4b43951b59
commit c5852db6ed
7 changed files with 510 additions and 6 deletions

View File

@ -0,0 +1,41 @@
namespace Bit.Core.Models.BitStripe
{
// Stripe's SubscriptionListOptions model has a complex input for date filters.
// It expects a dictionary, and has lots of validation rules around what can have a value and what can't.
// To simplify this a bit we are extending Stripe's model and using our own date inputs, and building the dictionary they expect JiT.
// ___
// Our model also facilitates selecting all elements in a list, which is unsupported by Stripe's model.
public class StripeSubscriptionListOptions : Stripe.SubscriptionListOptions
{
public DateTime? CurrentPeriodEndDate { get; set; }
public string CurrentPeriodEndRange { get; set; } = "lt";
public bool SelectAll { get; set; }
public new Stripe.DateRangeOptions CurrentPeriodEnd
{
get
{
return CurrentPeriodEndDate.HasValue ?
new Stripe.DateRangeOptions()
{
LessThan = CurrentPeriodEndRange == "lt" ? CurrentPeriodEndDate : null,
GreaterThan = CurrentPeriodEndRange == "gt" ? CurrentPeriodEndDate : null
} :
null;
}
}
public Stripe.SubscriptionListOptions ToStripeApiOptions()
{
var stripeApiOptions = (Stripe.SubscriptionListOptions)this;
if (CurrentPeriodEndDate.HasValue)
{
stripeApiOptions.CurrentPeriodEnd = new Stripe.DateRangeOptions()
{
LessThan = CurrentPeriodEndRange == "lt" ? CurrentPeriodEndDate : null,
GreaterThan = CurrentPeriodEndRange == "gt" ? CurrentPeriodEndDate : null
};
}
return stripeApiOptions;
}
}
}

View File

@ -1,4 +1,6 @@
namespace Bit.Core.Services
using Bit.Core.Models.BitStripe;
namespace Bit.Core.Services
{
public interface IStripeAdapter
{
@ -8,8 +10,9 @@
Task<Stripe.Customer> CustomerDeleteAsync(string id);
Task<Stripe.Subscription> SubscriptionCreateAsync(Stripe.SubscriptionCreateOptions subscriptionCreateOptions);
Task<Stripe.Subscription> SubscriptionGetAsync(string id, Stripe.SubscriptionGetOptions options = null);
Task<List<Stripe.Subscription>> SubscriptionListAsync(StripeSubscriptionListOptions subscriptionSearchOptions);
Task<Stripe.Subscription> SubscriptionUpdateAsync(string id, Stripe.SubscriptionUpdateOptions options = null);
Task<Stripe.Subscription> SubscriptionCancelAsync(string Id, Stripe.SubscriptionCancelOptions options);
Task<Stripe.Subscription> SubscriptionCancelAsync(string Id, Stripe.SubscriptionCancelOptions options = null);
Task<Stripe.Invoice> InvoiceUpcomingAsync(Stripe.UpcomingInvoiceOptions options);
Task<Stripe.Invoice> InvoiceGetAsync(string id, Stripe.InvoiceGetOptions options);
Task<Stripe.StripeList<Stripe.Invoice>> InvoiceListAsync(Stripe.InvoiceListOptions options);
@ -31,5 +34,6 @@
Task<Stripe.Card> CardDeleteAsync(string customerId, string cardId, Stripe.CardDeleteOptions options = null);
Task<Stripe.BankAccount> BankAccountCreateAsync(string customerId, Stripe.BankAccountCreateOptions options = null);
Task<Stripe.BankAccount> BankAccountDeleteAsync(string customerId, string bankAccount, Stripe.BankAccountDeleteOptions options = null);
Task<Stripe.StripeList<Stripe.Price>> PriceListAsync(Stripe.PriceListOptions options = null);
}
}

View File

@ -1,4 +1,6 @@
namespace Bit.Core.Services
using Bit.Core.Models.BitStripe;
namespace Bit.Core.Services
{
public class StripeAdapter : IStripeAdapter
{
@ -12,6 +14,7 @@
private readonly Stripe.RefundService _refundService;
private readonly Stripe.CardService _cardService;
private readonly Stripe.BankAccountService _bankAccountService;
private readonly Stripe.PriceService _priceService;
public StripeAdapter()
{
@ -25,6 +28,7 @@
_refundService = new Stripe.RefundService();
_cardService = new Stripe.CardService();
_bankAccountService = new Stripe.BankAccountService();
_priceService = new Stripe.PriceService();
}
public Task<Stripe.Customer> CustomerCreateAsync(Stripe.CustomerCreateOptions options)
@ -63,7 +67,7 @@
return _subscriptionService.UpdateAsync(id, options);
}
public Task<Stripe.Subscription> SubscriptionCancelAsync(string Id, Stripe.SubscriptionCancelOptions options)
public Task<Stripe.Subscription> SubscriptionCancelAsync(string Id, Stripe.SubscriptionCancelOptions options = null)
{
return _subscriptionService.CancelAsync(Id, options);
}
@ -173,5 +177,26 @@
{
return _bankAccountService.DeleteAsync(customerId, bankAccount, options);
}
public async Task<List<Stripe.Subscription>> SubscriptionListAsync(StripeSubscriptionListOptions options)
{
if (!options.SelectAll)
{
return (await _subscriptionService.ListAsync(options.ToStripeApiOptions())).Data;
}
options.Limit = 100;
var items = new List<Stripe.Subscription>();
await foreach (var i in _subscriptionService.ListAutoPagingAsync(options.ToStripeApiOptions()))
{
items.Add(i);
}
return items;
}
public async Task<Stripe.StripeList<Stripe.Price>> PriceListAsync(Stripe.PriceListOptions options = null)
{
return await _priceService.ListAsync(options);
}
}
}