1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-02 18:47:44 +01:00

webhook fixes

This commit is contained in:
Kyle Spearrin 2019-02-15 16:18:53 -05:00
parent fb21b19490
commit dee395960a
3 changed files with 63 additions and 49 deletions

View File

@ -47,7 +47,7 @@ namespace Bit.Billing.Controllers
body = await reader.ReadToEndAsync(); body = await reader.ReadToEndAsync();
} }
if(body == null) if(string.IsNullOrWhiteSpace(body))
{ {
return new BadRequestResult(); return new BadRequestResult();
} }

View File

@ -176,30 +176,45 @@ namespace Bit.Billing.Controllers
throw new Exception("Charge is null."); throw new Exception("Charge is null.");
} }
if(charge.InvoiceId == null) var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync(
GatewayType.Stripe, charge.Id);
if(chargeTransaction != null)
{ {
return new OkResult(); return new OkResult();
} }
var chargeTransaction = await _transactionRepository.GetByGatewayIdAsync( Tuple<Guid?, Guid?> ids = null;
GatewayType.Stripe, charge.Id); Subscription subscription = null;
if(chargeTransaction == null) var subscriptionService = new SubscriptionService();
if(charge.InvoiceId != null)
{ {
var invoiceService = new InvoiceService(); var invoiceService = new InvoiceService();
var invoice = await invoiceService.GetAsync(charge.InvoiceId); var invoice = await invoiceService.GetAsync(charge.InvoiceId);
if(invoice == null) if(invoice?.SubscriptionId != null)
{ {
return new OkResult(); subscription = await subscriptionService.GetAsync(invoice.SubscriptionId);
ids = GetIdsFromMetaData(subscription?.Metadata);
}
} }
var subscriptionService = new SubscriptionService(); if(subscription == null || ids == null || (ids.Item1.HasValue && ids.Item2.HasValue))
var subscription = await subscriptionService.GetAsync(invoice.SubscriptionId);
if(subscription == null)
{ {
return new OkResult(); var subscriptions = await subscriptionService.ListAsync(new SubscriptionListOptions
{
CustomerId = charge.CustomerId
});
foreach(var sub in subscriptions)
{
ids = GetIdsFromMetaData(sub.Metadata);
if(ids.Item1.HasValue || ids.Item2.HasValue)
{
subscription = sub;
break;
}
}
} }
var ids = GetIdsFromMetaData(subscription.Metadata);
if(ids.Item1.HasValue || ids.Item2.HasValue) if(ids.Item1.HasValue || ids.Item2.HasValue)
{ {
var tx = new Transaction var tx = new Transaction
@ -236,7 +251,6 @@ namespace Bit.Billing.Controllers
catch(SqlException e) when(e.Number == 547) { } catch(SqlException e) when(e.Number == 547) { }
} }
} }
}
else if(parsedEvent.Type.Equals("charge.refunded")) else if(parsedEvent.Type.Equals("charge.refunded"))
{ {
if(!(parsedEvent.Data.Object is Charge charge)) if(!(parsedEvent.Data.Object is Charge charge))
@ -317,7 +331,7 @@ namespace Bit.Billing.Controllers
private Tuple<Guid?, Guid?> GetIdsFromMetaData(IDictionary<string, string> metaData) private Tuple<Guid?, Guid?> GetIdsFromMetaData(IDictionary<string, string> metaData)
{ {
if(metaData == null) if(metaData == null || !metaData.Any())
{ {
return new Tuple<Guid?, Guid?>(null, null); return new Tuple<Guid?, Guid?>(null, null);
} }

View File

@ -18,7 +18,7 @@
}, },
"billingSettings": { "billingSettings": {
"payPal": { "payPal": {
"production": false "production": true
} }
} }
} }