1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-04 03:02:18 -05:00

load fresh sub and invoices on webhooks

This commit is contained in:
Kyle Spearrin 2019-08-20 11:09:02 -04:00
parent 7957707559
commit 669a7c00d8

View File

@ -93,11 +93,7 @@ namespace Bit.Billing.Controllers
if(subDeleted || subUpdated) if(subDeleted || subUpdated)
{ {
if(!(parsedEvent.Data.Object is Subscription subscription)) var subscription = await GetSubscriptionAsync(parsedEvent, true);
{
throw new Exception("Subscription is null. " + parsedEvent.Id);
}
var ids = GetIdsFromMetaData(subscription.Metadata); var ids = GetIdsFromMetaData(subscription.Metadata);
var subCanceled = subDeleted && subscription.Status == "canceled"; var subCanceled = subDeleted && subscription.Status == "canceled";
@ -136,11 +132,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("invoice.upcoming")) else if(parsedEvent.Type.Equals("invoice.upcoming"))
{ {
if(!(parsedEvent.Data.Object is Invoice invoice)) var invoice = await GetInvoiceAsync(parsedEvent);
{
throw new Exception("Invoice is null. " + parsedEvent.Id);
}
var subscriptionService = new SubscriptionService(); var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(invoice.SubscriptionId); var subscription = await subscriptionService.GetAsync(invoice.SubscriptionId);
if(subscription == null) if(subscription == null)
@ -178,11 +170,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("charge.succeeded")) else if(parsedEvent.Type.Equals("charge.succeeded"))
{ {
if(!(parsedEvent.Data.Object is Charge charge)) var charge = await GetChargeAsync(parsedEvent);
{
throw new Exception("Charge is null. " + parsedEvent.Id);
}
var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync( var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync(
GatewayType.Stripe, charge.Id); GatewayType.Stripe, charge.Id);
if(chargeTransaction != null) if(chargeTransaction != null)
@ -288,11 +276,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("charge.refunded")) else if(parsedEvent.Type.Equals("charge.refunded"))
{ {
if(!(parsedEvent.Data.Object is Charge charge)) var charge = await GetChargeAsync(parsedEvent);
{
throw new Exception("Charge is null. " + parsedEvent.Id);
}
var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync( var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync(
GatewayType.Stripe, charge.Id); GatewayType.Stripe, charge.Id);
if(chargeTransaction == null) if(chargeTransaction == null)
@ -342,11 +326,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("invoice.payment_succeeded")) else if(parsedEvent.Type.Equals("invoice.payment_succeeded"))
{ {
if(!(parsedEvent.Data.Object is Invoice invoice)) var invoice = await GetInvoiceAsync(parsedEvent, true);
{
throw new Exception("Invoice is null. " + parsedEvent.Id);
}
if(invoice.Paid && invoice.BillingReason == "subscription_create") if(invoice.Paid && invoice.BillingReason == "subscription_create")
{ {
var subscriptionService = new SubscriptionService(); var subscriptionService = new SubscriptionService();
@ -375,11 +355,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("invoice.payment_failed")) else if(parsedEvent.Type.Equals("invoice.payment_failed"))
{ {
if(!(parsedEvent.Data.Object is Invoice invoice)) var invoice = await GetInvoiceAsync(parsedEvent, true);
{
throw new Exception("Invoice is null. " + parsedEvent.Id);
}
if(!invoice.Paid && invoice.AttemptCount > 1 && UnpaidAutoChargeInvoiceForSubscriptionCycle(invoice)) if(!invoice.Paid && invoice.AttemptCount > 1 && UnpaidAutoChargeInvoiceForSubscriptionCycle(invoice))
{ {
await AttemptToPayInvoiceWithBraintreeAsync(invoice); await AttemptToPayInvoiceWithBraintreeAsync(invoice);
@ -387,11 +363,7 @@ namespace Bit.Billing.Controllers
} }
else if(parsedEvent.Type.Equals("invoice.created")) else if(parsedEvent.Type.Equals("invoice.created"))
{ {
if(!(parsedEvent.Data.Object is Invoice invoice)) var invoice = await GetInvoiceAsync(parsedEvent, true);
{
throw new Exception("Invoice is null. " + parsedEvent.Id);
}
if(!invoice.Paid && UnpaidAutoChargeInvoiceForSubscriptionCycle(invoice)) if(!invoice.Paid && UnpaidAutoChargeInvoiceForSubscriptionCycle(invoice))
{ {
await AttemptToPayInvoiceWithBraintreeAsync(invoice); await AttemptToPayInvoiceWithBraintreeAsync(invoice);
@ -544,5 +516,62 @@ namespace Bit.Billing.Controllers
return invoice.AmountDue > 0 && !invoice.Paid && invoice.CollectionMethod == "charge_automatically" && return invoice.AmountDue > 0 && !invoice.Paid && invoice.CollectionMethod == "charge_automatically" &&
invoice.BillingReason == "subscription_cycle" && invoice.SubscriptionId != null; invoice.BillingReason == "subscription_cycle" && invoice.SubscriptionId != null;
} }
private async Task<Charge> GetChargeAsync(Stripe.Event parsedEvent, bool fresh = false)
{
if(!(parsedEvent.Data.Object is Charge eventCharge))
{
throw new Exception("Charge is null (from parsed event). " + parsedEvent.Id);
}
if(!fresh)
{
return eventCharge;
}
var chargeService = new ChargeService();
var charge = await chargeService.GetAsync(eventCharge.Id);
if(charge == null)
{
throw new Exception("Charge is null. " + eventCharge.Id);
}
return charge;
}
private async Task<Invoice> GetInvoiceAsync(Stripe.Event parsedEvent, bool fresh = false)
{
if(!(parsedEvent.Data.Object is Invoice eventInvoice))
{
throw new Exception("Invoice is null (from parsed event). " + parsedEvent.Id);
}
if(!fresh)
{
return eventInvoice;
}
var invoiceService = new InvoiceService();
var invoice = await invoiceService.GetAsync(eventInvoice.Id);
if(invoice == null)
{
throw new Exception("Invoice is null. " + eventInvoice.Id);
}
return invoice;
}
private async Task<Subscription> GetSubscriptionAsync(Stripe.Event parsedEvent, bool fresh = false)
{
if(!(parsedEvent.Data.Object is Subscription eventSubscription))
{
throw new Exception("Subscription is null (from parsed event). " + parsedEvent.Id);
}
if(!fresh)
{
return eventSubscription;
}
var subscriptionService = new SubscriptionService();
var subscription = await subscriptionService.GetAsync(eventSubscription.Id);
if(subscription == null)
{
throw new Exception("Subscription is null. " + eventSubscription.Id);
}
return subscription;
}
} }
} }