2019-11-12 03:56:08 -05:00
|
|
|
const RecurlyWrapper = require('../../app/src/Features/Subscription/RecurlyWrapper')
|
|
|
|
const async = require('async')
|
|
|
|
const minimist = require('minimist')
|
|
|
|
|
|
|
|
const slowCallback = (callback, error, data) =>
|
|
|
|
setTimeout(() => callback(error, data), 80)
|
|
|
|
|
|
|
|
const handleAPIError = (source, id, error, callback) => {
|
|
|
|
console.warn(`Errors in ${source} with id=${id}`, error)
|
|
|
|
if (typeof error === 'string' && error.match(/429$/)) {
|
|
|
|
return setTimeout(callback, 1000 * 60 * 5)
|
|
|
|
}
|
|
|
|
slowCallback(callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
const attemptInvoiceCollection = (invoice, callback) => {
|
|
|
|
isAccountUsingPaypal(invoice, (error, isPaypal) => {
|
|
|
|
if (error || !isPaypal) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const accountId = invoice.account.url.match(/accounts\/(.*)/)[1]
|
|
|
|
if (USERS_COLLECTED.indexOf(accountId) > -1) {
|
|
|
|
console.warn(`Skipping duplicate user ${accountId}`)
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
INVOICES_COLLECTED.push(invoice.invoice_number)
|
|
|
|
USERS_COLLECTED.push(accountId)
|
|
|
|
if (DRY_RUN) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
RecurlyWrapper.attemptInvoiceCollection(
|
|
|
|
invoice.invoice_number,
|
|
|
|
(error, response) => {
|
|
|
|
if (error) {
|
|
|
|
return handleAPIError(
|
|
|
|
'attemptInvoiceCollection',
|
|
|
|
invoice.invoice_number,
|
|
|
|
error,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
2019-11-27 07:49:28 -05:00
|
|
|
INVOICES_COLLECTED_SUCCESS.push(invoice.invoice_number)
|
2019-11-12 03:56:08 -05:00
|
|
|
slowCallback(callback, null)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const isAccountUsingPaypal = (invoice, callback) => {
|
|
|
|
const accountId = invoice.account.url.match(/accounts\/(.*)/)[1]
|
|
|
|
RecurlyWrapper.getBillingInfo(accountId, (error, response) => {
|
|
|
|
if (error) {
|
|
|
|
return handleAPIError('billing info', accountId, error, callback)
|
|
|
|
}
|
|
|
|
if (response.billing_info.paypal_billing_agreement_id) {
|
|
|
|
return slowCallback(callback, null, true)
|
|
|
|
}
|
|
|
|
slowCallback(callback, null, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const attemptInvoicesCollection = callback => {
|
|
|
|
RecurlyWrapper.getPaginatedEndpoint(
|
|
|
|
'invoices',
|
|
|
|
{ state: 'past_due' },
|
|
|
|
(error, invoices) => {
|
|
|
|
console.log('invoices', invoices.length)
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
async.eachSeries(invoices, attemptInvoiceCollection, callback)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const argv = minimist(process.argv.slice(2))
|
|
|
|
const DRY_RUN = argv.n !== undefined
|
|
|
|
const INVOICES_COLLECTED = []
|
2019-11-27 07:49:28 -05:00
|
|
|
const INVOICES_COLLECTED_SUCCESS = []
|
2019-11-12 03:56:08 -05:00
|
|
|
const USERS_COLLECTED = []
|
|
|
|
attemptInvoicesCollection(error => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
console.log(
|
2020-12-15 05:23:54 -05:00
|
|
|
`DONE (DRY_RUN=${DRY_RUN}). ${INVOICES_COLLECTED.length} invoices collection attempts for ${USERS_COLLECTED.length} users. ${INVOICES_COLLECTED_SUCCESS.length} successful collections`
|
2019-11-27 07:49:28 -05:00
|
|
|
)
|
|
|
|
console.dir(
|
|
|
|
{
|
|
|
|
INVOICES_COLLECTED,
|
|
|
|
INVOICES_COLLECTED_SUCCESS,
|
2021-04-27 03:52:58 -04:00
|
|
|
USERS_COLLECTED,
|
2019-11-27 07:49:28 -05:00
|
|
|
},
|
|
|
|
{ maxArrayLength: null }
|
2019-11-12 03:56:08 -05:00
|
|
|
)
|
2021-07-01 11:07:26 -04:00
|
|
|
|
|
|
|
if (INVOICES_COLLECTED_SUCCESS === 0) {
|
|
|
|
process.exit(1)
|
|
|
|
} else {
|
|
|
|
process.exit()
|
|
|
|
}
|
2019-11-12 03:56:08 -05:00
|
|
|
})
|