Last active
January 4, 2016 17:59
-
-
Save Abdelkrim/8658078 to your computer and use it in GitHub Desktop.
This file contains 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 url = require('url'); | |
var nodedump = require('nodedump').init({ expand: true }).dump; | |
var stripeKPIs = require('../lib/stripeKPIs.js'); | |
var async = require('async'); | |
module.exports = function (app) { | |
app.get('/stripe/dashboard', function (req, res) { | |
var parts = url.parse(req.url, true); | |
var queryString = parts.query; | |
var payingCustomers = -1; | |
var apiKey = req.session.user.credentials.access_token; | |
console.log('/stripe/dashboard: queryString: ' + JSON.stringify(queryString)); | |
console.log('/stripe/dashboard: apiKey: ' + apiKey); | |
async.waterfall([function (callback) { | |
callback(null, stripeKPIs.payingCustomers(apiKey, new Date(2013, 1, 1), new Date(2013, 12, 1))); | |
}], | |
function (err, aSyncResults) { | |
payingCustomers = aSyncResults[0]; | |
console.log('*********************'); | |
console.log('callback called! ' + JSON.stringify(payingCustomers)); | |
}); | |
res.render('stripe/stripeDashboard.html', {payingCustomers: payingCustomers, session: req.session, nodedumps: [ | |
{_nodedump: nodedump(req.body, {label: 'req.body'})}, | |
{_nodedump: nodedump(req.session, {label: 'req.session'})}, | |
{_nodedump: nodedump(payingCustomers, {label: 'payingCustomers'})} | |
]}); | |
}); | |
}; |
This file contains 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 async = require('async'); | |
function StripeException(message) { | |
this.message = message; | |
this.name = "StripeException"; | |
} | |
function payingCustomers(apiKey, startDate, endDate, callback) { | |
// see https://stripe.com/docs/api/node#list_customers | |
// return(how many customers) | |
var kontinue = true; | |
var result = []; //json containing all customers.data | |
var stripe = require("stripe")(apiKey); | |
var count = 100; | |
var offset = 0; | |
while (kontinue) { | |
async.series([function (callback) { | |
stripe.customers.list({count: count, offset: offset/*, gte: startDate, lte: endDate*/}, function (err, customers) { | |
if (err) { | |
kontinue = false; | |
throw new StripeException('stripe.customers.list error: ' + err); | |
} | |
else { | |
console.log('collectCustomers: offset: ' + offset + ' - customers: ' + JSON.stringify(customers)); | |
callback(null, customers); | |
} | |
}); | |
}], | |
function (err, aSyncResults) { | |
console.log('count(' + count + ') - offset(' + offset + ')'); | |
console.log('inside the callback'); | |
if (err) { | |
console.errror('async callback error: ' + err); | |
return next(err); | |
} | |
var customers = aSyncResults[0]; | |
result = result.concat(customers.data); | |
if (customers.data.length < count) { | |
console.log('customers.data.length(' + customers.data.length + ') < count (' + count + ')'); | |
kontinue = false; | |
} | |
offset = result.length; | |
console.log('payingCustomers: offset: ' + offset + ' - result: ' + result.length); | |
console.log('aSyncResults: offset: ' + offset + ' - result: ' + aSyncResults[0].data.length); | |
}); | |
} | |
return(result); | |
} | |
module.exports.payingCustomers = payingCustomers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment