Просмотр исходного кода

[migration] fix issues found in finalization script during stage 3b (#32472)

* accept taxInfoPending as tax id comparison point
* convert recurly unit amount to minor units correctly for no cent currencies
* Use original subscription currency in finalization check
* make it easier to spot migrated with tax info pending

---------

Co-authored-by: John Lees-Miller <jdleesmiller@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
GitOrigin-RevId: d3c74032bc06369fcf6bafb4cfaff85c49a3cfef
Kristina 4 месяцев назад
Родитель
Сommit
ad853d661a

+ 13 - 6
services/web/scripts/helpers/migrate_recurly_customers_to_stripe.helpers.mjs

@@ -1459,13 +1459,8 @@ export async function compareAccountFields({
     if (taxIdTypeResult.type && address?.country) {
       expectedTaxIdType = taxIdTypeResult.type
       expectedTaxIdValue = vatNumber
-    } else {
-      expectedMetadata.taxInfoPending = vatNumber
     }
   }
-  if (!expectedMetadata.taxInfoPending) {
-    expectedMetadata.taxInfoPending = ''
-  }
 
   for (const [key, expectedValue] of Object.entries(expectedMetadata)) {
     const stripeValue = stripeCustomer.metadata?.[key] ?? ''
@@ -1504,7 +1499,19 @@ export async function compareAccountFields({
   // Tax ID
   if (expectedTaxIdType && expectedTaxIdValue) {
     const normalizedExpectedTaxIdValue = normalizeTaxIdValue(expectedTaxIdValue)
-    const stripeTaxIds = stripeCustomer.tax_ids?.data || []
+    const stripePendingTaxId = stripeCustomer.metadata?.taxInfoPending
+      ? [
+          {
+            type: expectedTaxIdType,
+            value: stripeCustomer.metadata.taxInfoPending,
+          },
+        ]
+      : null
+    const stripeTaxIdsFromStripe = stripeCustomer.tax_ids?.data
+    const stripeTaxIds =
+      Array.isArray(stripeTaxIdsFromStripe) && stripeTaxIdsFromStripe.length > 0
+        ? stripeTaxIdsFromStripe
+        : stripePendingTaxId || []
     const matchingTaxId = stripeTaxIds.find(
       tid =>
         tid.type === expectedTaxIdType &&

+ 10 - 3
services/web/scripts/stripe/finalize-stripe-subscription-migration.mjs

@@ -56,7 +56,7 @@ import AccountMappingHelper from '../../app/src/Features/Analytics/AccountMappin
 import PlansLocator from '../../app/src/Features/Subscription/PlansLocator.mjs'
 import UserAnalyticsIdCache from '../../app/src/Features/Analytics/UserAnalyticsIdCache.mjs'
 import CustomerIoHandler from '../../modules/customer-io/app/src/CustomerIoHandler.mjs'
-import { ReportError } from './helpers.mjs'
+import { ReportError, convertToMinorUnits } from './helpers.mjs'
 import isEqual from 'lodash/isEqual.js'
 import { compareAccountFields } from '../helpers/migrate_recurly_customers_to_stripe.helpers.mjs'
 import {
@@ -472,6 +472,12 @@ async function processMigration(input, commit) {
 
     result.status = 'migrated'
     result.note = 'Successfully migrated to Stripe'
+
+    if (stripeCustomer.metadata?.taxInfoPending) {
+      result.status += '-tax-info-pending'
+      result.note += '; Tax info pending'
+    }
+
     return result
   } else {
     result.status = 'validated'
@@ -511,12 +517,13 @@ function detectSubscriptionChanges(
     (targetRecurlySubscription.addOns || []).find(
       addOn => addOn.addOn.code === 'additional-license'
     )?.quantity || 0
+  const currency = recurlySubscription.currency
   const recurlyItems = [
     {
       code: simplifiedPlanCode,
       quantity: recurlyPlanItem.quantity + additionalLicenseQuantity,
       amount:
-        Math.round(targetRecurlySubscription.unitAmount * 100) /
+        convertToMinorUnits(targetRecurlySubscription.unitAmount, currency) /
         recurlyPlanItem.quantity,
     },
     ...(targetRecurlySubscription.addOns || [])
@@ -524,7 +531,7 @@ function detectSubscriptionChanges(
       .map(addOn => ({
         code: addOn.addOn.code,
         quantity: addOn.quantity,
-        amount: Math.round(addOn.unitAmount * 100),
+        amount: convertToMinorUnits(addOn.unitAmount, currency),
       })),
   ].sort((a, b) => a.code.localeCompare(b.code))