Created
May 3, 2016 21:13
-
-
Save kennberg/21994873856536cddbfbfac38c73815e to your computer and use it in GitHub Desktop.
Cancel all Stripe subscriptions
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
var STRIPE_SECRET_KEY = 'YOUR_KEY'; | |
var OBJECTS_PER_REQUEST = 100; | |
var stripe = require('stripe')(STRIPE_SECRET_KEY); | |
function processSubs(err, subs) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
if (!subs) { | |
return; | |
} | |
console.log('Subs=' + subs['data'].length); | |
if (!subs['data'].length) { | |
return; | |
} | |
for (var j = 0; j < subs['data'].length; j++) { | |
var sub = subs['data'][j]; | |
console.log('Canceling ' + sub['customer'] + ' ' + sub['id']); | |
stripe.customers.cancelSubscription(sub['customer'], sub['id'], function(err, confirmation) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
console.log(confirmation); | |
}); | |
} | |
if (subs['has_more']) { | |
var sub = subs['data'][subs['data'].length - 1]; | |
subscriptionList(sub['customer'], sub['id']); | |
} | |
} | |
function subscriptionList(customerId, startingAfter) { | |
var options = { 'customer': customerId, 'limit': OBJECTS_PER_REQUEST }; | |
if (startingAfter) { | |
options['starting_after'] = startingAfter; | |
} | |
stripe.customers.listSubscriptions(options, processSubs); | |
} | |
function customerList(startingAfter) { | |
var options = { 'limit': OBJECTS_PER_REQUEST }; | |
if (startingAfter) { | |
options['starting_after'] = startingAfter; | |
} | |
stripe.customers.list(options, function(err, customers) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
if (!customers) { | |
return; | |
} | |
console.log('Customers=' + customers['data'].length); | |
if (!customers['data'].length) { | |
return; | |
} | |
for (var i = 0; i < customers['data'].length; i++) { | |
var customer = customers['data'][i]; | |
processSubs(null, customer['subscriptions']); | |
} | |
if (customers['has_more']) { | |
var customer = customers['data'][customers['data'].length - 1]; | |
customerList(customer['id']); | |
} | |
}); | |
} | |
customerList(null); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this script worked well for me, thanks @kennberg