diff --git a/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs b/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs index eecd0b737e..de1daa6e16 100755 --- a/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs +++ b/services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs @@ -322,6 +322,8 @@ async function processMigration(input, commit) { region: stripeClient.serviceName, } ) + + // handle no subscriptions found if ( !stripeCustomer.subscriptions || stripeCustomer.subscriptions.data.length === 0 @@ -331,13 +333,25 @@ async function processMigration(input, commit) { 'No Stripe subscriptions found for customer' ) } - // find the subscription with migration metadata + + // handle multiple active subscriptions found + const activeSubscriptions = stripeCustomer.subscriptions.data.filter(sub => + ['active', 'past_due', 'incomplete'].includes(sub.status) + ) + if (activeSubscriptions.length > 1) { + throw new ReportError( + 'multiple-active-stripe-subscriptions', + 'Multiple active Stripe subscriptions found for customer' + ) + } + + // find the target subscription with migration metadata stripeSubscription = stripeCustomer.subscriptions.data.find( sub => sub.metadata?.recurly_to_stripe_migration_status === 'in_progress' ) if (!stripeSubscription) { throw new ReportError( - 'no-stripe-subscription', + 'no-target-stripe-subscription', 'No target Stripe subscription found for customer' ) }