2015-03-16 12:10:07 -07:00
// Find latest Stripe basic subscribers who haven't cancelled
2015-03-26 14:12:17 -07:00
// TODO: doesn't handle 11+ sponsored subs for a given customer
2015-03-16 12:10:07 -07:00
if ( process . argv . length !== 3 ) {
console . log ( "Usage: node <script> <API key>" ) ;
process . exit ( ) ;
}
var apiKey = process . argv [ 2 ] ;
var stripe = require ( "stripe" ) ( apiKey ) ;
var yesterday = new Date ( ) ;
yesterday . setUTCDate ( yesterday . getUTCDate ( ) - 1 ) ;
2015-03-26 14:12:17 -07:00
var earliestDate = new Date ( "2015-03-01T00:00:00.000Z" ) ;
var subs = { } ;
getSubscriptions ( null , function ( ) {
2015-04-14 15:01:00 -07:00
console . log ( 'Recurring sub counts:' ) ;
2015-03-26 14:12:17 -07:00
for ( var created in subs ) {
for ( var amount in subs [ created ] ) {
2015-04-14 15:01:00 -07:00
if ( parseInt ( amount ) > 0 ) {
console . log ( created , amount , subs [ created ] [ amount ] [ 'recurring' ] )
2015-03-16 12:10:07 -07:00
}
}
}
} ) ;
2015-03-26 14:12:17 -07:00
2015-03-27 11:22:21 -07:00
function getSubscriptions ( starting _after , done )
2015-03-26 14:12:17 -07:00
{
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 ( ) ;
}
} ) ;
}