From 459f37a4c630b6cb3fbb4ea889fdccceae828b44 Mon Sep 17 00:00:00 2001 From: cyprain-okeke <108260115+cyprain-okeke@users.noreply.github.com> Date: Mon, 16 Sep 2024 16:40:41 +0100 Subject: [PATCH] resolve stripe plan upgrade when payment fails (#4783) Signed-off-by: Cy Okeke --- .../UpgradeOrganizationPlanCommand.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Core/OrganizationFeatures/OrganizationSubscriptions/UpgradeOrganizationPlanCommand.cs b/src/Core/OrganizationFeatures/OrganizationSubscriptions/UpgradeOrganizationPlanCommand.cs index 33dd38833..7f463460d 100644 --- a/src/Core/OrganizationFeatures/OrganizationSubscriptions/UpgradeOrganizationPlanCommand.cs +++ b/src/Core/OrganizationFeatures/OrganizationSubscriptions/UpgradeOrganizationPlanCommand.cs @@ -231,9 +231,19 @@ public class UpgradeOrganizationPlanCommand : IUpgradeOrganizationPlanCommand } else { - paymentIntentClientSecret = await _paymentService.UpgradeFreeOrganizationAsync(organization, - newPlan, upgrade); - success = string.IsNullOrWhiteSpace(paymentIntentClientSecret); + try + { + paymentIntentClientSecret = await _paymentService.UpgradeFreeOrganizationAsync(organization, + newPlan, upgrade); + success = string.IsNullOrWhiteSpace(paymentIntentClientSecret); + } + catch + { + await _paymentService.CancelAndRecoverChargesAsync(organization); + organization.GatewayCustomerId = null; + await _organizationService.ReplaceAndUpdateCacheAsync(organization); + throw; + } } } else