From 1dc22f61d1c09f50b473802fc8e2a1f9f5e05aab Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 7 Feb 2019 17:28:09 -0500 Subject: [PATCH] catch sql FK violations on webhook tx creation --- src/Billing/Controllers/PayPalController.cs | 28 +++++++++++++-------- src/Billing/Controllers/StripeController.cs | 8 +++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Billing/Controllers/PayPalController.cs b/src/Billing/Controllers/PayPalController.cs index 2acf1f2709..c5771fa046 100644 --- a/src/Billing/Controllers/PayPalController.cs +++ b/src/Billing/Controllers/PayPalController.cs @@ -4,6 +4,7 @@ using Bit.Core.Repositories; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Newtonsoft.Json; +using System.Data.SqlClient; using System.IO; using System.Text; using System.Threading.Tasks; @@ -64,18 +65,23 @@ namespace Bit.Billing.Controllers var ids = sale.GetIdsFromCustom(); if(ids.Item1.HasValue || ids.Item2.HasValue) { - await _transactionRepository.CreateAsync(new Core.Models.Table.Transaction + try { - Amount = sale.Amount.TotalAmount, - CreationDate = sale.CreateTime, - OrganizationId = ids.Item1, - UserId = ids.Item2, - Type = TransactionType.Charge, - Gateway = GatewayType.PayPal, - GatewayId = sale.Id, - PaymentMethodType = PaymentMethodType.PayPal, - Details = sale.Id - }); + await _transactionRepository.CreateAsync(new Core.Models.Table.Transaction + { + Amount = sale.Amount.TotalAmount, + CreationDate = sale.CreateTime, + OrganizationId = ids.Item1, + UserId = ids.Item2, + Type = TransactionType.Charge, + Gateway = GatewayType.PayPal, + GatewayId = sale.Id, + PaymentMethodType = PaymentMethodType.PayPal, + Details = sale.Id + }); + } + // Catch foreign key violations because user/org could have been deleted. + catch(SqlException e) when(e.Number == 547) { } } } } diff --git a/src/Billing/Controllers/StripeController.cs b/src/Billing/Controllers/StripeController.cs index 44d6133498..d05d0dadaf 100644 --- a/src/Billing/Controllers/StripeController.cs +++ b/src/Billing/Controllers/StripeController.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Options; using Stripe; using System; using System.Collections.Generic; +using System.Data.SqlClient; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -227,7 +228,12 @@ namespace Bit.Billing.Controllers return new OkResult(); } - await _transactionRepository.CreateAsync(tx); + try + { + await _transactionRepository.CreateAsync(tx); + } + // Catch foreign key violations because user/org could have been deleted. + catch(SqlException e) when(e.Number == 547) { } } } }