Skip to content

Commit

Permalink
Updated customer metadata to only store one old btCustomerId (bitward…
Browse files Browse the repository at this point in the history
…en#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 <[email protected]>
  • Loading branch information
2 people authored and vgrassia committed Nov 21, 2024
1 parent 5312967 commit 559fc50
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/Core/Billing/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubscriptionSuspension> GetSubscriptionSuspensionAsync(
IStripeAdapter stripeAdapter,
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Services/Implementations/StripePaymentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1360,9 +1360,9 @@ public async Task<bool> UpdatePaymentMethodAsync(ISubscriber subscriber, Payment
{
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))
Expand Down

0 comments on commit 559fc50

Please sign in to comment.