mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 17:45:40 -05:00
Update daily Stripe subscriber counts script
This commit is contained in:
parent
356a70fafa
commit
ca761866c0
1 changed files with 81 additions and 28 deletions
|
@ -1,5 +1,7 @@
|
|||
// Find latest Stripe basic subscribers who haven't cancelled
|
||||
|
||||
// TODO: doesn't handle 11+ sponsored subs for a given customer
|
||||
|
||||
if (process.argv.length !== 3) {
|
||||
console.log("Usage: node <script> <API key>");
|
||||
process.exit();
|
||||
|
@ -11,36 +13,87 @@ var stripe = require("stripe")(apiKey);
|
|||
var yesterday = new Date();
|
||||
yesterday.setUTCDate(yesterday.getUTCDate() - 1);
|
||||
|
||||
stripe.customers.list({ limit: 40 }, function(err, customers) {
|
||||
// asynchronously called
|
||||
for (var i = 0; i < customers.data.length; i++) {
|
||||
var customer = customers.data[i];
|
||||
var created = new Date(customer.created * 1000);
|
||||
// console.log(created, yesterday);
|
||||
// if (created > yesterday) continue;
|
||||
var earliestDate = new Date("2015-03-01T00:00:00.000Z");
|
||||
|
||||
if (customer.subscriptions && customer.subscriptions.data.length > 0) {
|
||||
var basic = false;
|
||||
// console.log(customer.id + " " + created.toISOString() + " " + customer.email);
|
||||
for (var j = 0;j < customer.subscriptions.data.length; j++) {
|
||||
var subscription = customer.subscriptions.data[j];
|
||||
// console.log(subscription.id + " " + subscription.plan.id + " " + subscription.cancel_at_period_end);
|
||||
if (subscription.plan.id === 'basic') {
|
||||
if (subscription.cancel_at_period_end === false) {
|
||||
basic = true;
|
||||
break;
|
||||
}
|
||||
// else {
|
||||
// console.log("CANCELLED", customer.id);
|
||||
// }
|
||||
}
|
||||
var subs = {};
|
||||
|
||||
getSubscriptions(null, function () {
|
||||
console.log('999 recurring sub counts:');
|
||||
for (var created in subs) {
|
||||
for (var amount in subs[created]) {
|
||||
if (amount === '999') {
|
||||
console.log(created, subs[created][amount]['recurring'])
|
||||
}
|
||||
if (basic) {
|
||||
console.log(created.toISOString(), customer.email);
|
||||
}
|
||||
// else {
|
||||
// console.log("NO SUB", customer.id);
|
||||
// }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function getSubscriptions(starting_after, done)
|
||||
{
|
||||
var options = {limit: 100};
|
||||
if (starting_after) options.starting_after = starting_after;
|
||||
|
||||
stripe.customers.list(options, function(err, customers) {
|
||||
for (var i = 0; i < customers.data.length; i++) {
|
||||
var customer = customers.data[i];
|
||||
|
||||
if (customer.subscriptions && customer.subscriptions.data.length > 0) {
|
||||
var basic = false;
|
||||
for (var j = 0;j < customer.subscriptions.data.length; j++) {
|
||||
var subscription = customer.subscriptions.data[j];
|
||||
if (subscription.plan.id === 'basic') {
|
||||
|
||||
var created = new Date(subscription.start * 1000);
|
||||
if (created < earliestDate) continue;
|
||||
created = created.toISOString().substring(0, 10);
|
||||
if (!subs[created]) subs[created] = {};
|
||||
|
||||
var amount = subscription.plan.amount;
|
||||
if (subscription.discount && subscription.discount.coupon) {
|
||||
if (subscription.discount.coupon.percent_off) {
|
||||
amount = amount * (100 - subscription.discount.coupon.percent_off) / 100;
|
||||
}
|
||||
else if (subscription.discount.coupon.amount_off) {
|
||||
amount -= subscription.discount.coupon.amount_off;
|
||||
}
|
||||
}
|
||||
else if (customer.discount && customer.discount.coupon) {
|
||||
if (customer.discount.coupon.percent_off) {
|
||||
amount = amount * (100 - customer.discount.coupon.percent_off) / 100;
|
||||
}
|
||||
else if (customer.discount.coupon.amount_off) {
|
||||
amount -= customer.discount.coupon.amount_off;
|
||||
}
|
||||
}
|
||||
|
||||
if (!subs[created][amount]) subs[created][amount] = {};
|
||||
|
||||
if (subscription.cancel_at_period_end === true) {
|
||||
if (!subs[created][amount]['cancelled']) subs[created][amount]['cancelled'] = 0;
|
||||
subs[created][amount]['cancelled']++;
|
||||
}
|
||||
else {
|
||||
if (!subs[created][amount]['recurring']) subs[created][amount]['recurring'] = {};
|
||||
|
||||
if (customer.alipay_accounts && customer.alipay_accounts.total_count) {
|
||||
if (!subs[created][amount]['recurring']['alipay']) subs[created][amount]['recurring']['alipay'] = 0;
|
||||
subs[created][amount]['recurring']['alipay']++;
|
||||
}
|
||||
else {
|
||||
if (!subs[created][amount]['recurring']['card']) subs[created][amount]['recurring']['card'] = 0;
|
||||
subs[created][amount]['recurring']['card']++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (customers.has_more) {
|
||||
getSubscriptions(customers.data[customers.data.length - 1].id, done);
|
||||
}
|
||||
else {
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue