From e2937c20f9d0e57f513dfa977bda1b6985b77761 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 11 Apr 2017 13:04:37 -0400 Subject: [PATCH] refund charges if signup fails --- .../Implementations/OrganizationService.cs | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index cea64fd9dd..c2737ef759 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -514,7 +514,19 @@ namespace Bit.Core.Services }); } - subscription = await subscriptionService.CreateAsync(customer.Id, subCreateOptions); + try + { + subscription = await subscriptionService.CreateAsync(customer.Id, subCreateOptions); + } + catch(StripeException) + { + if(customer != null) + { + await customerService.DeleteAsync(customer.Id); + } + + throw; + } } var organization = new Organization @@ -542,8 +554,8 @@ namespace Bit.Core.Services UserId = signup.Owner.Id, Email = signup.Owner.Email, Key = signup.OwnerKey, - Type = Enums.OrganizationUserType.Owner, - Status = Enums.OrganizationUserStatusType.Confirmed, + Type = OrganizationUserType.Owner, + Status = OrganizationUserStatusType.Confirmed, CreationDate = DateTime.UtcNow, RevisionDate = DateTime.UtcNow }; @@ -556,10 +568,24 @@ namespace Bit.Core.Services { if(subscription != null) { - await subscriptionService.CancelAsync(subscription.Id); + await subscriptionService.CancelAsync(subscription.Id, false); } - // TODO: reverse payments + if(customer != null) + { + var chargeService = new StripeChargeService(); + var charges = await chargeService.ListAsync(new StripeChargeListOptions { CustomerId = customer.Id }); + if(charges?.Data != null) + { + var refundService = new StripeRefundService(); + foreach(var charge in charges.Data.Where(c => !c.Refunded)) + { + await refundService.CreateAsync(charge.Id); + } + } + + await customerService.DeleteAsync(customer.Id); + } if(organization.Id != default(Guid)) {