diff --git a/services/web/scripts/recurly/migrate_recurly_customers_to_stripe.mjs b/services/web/scripts/recurly/migrate_recurly_customers_to_stripe.mjs index 72865eaf60..7603785e8e 100644 --- a/services/web/scripts/recurly/migrate_recurly_customers_to_stripe.mjs +++ b/services/web/scripts/recurly/migrate_recurly_customers_to_stripe.mjs @@ -1023,6 +1023,19 @@ async function processCustomer( if (account.createdAt) { metadata.recurlyCreatedAt = account.createdAt.toISOString() } + if ( + existingCustomer.metadata != null && + existingCustomer.metadata.recurlyAccountCode === recurlyAccountCode && + existingCustomer.metadata.userId == null + ) { + metadata.recurlyAccountCode = '' + metadata.userId = recurlyAccountCode + } else { + logWarn('Stripe customer metadata cannot be remapped', { + ...context, + existingCustomerMetadata: existingCustomer.metadata, + }) + } /** @type {Stripe.CustomerUpdateParams} */ const customerParams = { diff --git a/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs b/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs index bac0a2c853..797cedd40d 100755 --- a/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs +++ b/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs @@ -314,7 +314,8 @@ async function processMigration(input, commit) { mongoSubscription, stripeSubscription, recurlySubscription, - stripeClient + stripeClient, + stripeCustomer ) return { status: 'migrated', @@ -403,7 +404,8 @@ async function performCutover( mongoSubscription, stripeSubscription, recurlySubscription, - stripeClient + stripeClient, + stripeCustomer ) { const adminUserId = mongoSubscription.admin_id.toString() @@ -473,6 +475,25 @@ async function performCutover( `Successfully migrated to Stripe but failed to register analytics mapping: ${err.message}` ) } + + // Step 6. Remap customer metadata (if needed) in Stripe + if ( + stripeCustomer.metadata != null && + stripeCustomer.metadata.recurlyAccountCode != null && + stripeCustomer.metadata.userId == null + ) { + try { + await stripeClient.updateCustomerMetadata(stripeCustomer.id, { + recurlyAccountCode: '', + userId: adminUserId, + }) + } catch (err) { + throw new ReportError( + 'customer-metadata-removal-failed', + `Successfully migrated to Stripe and registered analytics mapping but failed to remove customer metadata: ${err.message}` + ) + } + } } function parseArgs() {