dashboards-stripe-charges
Version:
Stripe charges plugin for segmentio/dashboards
166 lines (138 loc) • 4.86 kB
JavaScript
var Charges = require('stripe-charges');
var Dates = require('date-math');
/**
* Expose `ChargesDashboard`.
*/
module.exports = ChargesDashboard;
/**
* Return a Stripe dashboards data function.
* @param {String} key
* @param {Object} options
*/
function ChargesDashboard (key, options) {
if (!(this instanceof ChargesDashboard)) return new ChargesDashboard(key, options);
this.charges = Charges(key);
this.options = options || {};
var self = this;
return function () { return self.stripe.apply(self, arguments); };
}
/**
* Generate a Stripe data function.
*
* @param {String} key
* @param {Object} options
*
* @returns {Function}
*/
ChargesDashboard.prototype.stripe = function (data, callback) {
var self = this;
var results = data.stripe = (data.stripe || {});
// select all the charges
this.charges(new Date(0), new Date(), function (err, charges) {
if (err) return callback(err);
self.last2Days(charges, results);
self.daily(charges, results);
self.weekly(charges, results);
self.monthly(charges, results);
self.total(charges, results);
callback();
});
};
/**
* Calculate active subscriptions and charges for the last two days.
*
* @param {Customers} charges
* @param {Object} results
*/
ChargesDashboard.prototype.last2Days = function (charges, results) {
var today = new Date();
var todayCharges = this.paidCharges(charges, today, today);
results['charges today'] = todayCharges.count();
results['total charged today'] = todayCharges.total();
var yesterday = Dates.day.shift(new Date(), -1);
var yesterdayCharges = this.paidCharges(charges, yesterday, yesterday);
results['charges yesterday'] = yesterdayCharges.count();
results['total charged yesterday'] = yesterdayCharges.total();
var twoDaysAgo = Dates.day.shift(yesterday, -1);
var twoDaysAgoCharges = this.paidCharges(charges, twoDaysAgo, twoDaysAgo);
results['charges 2 days ago'] = twoDaysAgoCharges.count();
results['total charged 2 days ago'] = twoDaysAgoCharges.total();
};
/**
* Calculate active subscriptions for the past week.
*
* @param {Customers} charges
* @param {Object} results
*/
ChargesDashboard.prototype.daily = function (charges, results) {
var today = new Date();
var numbers = [];
var amounts = [];
for (var ago = 7; ago >= 0; ago -= 1) {
var current = Dates.day.shift(today, -ago);
var filtered = this.paidCharges(charges, current, current);
numbers.push(filtered.count());
amounts.push(filtered.total());
}
results['charges for the last week'] = numbers;
results['charge amounts for the last week'] = amounts;
};
/**
* Calculate the weekly active subscriptions and charges.
*
* @param {Customers} charges
* @param {Object} results
*/
ChargesDashboard.prototype.weekly = function (charges, results) {
var now = new Date();
var oneWeekAgo = Dates.day.shift(now, -7);
var oneWeekAgoCharges = this.paidCharges(charges, oneWeekAgo, now);
results['charges 0-1 weeks ago'] = oneWeekAgoCharges.count();
results['total charged 0-1 weeks ago'] = oneWeekAgoCharges.total();
var twoWeeksAgo = Dates.day.shift(oneWeekAgo, -7);
var twoWeeksAgoCharges = this.paidCharges(charges, twoWeeksAgo, oneWeekAgo);
results['charges 1-2 weeks ago'] = twoWeeksAgoCharges.count();
results['total charged 1-2 weeks ago'] = twoWeeksAgoCharges.total();
};
/**
* Calculate the monthly active subscriptions and charges.
*
* @param {Customers} charges
* @param {Object} results
*/
ChargesDashboard.prototype.monthly = function (charges, results) {
var now = new Date();
var oneMonthAgo = Dates.month.shift(now, -1);
var oneMonthAgoCharges = this.paidCharges(charges, oneMonthAgo, now);
results['charges 0-1 months ago'] = oneMonthAgoCharges.count();
results['total charged 0-1 months ago'] = oneMonthAgoCharges.total();
var twoMonthsAgo = Dates.month.shift(oneMonthAgo, -1);
var twoMonthsAgoCharges = this.paidCharges(charges, twoMonthsAgo, oneMonthAgo);
results['charges 1-2 months ago'] = twoMonthsAgoCharges.count();
results['total charged 1-2 months ago'] = twoMonthsAgoCharges.total();
};
/**
* Calculate the total active charges.
*
* @param {Customers} charges
* @param {Object} results
*/
ChargesDashboard.prototype.total = function (charges, results) {
charges = this.paidCharges(charges);
results['charges'] = charges.count();
results['total charged'] = charges.total();
};
/**
* Filter by active, paid charges.
*
* @param {Customers} charges
* @param {Date} start
* @param {Date} end
* @return {Charges}
*/
ChargesDashboard.prototype.paidCharges = function (charges, start, end) {
if (this.options.filter) charges = charges.filter(this.options.filter);
if (start && end) charges = charges.created(start, end);
charges = charges.paid(true).refunded(false);
return charges;
};