From 4f2d1670e627d944031b86ebcb8d04583d21e46b Mon Sep 17 00:00:00 2001 From: Conner Turnbull Date: Thu, 7 Nov 2024 12:00:12 -0500 Subject: [PATCH 1/3] Updated customer metadata to only store one old btCustomerId --- src/Core/Services/Implementations/StripePaymentService.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 7eb2b402bb23..60dff6ae6b37 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1360,8 +1360,7 @@ public async Task UpdatePaymentMethodAsync(ISubscriber subscriber, Payment { if (braintreeCustomer?.Id != stripeCustomerMetadata["btCustomerId"]) { - var nowSec = Utilities.CoreHelpers.ToEpocSeconds(DateTime.UtcNow); - stripeCustomerMetadata.Add($"btCustomerId_{nowSec}", stripeCustomerMetadata["btCustomerId"]); + stripeCustomerMetadata.Add("btCustomerId_old", stripeCustomerMetadata["btCustomerId"]); } stripeCustomerMetadata["btCustomerId"] = braintreeCustomer?.Id; } From 1f36f1e9f2a86bd287cb026aa6561609b159d300 Mon Sep 17 00:00:00 2001 From: Conner Turnbull Date: Thu, 7 Nov 2024 12:52:40 -0500 Subject: [PATCH 2/3] Updated to include case where old key already exists --- src/Core/Services/Implementations/StripePaymentService.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 60dff6ae6b37..259a4eb7572a 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1360,8 +1360,9 @@ public async Task UpdatePaymentMethodAsync(ISubscriber subscriber, Payment { if (braintreeCustomer?.Id != stripeCustomerMetadata["btCustomerId"]) { - stripeCustomerMetadata.Add("btCustomerId_old", stripeCustomerMetadata["btCustomerId"]); + stripeCustomerMetadata["btCustomerId_old"] = stripeCustomerMetadata["btCustomerId"]; } + stripeCustomerMetadata["btCustomerId"] = braintreeCustomer?.Id; } else if (!string.IsNullOrWhiteSpace(braintreeCustomer?.Id)) From 104879d2a8c3c3482a822d98e67fd367e72da254 Mon Sep 17 00:00:00 2001 From: Conner Turnbull Date: Fri, 15 Nov 2024 11:58:18 -0500 Subject: [PATCH 3/3] Updated SubscriberService to also save btCustomerId_old on the Stripe Customer's metadata --- src/Core/Billing/Services/Implementations/SubscriberService.cs | 3 ++- src/Core/Billing/Utilities.cs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Core/Billing/Services/Implementations/SubscriberService.cs b/src/Core/Billing/Services/Implementations/SubscriberService.cs index 9daf956221f0..401d9ce2caa9 100644 --- a/src/Core/Billing/Services/Implementations/SubscriberService.cs +++ b/src/Core/Billing/Services/Implementations/SubscriberService.cs @@ -523,8 +523,9 @@ await stripeAdapter.PaymentMethodAttachAsync(token, var metadata = customer.Metadata; - if (metadata.ContainsKey(BraintreeCustomerIdKey)) + if (metadata.TryGetValue(BraintreeCustomerIdKey, out var value)) { + metadata[BraintreeCustomerIdOldKey] = value; metadata[BraintreeCustomerIdKey] = null; } diff --git a/src/Core/Billing/Utilities.cs b/src/Core/Billing/Utilities.cs index b8bc1887bc17..28527af0c038 100644 --- a/src/Core/Billing/Utilities.cs +++ b/src/Core/Billing/Utilities.cs @@ -7,6 +7,7 @@ namespace Bit.Core.Billing; public static class Utilities { public const string BraintreeCustomerIdKey = "btCustomerId"; + public const string BraintreeCustomerIdOldKey = "btCustomerId_old"; public static async Task GetSubscriptionSuspensionAsync( IStripeAdapter stripeAdapter,