1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

[fix] Payment Failed webhook fix (#2076)

This commit is contained in:
Addison Beck 2022-06-24 14:20:32 -07:00 committed by GitHub
parent 75ab5826e0
commit fe062dc987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,6 +28,7 @@ namespace Bit.Billing.Controllers
{
private const decimal PremiumPlanAppleIapPrice = 14.99M;
private const string PremiumPlanId = "premium-annually";
private const string PremiumPlanIdAppStore = "premium-annually-app";
private readonly BillingSettings _billingSettings;
private readonly IWebHostEnvironment _hostingEnvironment;
@ -437,7 +438,7 @@ namespace Bit.Billing.Controllers
}
else if (parsedEvent.Type.Equals(HandledStripeWebhook.PaymentFailed))
{
await HandlePaymentFailed(await GetInvoiceAsync(parsedEvent, true), await GetSubscriptionAsync(parsedEvent));
await HandlePaymentFailed(await GetInvoiceAsync(parsedEvent, true));
}
else if (parsedEvent.Type.Equals(HandledStripeWebhook.InvoiceCreated))
{
@ -802,12 +803,14 @@ namespace Bit.Billing.Controllers
private static bool IsSponsoredSubscription(Subscription subscription) =>
StaticStore.SponsoredPlans.Any(p => p.StripePlanId == subscription.Id);
private async Task HandlePaymentFailed(Invoice invoice, Subscription subscription)
private async Task HandlePaymentFailed(Invoice invoice)
{
if (!invoice.Paid && invoice.AttemptCount > 1 && UnpaidAutoChargeInvoiceForSubscriptionCycle(invoice))
{
var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(invoice.SubscriptionId);
// attempt count 4 = 11 days after initial failure
if (invoice.AttemptCount > 3 && subscription.Items.Any(i => i.Plan.Id == PremiumPlanId))
if (invoice.AttemptCount > 3 && subscription.Items.Any(i => i.Price.Id == PremiumPlanId || i.Price.Id == PremiumPlanIdAppStore))
{
await CancelSubscription(invoice.SubscriptionId);
await VoidOpenInvoices(invoice.SubscriptionId);