collect_paypal_past_due_invoice.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper')
  2. const minimist = require('minimist')
  3. const logger = require('@overleaf/logger')
  4. const waitMs =
  5. require.main === module
  6. ? timeout => new Promise(resolve => setTimeout(() => resolve(), timeout))
  7. : () => Promise.resolve()
  8. // NOTE: Errors are not propagated to the caller
  9. const handleAPIError = async (source, id, error) => {
  10. logger.warn(`Errors in ${source} with id=${id}`, error)
  11. if (typeof error === 'string' && error.match(/429$/)) {
  12. return waitMs(1000 * 60 * 5)
  13. }
  14. await waitMs(80)
  15. }
  16. /**
  17. * @returns {Promise<{
  18. * INVOICES_COLLECTED: string[],
  19. * INVOICES_COLLECTED_SUCCESS: string[],
  20. * USERS_COLLECTED: string[],
  21. * }>}
  22. */
  23. const main = async () => {
  24. const attemptInvoiceCollection = async invoice => {
  25. const isPaypal = await isAccountUsingPaypal(invoice)
  26. if (!isPaypal) {
  27. return
  28. }
  29. const accountId = invoice.account.url.match(/accounts\/(.*)/)[1]
  30. if (USERS_COLLECTED.indexOf(accountId) > -1) {
  31. logger.warn(`Skipping duplicate user ${accountId}`)
  32. return
  33. }
  34. INVOICES_COLLECTED.push(invoice.invoice_number)
  35. USERS_COLLECTED.push(accountId)
  36. if (DRY_RUN) {
  37. return
  38. }
  39. try {
  40. await RecurlyWrapper.promises.attemptInvoiceCollection(
  41. invoice.invoice_number
  42. )
  43. INVOICES_COLLECTED_SUCCESS.push(invoice.invoice_number)
  44. await waitMs(80)
  45. } catch (error) {
  46. return handleAPIError(
  47. 'attemptInvoiceCollection',
  48. invoice.invoice_number,
  49. error
  50. )
  51. }
  52. }
  53. const isAccountUsingPaypal = async invoice => {
  54. const accountId = invoice.account.url.match(/accounts\/(.*)/)[1]
  55. try {
  56. const response = await RecurlyWrapper.promises.getBillingInfo(accountId)
  57. await waitMs(80)
  58. return !!response.billing_info.paypal_billing_agreement_id
  59. } catch (error) {
  60. return handleAPIError('billing info', accountId, error)
  61. }
  62. }
  63. const attemptInvoicesCollection = async () => {
  64. let getPage = await RecurlyWrapper.promises.getPaginatedEndpointIterator(
  65. 'invoices',
  66. { state: 'past_due' }
  67. )
  68. while (getPage) {
  69. const { items, getNextPage } = await getPage()
  70. logger.info('invoices', items?.length)
  71. for (const invoice of items) {
  72. await attemptInvoiceCollection(invoice)
  73. }
  74. getPage = getNextPage
  75. }
  76. }
  77. const argv = minimist(process.argv.slice(2))
  78. const DRY_RUN = argv.n !== undefined
  79. const INVOICES_COLLECTED = []
  80. const INVOICES_COLLECTED_SUCCESS = []
  81. const USERS_COLLECTED = []
  82. try {
  83. await attemptInvoicesCollection()
  84. const diff = INVOICES_COLLECTED.length - INVOICES_COLLECTED_SUCCESS.length
  85. if (diff !== 0) {
  86. throw new Error(`Invoices collection failed for ${diff} invoices`)
  87. }
  88. return {
  89. INVOICES_COLLECTED,
  90. INVOICES_COLLECTED_SUCCESS,
  91. USERS_COLLECTED,
  92. }
  93. } finally {
  94. logger.info(
  95. `DONE (DRY_RUN=${DRY_RUN}). ${INVOICES_COLLECTED.length} invoices collection attempts for ${USERS_COLLECTED.length} users. ${INVOICES_COLLECTED_SUCCESS.length} successful collections`
  96. )
  97. console.dir(
  98. {
  99. INVOICES_COLLECTED,
  100. INVOICES_COLLECTED_SUCCESS,
  101. USERS_COLLECTED,
  102. },
  103. { maxArrayLength: null }
  104. )
  105. }
  106. }
  107. if (require.main === module) {
  108. main()
  109. .then(() => {
  110. logger.error('Done.')
  111. process.exit(0)
  112. })
  113. .catch(err => {
  114. logger.error('Error', err)
  115. process.exit(1)
  116. })
  117. }
  118. module.exports = { main }