Created
April 15, 2020 14:48
-
-
Save vincelwt/dd556355c33848a71d4b75fdc6382e3e to your computer and use it in GitHub Desktop.
Scripts to migrate all subscriptions between two Stripe accounts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cancel all the Stripe subscriptions supplied from a CSV export | |
const fs = require('fs') | |
const neatCsv = require('neat-csv') | |
const moment = require('moment') | |
const stripe = require('stripe')('sk_live_xxx') | |
// subscriptions.csv is the full export of subs from the stripe billing dashboard | |
fs.readFile('./subscriptions.csv', async (err, data) => { | |
if (err) return console.error(err) | |
const subs = await neatCsv(data) | |
for (let sub of subs) { | |
const id = sub['id'] | |
await stripe.subscriptions.del(id) | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Recreate Stripe subscriptions from a csv export on another account, assuming you already migrated customers. | |
// Add the corrects anchor dates and restore trials | |
const fs = require('fs') | |
const neatCsv = require('neat-csv') | |
const moment = require('moment') | |
const stripe = require('stripe')('sk_live_xxx') | |
const toTimestamp = (date) => parseInt(moment.utc(date).format("X")) | |
// Map the Stripe plan IDs to the new ones on the destination account | |
const targetPlans = { | |
'old_plan': 'new_plan', | |
'old_plan2': 'new_plan2' | |
} | |
// subscriptions.csv is the full export of subs from the stripe billing dashboard | |
fs.readFile('./subscriptions.csv', async (err, data) => { | |
if (err) return console.error(err) | |
const subs = await neatCsv(data) | |
for (let sub of subs) { | |
const cId = sub['Customer ID'] | |
const plan = sub['Plan'] | |
const status = sub['Status'] | |
const cancelled = (sub['Cancel At Period End'] === 'true') | |
const endOfPeriod = sub['Current Period End (UTC)'] | |
let options = { | |
customer: cId, | |
items: [{plan: targetPlans[plan]}], | |
prorate: false | |
} | |
if (status === 'past_due') continue | |
if (status === 'active') { | |
options.billing_cycle_anchor = toTimestamp(endOfPeriod) | |
} | |
if (status === 'trialing') { | |
options.trial_end = toTimestamp(endOfPeriod) | |
} | |
console.log(options) | |
const obj = await stripe.subscriptions.create(options) | |
if (cancelled) { | |
await stripe.subscriptions.update(obj.id, { cancel_at_period_end: true }) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment