finalize-stripe-subscription-migration.mjs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. #!/usr/bin/env node
  2. /**
  3. * This script handles the cutover for subscriptions migrating from Recurly to Stripe.
  4. *
  5. * IMPORTANT: Only run this after Stripe subscriptions have been created in Stripe and
  6. * are ready to take over billing from Recurly.
  7. *
  8. * NOTE: This script will trigger lifecycle emails to be sent. Please turn off:
  9. * - "Send emails about upcoming renewals" (https://dashboard.stripe.com/<account>/settings/billing/subscriptions)
  10. * - "Subscription Change Template" (https://sharelatex.recurly.com/emails/subscription_change/template/edit)
  11. *
  12. * Usage:
  13. * node scripts/stripe/finalize-stripe-subscription-migration.mjs [OPTS] [INPUT-FILE]
  14. *
  15. * Options:
  16. * --output PATH Output file path (default: /tmp/migrate_output_<timestamp>.csv)
  17. * --commit Apply changes (without this, runs in dry-run mode)
  18. * --concurrency, -c <n> Number of customers to process concurrently (default: 10)
  19. * --recurly-rate-limit N Requests per second for Recurly (default: 10)
  20. * --recurly-api-retries N Number of retries on Recurly 429s (default: 5)
  21. * --recurly-retry-delay-ms N Delay between Recurly retries in ms (default: 1000)
  22. * --stripe-rate-limit N Requests per second for Stripe (default: 50)
  23. * --stripe-api-retries N Number of retries on Stripe 429s (default: 5)
  24. * --stripe-retry-delay-ms N Delay between Stripe retries in ms (default: 1000)
  25. * --help Show help message
  26. *
  27. * CSV Input Format:
  28. * recurly_account_code,target_stripe_account,stripe_customer_id
  29. * 507f1f77bcf86cd799439011,stripe-uk,cus_1234567890abcdef
  30. *
  31. * CSV Output Format:
  32. * recurly_account_code,target_stripe_account,stripe_customer_id,previous_recurly_status,previous_recurly_subscription_id,email,analyticsId,status,note
  33. *
  34. * Note: recurly_account_code is the Overleaf user ID (admin_id)
  35. */
  36. import fs from 'node:fs'
  37. import path from 'node:path'
  38. import * as csv from 'csv'
  39. import minimist from 'minimist'
  40. import Settings from '@overleaf/settings'
  41. import recurly from 'recurly'
  42. import PQueue from 'p-queue'
  43. import { z } from '../../app/src/infrastructure/Validation.mjs'
  44. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  45. import {
  46. getRegionClient,
  47. convertStripeStatusToSubscriptionState,
  48. } from '../../modules/subscriptions/app/src/StripeClient.mjs'
  49. import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.mjs'
  50. import { Subscription } from '../../app/src/models/Subscription.mjs'
  51. import { User } from '../../app/src/models/User.mjs'
  52. import AnalyticsManager from '../../app/src/Features/Analytics/AnalyticsManager.mjs'
  53. import AccountMappingHelper from '../../app/src/Features/Analytics/AccountMappingHelper.mjs'
  54. import PlansLocator from '../../app/src/Features/Subscription/PlansLocator.mjs'
  55. import UserAnalyticsIdCache from '../../app/src/Features/Analytics/UserAnalyticsIdCache.mjs'
  56. import CustomerIoHandler from '../../modules/customer-io/app/src/CustomerIoHandler.mjs'
  57. import { ReportError, convertToMinorUnits } from './helpers.mjs'
  58. import isEqual from 'lodash/isEqual.js'
  59. import { compareAccountFields } from '../helpers/migrate_recurly_customers_to_stripe.helpers.mjs'
  60. import {
  61. createRateLimitedApiWrappers,
  62. DEFAULT_RECURLY_RATE_LIMIT,
  63. DEFAULT_STRIPE_RATE_LIMIT,
  64. DEFAULT_RECURLY_API_RETRIES,
  65. DEFAULT_RECURLY_RETRY_DELAY_MS,
  66. DEFAULT_STRIPE_API_RETRIES,
  67. DEFAULT_STRIPE_RETRY_DELAY_MS,
  68. } from './RateLimiter.mjs'
  69. const preloadedProductMetadata = new Map()
  70. // rate limiters - initialized in main()
  71. let rateLimiters
  72. // Recurly SDK client - initialized at module level
  73. const recurlyApiKey =
  74. process.env.RECURLY_API_KEY || Settings.apis?.recurly?.apiKey
  75. if (!recurlyApiKey) {
  76. throw new Error(
  77. 'Recurly API key is not set. Set RECURLY_API_KEY env var or configure Settings.apis.recurly.apiKey'
  78. )
  79. }
  80. const recurlyClient = new recurly.Client(recurlyApiKey)
  81. function usage() {
  82. console.error(`Usage: node scripts/stripe/finalize-stripe-subscription-migration.mjs [OPTS] [INPUT-FILE]
  83. Options:
  84. --output PATH Output file path (default: /tmp/migrate_output_<timestamp>.csv)
  85. --commit Apply changes (without this, runs in dry-run mode)
  86. --concurrency N Number of customers to process concurrently (default: 10)
  87. --recurly-rate-limit N Requests per second for Recurly (default: ${DEFAULT_RECURLY_RATE_LIMIT})
  88. --recurly-api-retries N Number of retries on Recurly 429s (default: ${DEFAULT_RECURLY_API_RETRIES})
  89. --recurly-retry-delay-ms N Delay between Recurly retries in ms (default: ${DEFAULT_RECURLY_RETRY_DELAY_MS})
  90. --stripe-rate-limit N Requests per second for Stripe (default: ${DEFAULT_STRIPE_RATE_LIMIT})
  91. --stripe-api-retries N Number of retries on Stripe 429s (default: ${DEFAULT_STRIPE_API_RETRIES})
  92. --stripe-retry-delay-ms N Delay between Stripe retries in ms (default: ${DEFAULT_STRIPE_RETRY_DELAY_MS})
  93. --help Show this help message
  94. `)
  95. }
  96. async function main(trackProgress) {
  97. const opts = parseArgs()
  98. const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
  99. const outputFile = opts.output ?? `/tmp/migrate_output_${timestamp}.csv`
  100. // initialize rate limiters
  101. rateLimiters = createRateLimitedApiWrappers({
  102. recurlyRateLimit: opts.recurlyRateLimit,
  103. recurlyApiRetries: opts.recurlyApiRetries,
  104. recurlyRetryDelayMs: opts.recurlyRetryDelayMs,
  105. stripeRateLimit: opts.stripeRateLimit,
  106. stripeApiRetries: opts.stripeApiRetries,
  107. stripeRetryDelayMs: opts.stripeRetryDelayMs,
  108. })
  109. await trackProgress('Starting Recurly to Stripe migration cutover')
  110. await trackProgress(`Run mode: ${opts.commit ? 'COMMIT' : 'DRY RUN'}`)
  111. await trackProgress(
  112. `Rate limits: Recurly ${opts.recurlyRateLimit}/s, Stripe ${opts.stripeRateLimit}/s`
  113. )
  114. await trackProgress(`Concurrency: ${opts.concurrency}`)
  115. const inputStream = opts.inputFile
  116. ? fs.createReadStream(opts.inputFile)
  117. : process.stdin
  118. const csvReader = getCsvReader(inputStream)
  119. const csvWriter = getCsvWriter(outputFile)
  120. await trackProgress('Populating product metadata...')
  121. await preloadProductMetadata('uk')
  122. await preloadProductMetadata('us')
  123. await trackProgress('Product metadata populated')
  124. await trackProgress(`Output: ${outputFile}`)
  125. let processedCount = 0
  126. let successCount = 0
  127. let errorCount = 0
  128. const queue = new PQueue({ concurrency: opts.concurrency })
  129. const maxQueueSize = opts.concurrency
  130. try {
  131. for await (const input of csvReader) {
  132. // throttle input if queue is full
  133. if (queue.size >= maxQueueSize) {
  134. await queue.onSizeLessThan(maxQueueSize)
  135. }
  136. queue.add(async () => {
  137. try {
  138. const result = await processMigration(input, opts.commit)
  139. csvWriter.write({
  140. recurly_account_code: input.recurly_account_code,
  141. target_stripe_account: input.target_stripe_account,
  142. stripe_customer_id: input.stripe_customer_id,
  143. previous_recurly_status: result.previousRecurlyStatus || '',
  144. previous_recurly_subscription_id:
  145. result.previousRecurlySubscriptionId || '',
  146. email: result.email || '',
  147. analyticsId: result.analyticsId || '',
  148. status: result.status,
  149. note: result.note,
  150. })
  151. if (
  152. result.status.startsWith('migrated') ||
  153. result.status === 'validated'
  154. ) {
  155. successCount++
  156. } else {
  157. errorCount++
  158. }
  159. } catch (err) {
  160. errorCount++
  161. if (err instanceof ReportError) {
  162. csvWriter.write({
  163. recurly_account_code: input.recurly_account_code,
  164. target_stripe_account: input.target_stripe_account,
  165. stripe_customer_id: input.stripe_customer_id,
  166. previous_recurly_status: '',
  167. previous_recurly_subscription_id: '',
  168. email: '',
  169. analyticsId: '',
  170. status: err.status,
  171. note: err.message,
  172. })
  173. } else {
  174. csvWriter.write({
  175. recurly_account_code: input.recurly_account_code,
  176. target_stripe_account: input.target_stripe_account,
  177. stripe_customer_id: input.stripe_customer_id,
  178. previous_recurly_status: '',
  179. previous_recurly_subscription_id: '',
  180. email: '',
  181. analyticsId: '',
  182. status: 'error',
  183. note: err.message,
  184. })
  185. }
  186. }
  187. processedCount++
  188. if (processedCount % 25 === 0) {
  189. await trackProgress(
  190. `Progress: ${processedCount} processed, ${successCount} successful, ${errorCount} errors`
  191. )
  192. }
  193. })
  194. }
  195. } finally {
  196. // wait for all queued tasks to complete
  197. await queue.onIdle()
  198. }
  199. await trackProgress(`✅ Total processed: ${processedCount}`)
  200. if (opts.commit) {
  201. await trackProgress(`✅ Successfully migrated: ${successCount}`)
  202. } else {
  203. await trackProgress(`✅ Successfully validated: ${successCount}`)
  204. await trackProgress('ℹ️ DRY RUN: No changes were applied')
  205. }
  206. await trackProgress(`❌ Errors: ${errorCount}`)
  207. await trackProgress('🎉 Script completed!')
  208. csvWriter.end()
  209. await CustomerIoHandler.closeCustomerIo()
  210. }
  211. function getCsvReader(inputStream) {
  212. const parser = csv.parse({ columns: true })
  213. inputStream.pipe(parser)
  214. return parser
  215. }
  216. function getCsvWriter(outputFile) {
  217. fs.mkdirSync(path.dirname(outputFile), { recursive: true })
  218. const outputStream = fs.createWriteStream(outputFile)
  219. const writer = csv.stringify({
  220. columns: [
  221. 'recurly_account_code',
  222. 'target_stripe_account',
  223. 'stripe_customer_id',
  224. 'previous_recurly_status',
  225. 'previous_recurly_subscription_id',
  226. 'email',
  227. 'analyticsId',
  228. 'status',
  229. 'note',
  230. ],
  231. header: true,
  232. })
  233. writer.on('error', err => {
  234. console.error(err)
  235. process.exit(1)
  236. })
  237. writer.pipe(outputStream)
  238. return writer
  239. }
  240. async function preloadProductMetadata(region) {
  241. if (preloadedProductMetadata.has(region)) return
  242. const stripeClient = getRegionClient(region)
  243. const products = await rateLimiters.requestWithRetries(
  244. stripeClient.serviceName,
  245. () =>
  246. stripeClient.stripe.products.list({
  247. active: true,
  248. limit: 100,
  249. }),
  250. { operation: 'products.list', region: stripeClient.serviceName }
  251. )
  252. const results = new Map()
  253. for (const product of products.data) {
  254. results.set(product.id, product.metadata)
  255. }
  256. preloadedProductMetadata.set(region, results)
  257. }
  258. async function processMigration(input, commit) {
  259. const {
  260. recurly_account_code: overleafUserId,
  261. target_stripe_account: targetStripeAccount,
  262. stripe_customer_id: stripeCustomerId,
  263. } = input
  264. // Get Stripe client for the target account (strip 'stripe-' prefix if present)
  265. const region = targetStripeAccount.replace(/^stripe-/, '')
  266. const stripeClient = getRegionClient(region)
  267. // 1. Fetch Mongo subscription
  268. const mongoSubscription = await Subscription.findOne({
  269. admin_id: overleafUserId,
  270. }).exec()
  271. if (!mongoSubscription) {
  272. throw new ReportError(
  273. 'no-mongo-subscription',
  274. 'No subscription found in Mongo'
  275. )
  276. }
  277. // 2. Check if already migrated to Stripe
  278. if (mongoSubscription.paymentProvider?.service?.includes('stripe')) {
  279. throw new ReportError('already-stripe', 'Subscription already using Stripe')
  280. }
  281. // 3. Store previous state for output
  282. const previousRecurlyStatus = mongoSubscription.recurlyStatus
  283. ? JSON.stringify(mongoSubscription.recurlyStatus)
  284. : ''
  285. const previousRecurlySubscriptionId =
  286. mongoSubscription.recurlySubscription_id || ''
  287. // 4. Find Stripe subscription for this customer
  288. let stripeCustomer
  289. let stripeSubscription
  290. try {
  291. stripeCustomer = await rateLimiters.requestWithRetries(
  292. stripeClient.serviceName,
  293. () =>
  294. stripeClient.getCustomerById(stripeCustomerId, [
  295. 'subscriptions',
  296. 'subscriptions.data.schedule',
  297. ]),
  298. {
  299. operation: 'getCustomerById',
  300. stripeCustomerId,
  301. region: stripeClient.serviceName,
  302. }
  303. )
  304. // handle no subscriptions found
  305. if (
  306. !stripeCustomer.subscriptions ||
  307. stripeCustomer.subscriptions.data.length === 0
  308. ) {
  309. throw new ReportError(
  310. 'no-stripe-subscription',
  311. 'No Stripe subscriptions found for customer'
  312. )
  313. }
  314. // handle multiple active subscriptions found
  315. const activeSubscriptions = stripeCustomer.subscriptions.data.filter(sub =>
  316. ['active', 'past_due', 'incomplete'].includes(sub.status)
  317. )
  318. if (activeSubscriptions.length > 1) {
  319. throw new ReportError(
  320. 'multiple-active-stripe-subscriptions',
  321. 'Multiple active Stripe subscriptions found for customer'
  322. )
  323. }
  324. // find the target subscription with migration metadata
  325. stripeSubscription = stripeCustomer.subscriptions.data.find(
  326. sub => sub.metadata?.recurly_to_stripe_migration_status === 'in_progress'
  327. )
  328. if (!stripeSubscription) {
  329. throw new ReportError(
  330. 'no-target-stripe-subscription',
  331. 'No target Stripe subscription found for customer'
  332. )
  333. }
  334. } catch (err) {
  335. if (err instanceof ReportError) throw err
  336. throw new ReportError(
  337. 'stripe-fetch-error',
  338. `Failed to fetch Stripe subscription: ${err.message}`
  339. )
  340. }
  341. // 5. Fetch Recurly subscription and account
  342. let recurlySubscription
  343. try {
  344. recurlySubscription = await rateLimiters.requestWithRetries(
  345. 'recurly',
  346. () =>
  347. recurlyClient.getSubscription(`uuid-${previousRecurlySubscriptionId}`),
  348. {
  349. operation: 'getSubscription',
  350. recurlySubscriptionId: previousRecurlySubscriptionId,
  351. }
  352. )
  353. } catch (err) {
  354. throw new ReportError(
  355. 'no-recurly-subscription',
  356. `Recurly subscription not found: ${err.message}`
  357. )
  358. }
  359. let recurlyAccount
  360. try {
  361. recurlyAccount = await rateLimiters.requestWithRetries(
  362. 'recurly',
  363. () => recurlyClient.getAccount(`code-${overleafUserId}`),
  364. {
  365. operation: 'getAccount',
  366. overleafUserId,
  367. }
  368. )
  369. } catch (err) {
  370. throw new ReportError(
  371. 'no-recurly-account',
  372. `Recurly account not found: ${err.message}`
  373. )
  374. }
  375. // 6. Detect changes between Recurly and Stripe
  376. const subscriptionChanges = detectSubscriptionChanges(
  377. recurlySubscription,
  378. stripeSubscription,
  379. region
  380. )
  381. const accountChanges = await detectAccountChanges(
  382. overleafUserId,
  383. stripeCustomerId,
  384. stripeClient,
  385. recurlyAccount,
  386. recurlySubscription.collectionMethod || null
  387. )
  388. const allChanges = [...subscriptionChanges, ...accountChanges]
  389. if (allChanges.length > 0) {
  390. throw new ReportError(
  391. 'changes-detected',
  392. `Changes detected between Recurly and Stripe: ${allChanges.join('; ')}`
  393. )
  394. }
  395. // 7. If commit mode, perform migration
  396. const analyticsId = await UserAnalyticsIdCache.getWithMetrics(
  397. overleafUserId,
  398. 'script' // no-op, metrics are not collected from scripts.
  399. )
  400. const mongoUser = await User.findOne({
  401. _id: overleafUserId,
  402. }).exec()
  403. const result = {
  404. status: 'not-migrated',
  405. note: 'Not yet migrated',
  406. previousRecurlyStatus,
  407. previousRecurlySubscriptionId,
  408. email: mongoUser?.email || stripeCustomer.email,
  409. analyticsId,
  410. }
  411. if (commit) {
  412. try {
  413. await performCutover(
  414. mongoSubscription,
  415. stripeSubscription,
  416. recurlySubscription,
  417. stripeClient,
  418. stripeCustomer,
  419. mongoUser?.email
  420. )
  421. } catch (err) {
  422. if (err instanceof ReportError && err.status?.startsWith('migrated-')) {
  423. result.status = err.status
  424. result.note = err.message
  425. return result
  426. }
  427. throw err
  428. }
  429. result.status = 'migrated'
  430. result.note = 'Successfully migrated to Stripe'
  431. if (stripeCustomer.metadata?.taxInfoPending) {
  432. result.status += '-tax-info-pending'
  433. result.note += '; Tax info pending'
  434. }
  435. return result
  436. } else {
  437. result.status = 'validated'
  438. result.note = 'DRY RUN: Ready to migrate'
  439. return result
  440. }
  441. }
  442. /**
  443. * Format subscription items for display in error messages
  444. */
  445. function formatItems(items) {
  446. return items
  447. .map(item => `${item.code}(qty:${item.quantity},amt:${item.amount})`)
  448. .join(', ')
  449. }
  450. function detectSubscriptionChanges(
  451. recurlySubscription,
  452. stripeSubscription,
  453. region
  454. ) {
  455. const changes = []
  456. // Extract item details from Recurly subscription
  457. const targetRecurlySubscription =
  458. recurlySubscription.pendingChange || recurlySubscription
  459. const recurlyPlanItem =
  460. PlansLocator.convertLegacyGroupPlanCodeToConsolidatedGroupPlanCodeIfNeeded(
  461. targetRecurlySubscription.plan.code
  462. )
  463. const simplifiedPlanCode = recurlyPlanItem.planCode.replace(
  464. /_free_trial.*$/,
  465. ''
  466. )
  467. const additionalLicenseQuantity =
  468. (targetRecurlySubscription.addOns || []).find(
  469. addOn => addOn.addOn.code === 'additional-license'
  470. )?.quantity || 0
  471. const currency = recurlySubscription.currency
  472. const recurlyItems = [
  473. {
  474. code: simplifiedPlanCode,
  475. quantity: recurlyPlanItem.quantity + additionalLicenseQuantity,
  476. amount:
  477. convertToMinorUnits(targetRecurlySubscription.unitAmount, currency) /
  478. recurlyPlanItem.quantity,
  479. },
  480. ...(targetRecurlySubscription.addOns || [])
  481. .filter(addOn => addOn.addOn.code !== 'additional-license')
  482. .map(addOn => ({
  483. code: addOn.addOn.code,
  484. quantity: addOn.quantity,
  485. amount: convertToMinorUnits(addOn.unitAmount, currency),
  486. })),
  487. ].sort((a, b) => a.code.localeCompare(b.code))
  488. // Extract item details from Stripe subscription
  489. const products = preloadedProductMetadata.get(region)
  490. const hasAddOns = stripeSubscription.items.data.length > 1
  491. const stripeItems = stripeSubscription.items.data
  492. .map(item => {
  493. const productMetadata = products.get(item.price.product)
  494. if (!productMetadata) {
  495. throw new ReportError(
  496. 'unknown-stripe-product',
  497. `Unknown Stripe product: ${item.price.product}`
  498. )
  499. }
  500. return {
  501. code:
  502. productMetadata?.planCode?.includes('assistant') && hasAddOns
  503. ? productMetadata?.addOnCode
  504. : productMetadata?.planCode,
  505. quantity: item.quantity,
  506. amount: item.price.unit_amount,
  507. }
  508. })
  509. .sort((a, b) => a.code.localeCompare(b.code))
  510. // Compare items
  511. if (!isEqual(recurlyItems, stripeItems)) {
  512. changes.push(
  513. `Items: Recurly=[${formatItems(recurlyItems)}], Stripe=[${formatItems(stripeItems)}]`
  514. )
  515. }
  516. // Compare states
  517. const recurlyState = recurlySubscription.state
  518. const stripeState = convertStripeStatusToSubscriptionState(stripeSubscription)
  519. if (recurlyState !== stripeState) {
  520. changes.push(`State: Recurly=${recurlyState}, Stripe=${stripeState}`)
  521. }
  522. return changes
  523. }
  524. /**
  525. * Detect account-level drift between the Recurly account and the migrated Stripe customer.
  526. *
  527. * Uses the Recurly SDK account (which includes billing info), and re-retrieves
  528. * the Stripe customer with expanded tax_ids and default_payment_method so the
  529. * comparison can cover all the fields that the customer-migration script set.
  530. *
  531. * @param {string} overleafUserId - Recurly account code / Overleaf user ID
  532. * @param {string} stripeCustomerId - Stripe customer ID
  533. * @param {object} stripeClient - Stripe client (from getRegionClient)
  534. * @param {object} account - Recurly SDK account object (from recurlyClient.getAccount)
  535. * @param {string|null} collectionMethod - Recurly subscription collection method
  536. * @returns {Promise<string[]>} - Array of change descriptions (empty = no drift)
  537. */
  538. async function detectAccountChanges(
  539. overleafUserId,
  540. stripeCustomerId,
  541. stripeClient,
  542. account,
  543. collectionMethod
  544. ) {
  545. const context = { overleafUserId, stripeCustomerId }
  546. // Fetch the Stripe customer with tax_ids and payment method expanded
  547. const stripeCustomer = await rateLimiters.requestWithRetries(
  548. stripeClient.serviceName,
  549. () =>
  550. stripeClient.stripe.customers.retrieve(stripeCustomerId, {
  551. expand: ['tax_ids', 'invoice_settings.default_payment_method'],
  552. }),
  553. { ...context, operation: 'customers.retrieve' }
  554. )
  555. if (stripeCustomer.deleted) {
  556. return [`Stripe customer ${stripeCustomerId} has been deleted`]
  557. }
  558. // Pre-fetch payment methods if needed for comparison
  559. let stripePaymentMethods = []
  560. const isPaypalBillingAgreement =
  561. account.billingInfo?.paymentMethod?.object === 'paypal_billing_agreement'
  562. if (!isPaypalBillingAgreement && account.billingInfo?.paymentMethod) {
  563. const result = await rateLimiters.requestWithRetries(
  564. stripeClient.serviceName,
  565. () => stripeClient.stripe.customers.listPaymentMethods(stripeCustomerId),
  566. { ...context, operation: 'customers.listPaymentMethods' }
  567. )
  568. stripePaymentMethods = result.data
  569. }
  570. const diffs = await compareAccountFields({
  571. account,
  572. stripeCustomer,
  573. overleafUserId,
  574. fetchCollectionMethod: async () => collectionMethod,
  575. stripePaymentMethods,
  576. stripeServiceName: stripeClient.serviceName,
  577. })
  578. return formatDiffsAsChanges(diffs)
  579. }
  580. /**
  581. * Convert structured diffs from compareAccountFields into human-readable change descriptions.
  582. */
  583. function formatDiffsAsChanges(diffs) {
  584. const changes = []
  585. for (const [field, diff] of Object.entries(diffs)) {
  586. if (field === 'address') {
  587. changes.push(
  588. `Address: Recurly=${JSON.stringify(diff.recurly)}, Stripe=${JSON.stringify(diff.stripe)}`
  589. )
  590. } else if (field === 'cc_emails') {
  591. changes.push(
  592. `CC emails: Recurly=[${[...diff.recurly].sort().join(',')}], Stripe=[${[...(diff.stripe || [])].sort().join(',')}]`
  593. )
  594. } else if (field === 'tax_id') {
  595. const stripeStr = diff.stripe
  596. ? diff.stripe.map(t => `{type:${t.type}, value:${t.value}}`).join(', ')
  597. : '(none)'
  598. changes.push(
  599. `Tax ID: Recurly={type:${diff.recurly.type}, value:${diff.recurly.value}}, Stripe=${stripeStr}`
  600. )
  601. } else if (field === 'default_payment_method') {
  602. changes.push(
  603. `Payment method: Recurly=${diff.recurly.type || diff.recurly.last4 || '(none)'}, Stripe=${diff.stripe.type || '(none)'}`
  604. )
  605. } else if (field.startsWith('metadata.')) {
  606. const key = field.slice('metadata.'.length)
  607. changes.push(
  608. `Metadata ${key}: Recurly=${diff.recurly || '(empty)'}, Stripe=${diff.stripe || '(empty)'}`
  609. )
  610. } else {
  611. const label =
  612. field.charAt(0).toUpperCase() + field.slice(1).replace(/_/g, ' ')
  613. changes.push(
  614. `${label}: Recurly=${diff.recurly || '(empty)'}, Stripe=${diff.stripe || '(empty)'}`
  615. )
  616. }
  617. }
  618. return changes
  619. }
  620. async function performCutover(
  621. mongoSubscription,
  622. stripeSubscription,
  623. recurlySubscription,
  624. stripeClient,
  625. stripeCustomer,
  626. mongoUserEmail
  627. ) {
  628. const adminUserId = mongoSubscription.admin_id.toString()
  629. // Step 1: Update Mongo subscription to point to Stripe
  630. mongoSubscription.paymentProvider = {
  631. service: stripeClient.serviceName,
  632. subscriptionId: stripeSubscription.id,
  633. state: convertStripeStatusToSubscriptionState(stripeSubscription),
  634. }
  635. mongoSubscription.recurlySubscription_id = undefined
  636. mongoSubscription.recurlyStatus = undefined
  637. try {
  638. await mongoSubscription.save()
  639. } catch (err) {
  640. throw new ReportError(
  641. 'not-migrated-mongo-update-failed',
  642. `Failed to update Mongo subscription: ${err.message}`
  643. )
  644. }
  645. // Step 2: Emit migration analytics event
  646. AnalyticsManager.recordEventForUserInBackground(
  647. adminUserId,
  648. 'subscription-migrated-to-stripe',
  649. {
  650. subscriptionId: mongoSubscription._id.toString(),
  651. migrationDirection: 'recurly-to-stripe',
  652. }
  653. )
  654. // Step 3: Postpone Recurly billing by +10 years if Recurly subscription is active
  655. if (recurlySubscription.state !== 'canceled') {
  656. const currentBillingDate = new Date(recurlySubscription.currentPeriodEndsAt)
  657. const postponedDate = new Date(currentBillingDate)
  658. postponedDate.setFullYear(currentBillingDate.getFullYear() + 10)
  659. try {
  660. await rateLimiters.requestWithRetries(
  661. 'recurly',
  662. () =>
  663. RecurlyWrapper.promises.apiRequest({
  664. url: `subscriptions/${recurlySubscription.uuid}/postpone`,
  665. qs: { bulk: true, next_bill_date: postponedDate },
  666. method: 'PUT',
  667. }),
  668. {
  669. operation: 'postpone',
  670. recurlySubscriptionId: recurlySubscription.uuid,
  671. }
  672. )
  673. } catch (err) {
  674. throw new ReportError(
  675. 'migrated-recurly-postpone-failed',
  676. `Failed to postpone Recurly billing: ${err.message}`
  677. )
  678. }
  679. }
  680. // Step 4: Remove migration metadata from Stripe
  681. try {
  682. await rateLimiters.requestWithRetries(
  683. stripeClient.serviceName,
  684. () =>
  685. stripeClient.updateSubscriptionMetadata(stripeSubscription.id, {
  686. recurly_to_stripe_migration_status: '',
  687. }),
  688. {
  689. operation: 'updateSubscriptionMetadata',
  690. stripeSubscriptionId: stripeSubscription.id,
  691. region: stripeClient.serviceName,
  692. }
  693. )
  694. } catch (err) {
  695. throw new ReportError(
  696. 'migrated-metadata-removal-failed',
  697. `Successfully migrated to Stripe but failed to remove metadata: ${err.message}`
  698. )
  699. }
  700. // Step 5: Register analytics mapping
  701. try {
  702. AnalyticsManager.registerAccountMapping(
  703. AccountMappingHelper.generateSubscriptionToStripeMapping(
  704. mongoSubscription._id,
  705. stripeSubscription.id,
  706. stripeClient.serviceName
  707. )
  708. )
  709. } catch (err) {
  710. throw new ReportError(
  711. 'migrated-analytics-mapping-failed',
  712. `Successfully migrated to Stripe but failed to register analytics mapping: ${err.message}`
  713. )
  714. }
  715. // Step 6. Send data to customer.io
  716. try {
  717. const migrationDate = new Date().toISOString().slice(0, 10)
  718. const needsToUpdateTaxInfo =
  719. (stripeCustomer.metadata?.taxInfoPending || '').length > 0
  720. // TODO: request Recurly account and billingInfo to verify if tax info in Stripe is up to date
  721. CustomerIoHandler.updateUserAttributes(adminUserId, {
  722. email: mongoUserEmail || stripeCustomer.email,
  723. stripe_migration: {
  724. migration_date: migrationDate,
  725. needs_to_update_tax_id: needsToUpdateTaxInfo,
  726. },
  727. })
  728. } catch (err) {
  729. throw new ReportError(
  730. 'migrated-customerio-upload-failed',
  731. `Successfully migrated to Stripe but failed to upload user to customer.io: ${err.message}`
  732. )
  733. }
  734. // Step 7: Release subscription schedule associated with the migration
  735. const schedule = stripeSubscription.schedule
  736. if (
  737. schedule &&
  738. typeof schedule !== 'string' &&
  739. schedule.metadata?.billing_migration_id
  740. ) {
  741. try {
  742. await rateLimiters.requestWithRetries(
  743. stripeClient.serviceName,
  744. () =>
  745. stripeClient.stripe.subscriptionSchedules.release(schedule.id, {
  746. preserve_cancel_date: true,
  747. }),
  748. {
  749. operation: 'subscriptionSchedules.release',
  750. scheduleId: schedule.id,
  751. region: stripeClient.serviceName,
  752. }
  753. )
  754. } catch (err) {
  755. throw new ReportError(
  756. 'migrated-schedule-release-failed',
  757. `Successfully migrated to Stripe but failed to release subscription schedule: ${err.message}`
  758. )
  759. }
  760. }
  761. }
  762. function parseArgs() {
  763. const args = minimist(process.argv.slice(2), {
  764. string: [
  765. 'output',
  766. 'concurrency',
  767. 'recurly-rate-limit',
  768. 'recurly-api-retries',
  769. 'recurly-retry-delay-ms',
  770. 'stripe-rate-limit',
  771. 'stripe-api-retries',
  772. 'stripe-retry-delay-ms',
  773. ],
  774. boolean: ['commit', 'help'],
  775. default: {
  776. commit: false,
  777. concurrency: 10,
  778. 'recurly-rate-limit': DEFAULT_RECURLY_RATE_LIMIT,
  779. 'recurly-api-retries': DEFAULT_RECURLY_API_RETRIES,
  780. 'recurly-retry-delay-ms': DEFAULT_RECURLY_RETRY_DELAY_MS,
  781. 'stripe-rate-limit': DEFAULT_STRIPE_RATE_LIMIT,
  782. 'stripe-api-retries': DEFAULT_STRIPE_API_RETRIES,
  783. 'stripe-retry-delay-ms': DEFAULT_STRIPE_RETRY_DELAY_MS,
  784. },
  785. })
  786. if (args.help) {
  787. usage()
  788. process.exit(0)
  789. }
  790. const inputFile = args._[0]
  791. const paramsSchema = z.object({
  792. output: z.string().optional(),
  793. commit: z.boolean(),
  794. concurrency: z.number().int().positive(),
  795. recurlyRateLimit: z.number().positive(),
  796. recurlyApiRetries: z.number().int().nonnegative(),
  797. recurlyRetryDelayMs: z.number().int().nonnegative(),
  798. stripeRateLimit: z.number().positive(),
  799. stripeApiRetries: z.number().int().nonnegative(),
  800. stripeRetryDelayMs: z.number().int().nonnegative(),
  801. inputFile: z.string().optional(),
  802. })
  803. try {
  804. return paramsSchema.parse({
  805. output: args.output,
  806. commit: args.commit,
  807. concurrency: Number(args.concurrency),
  808. recurlyRateLimit: Number(args['recurly-rate-limit']),
  809. recurlyApiRetries: Number(args['recurly-api-retries']),
  810. recurlyRetryDelayMs: Number(args['recurly-retry-delay-ms']),
  811. stripeRateLimit: Number(args['stripe-rate-limit']),
  812. stripeApiRetries: Number(args['stripe-api-retries']),
  813. stripeRetryDelayMs: Number(args['stripe-retry-delay-ms']),
  814. inputFile,
  815. })
  816. } catch (err) {
  817. console.error('Invalid arguments:', err.message)
  818. usage()
  819. process.exit(1)
  820. }
  821. }
  822. try {
  823. await scriptRunner(main)
  824. process.exit(0)
  825. } catch (error) {
  826. console.error(error)
  827. process.exit(1)
  828. }