From 44bf5619c86b8635caf73dac6c8036b99034bb32 Mon Sep 17 00:00:00 2001 From: Conner Turnbull <133619638+cturnbull-bitwarden@users.noreply.github.com> Date: Tue, 19 Nov 2024 11:38:30 -0500 Subject: [PATCH] Updated customer metadata to only store one old btCustomerId (#4997) * Updated customer metadata to only store one old btCustomerId * Updated to include case where old key already exists * Updated SubscriberService to also save btCustomerId_old on the Stripe Customer's metadata --------- Co-authored-by: Matt Bishop --- .../Billing/Services/Implementations/SubscriberService.cs | 3 ++- src/Core/Billing/Utilities.cs | 1 + src/Core/Services/Implementations/StripePaymentService.cs | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Core/Billing/Services/Implementations/SubscriberService.cs b/src/Core/Billing/Services/Implementations/SubscriberService.cs index 9daf95622..401d9ce2c 100644 --- a/src/Core/Billing/Services/Implementations/SubscriberService.cs +++ b/src/Core/Billing/Services/Implementations/SubscriberService.cs @@ -523,8 +523,9 @@ public class SubscriberService( 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 b8bc1887b..28527af0c 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, diff --git a/src/Core/Services/Implementations/StripePaymentService.cs b/src/Core/Services/Implementations/StripePaymentService.cs index 7eb2b402b..259a4eb75 100644 --- a/src/Core/Services/Implementations/StripePaymentService.cs +++ b/src/Core/Services/Implementations/StripePaymentService.cs @@ -1360,9 +1360,9 @@ public class StripePaymentService : IPaymentService { if (braintreeCustomer?.Id != stripeCustomerMetadata["btCustomerId"]) { - var nowSec = Utilities.CoreHelpers.ToEpocSeconds(DateTime.UtcNow); - stripeCustomerMetadata.Add($"btCustomerId_{nowSec}", stripeCustomerMetadata["btCustomerId"]); + stripeCustomerMetadata["btCustomerId_old"] = stripeCustomerMetadata["btCustomerId"]; } + stripeCustomerMetadata["btCustomerId"] = braintreeCustomer?.Id; } else if (!string.IsNullOrWhiteSpace(braintreeCustomer?.Id))