2014-03-25 02:41:50 +00:00
|
|
|
/* @see ./routes.coffee for routing*/
|
|
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
var logger = require('../logging');
|
|
|
|
|
var shared = require('habitrpg-shared');
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var User = require('./../models/user').model;
|
2014-11-07 01:25:44 +00:00
|
|
|
var utils = require('./../utils');
|
2014-03-25 02:41:50 +00:00
|
|
|
var logging = require('./../logging');
|
2014-09-17 13:55:19 +00:00
|
|
|
var request = require('request');
|
2014-09-20 21:53:14 +00:00
|
|
|
var moment = require('moment');
|
2014-03-25 02:41:50 +00:00
|
|
|
var api = module.exports;
|
|
|
|
|
var isProduction = nconf.get("NODE_ENV") === "production";
|
2014-10-04 20:29:56 +00:00
|
|
|
var stripe = require("stripe")(nconf.get('STRIPE_API_KEY'));
|
2014-11-15 00:47:51 +00:00
|
|
|
var paypal = require('./payments/paypal');
|
2014-03-25 02:41:50 +00:00
|
|
|
|
|
|
|
|
function revealMysteryItems(user) {
|
|
|
|
|
_.each(shared.content.gear.flat, function(item) {
|
|
|
|
|
if (
|
|
|
|
|
item.klass === 'mystery' &&
|
|
|
|
|
moment().isAfter(item.mystery.start) &&
|
|
|
|
|
moment().isBefore(item.mystery.end) &&
|
|
|
|
|
!user.items.gear.owned[item.key] &&
|
|
|
|
|
!~user.purchased.plan.mysteryItems.indexOf(item.key)
|
|
|
|
|
) {
|
|
|
|
|
user.purchased.plan.mysteryItems.push(item.key);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 00:47:51 +00:00
|
|
|
var createSubscription = api.createSubscription = function(user, data) {
|
2014-03-25 02:41:50 +00:00
|
|
|
if (!user.purchased.plan) user.purchased.plan = {};
|
|
|
|
|
_(user.purchased.plan)
|
|
|
|
|
.merge({ // override with these values
|
|
|
|
|
planId:'basic_earned',
|
|
|
|
|
customerId: data.customerId,
|
2014-09-20 21:53:14 +00:00
|
|
|
dateUpdated: new Date(),
|
2014-03-25 02:41:50 +00:00
|
|
|
gemsBought: 0,
|
2014-05-06 05:18:27 +00:00
|
|
|
paymentMethod: data.paymentMethod,
|
|
|
|
|
dateTerminated: null
|
2014-03-25 02:41:50 +00:00
|
|
|
})
|
|
|
|
|
.defaults({ // allow non-override if a plan was previously used
|
2014-09-20 21:53:14 +00:00
|
|
|
dateCreated: new Date(),
|
2014-03-25 02:41:50 +00:00
|
|
|
mysteryItems: []
|
|
|
|
|
});
|
|
|
|
|
revealMysteryItems(user);
|
2014-11-07 01:25:44 +00:00
|
|
|
if(isProduction) utils.txnEmail(user, 'subscription-begins');
|
2014-03-25 02:41:50 +00:00
|
|
|
user.purchased.txnCount++;
|
2014-11-07 01:25:44 +00:00
|
|
|
utils.ga.event('subscribe', data.paymentMethod).send();
|
|
|
|
|
utils.ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod + " > Stripe").send();
|
2014-03-25 02:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:18:27 +00:00
|
|
|
/**
|
|
|
|
|
* Sets their subscription to be cancelled later
|
|
|
|
|
*/
|
2014-11-15 00:47:51 +00:00
|
|
|
var cancelSubscription = api.cancelSubscription = function(user, data) {
|
2014-05-06 05:18:27 +00:00
|
|
|
var du = user.purchased.plan.dateUpdated, now = moment();
|
2014-11-07 01:25:44 +00:00
|
|
|
if(isProduction) utils.txnEmail(user, 'cancel-subscription');
|
2014-05-06 05:18:27 +00:00
|
|
|
user.purchased.plan.dateTerminated =
|
|
|
|
|
moment( now.format('MM') + '/' + moment(du).format('DD') + '/' + now.format('YYYY') )
|
2014-10-10 19:33:32 +00:00
|
|
|
.add({months:1})
|
2014-05-06 05:18:27 +00:00
|
|
|
.toDate();
|
2014-11-07 01:25:44 +00:00
|
|
|
utils.ga.event('unsubscribe', 'Stripe').send();
|
2014-03-25 02:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-15 00:47:51 +00:00
|
|
|
var buyGems = api.buyGems = function(user, data) {
|
2014-03-25 02:41:50 +00:00
|
|
|
user.balance += 5;
|
|
|
|
|
user.purchased.txnCount++;
|
2014-11-07 01:25:44 +00:00
|
|
|
if(isProduction) utils.txnEmail(user, 'donation');
|
|
|
|
|
utils.ga.event('checkout', data.paymentMethod).send();
|
|
|
|
|
utils.ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send();
|
2014-03-25 02:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Setup Stripe response when posting payment
|
|
|
|
|
*/
|
|
|
|
|
api.stripeCheckout = function(req, res, next) {
|
|
|
|
|
var token = req.body.id;
|
|
|
|
|
var user = res.locals.user;
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(cb){
|
|
|
|
|
if (req.query.plan) {
|
|
|
|
|
stripe.customers.create({
|
|
|
|
|
email: req.body.email,
|
|
|
|
|
metadata: {uuid: res.locals.user._id},
|
|
|
|
|
card: token,
|
2014-11-15 00:47:51 +00:00
|
|
|
plan: req.query.plan
|
2014-03-25 02:41:50 +00:00
|
|
|
}, cb);
|
|
|
|
|
} else {
|
|
|
|
|
stripe.charges.create({
|
|
|
|
|
amount: "500", // $5
|
|
|
|
|
currency: "usd",
|
|
|
|
|
card: token
|
|
|
|
|
}, cb);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function(response, cb) {
|
|
|
|
|
if (req.query.plan) {
|
|
|
|
|
createSubscription(user, {customerId: response.id, paymentMethod: 'Stripe'});
|
|
|
|
|
} else {
|
|
|
|
|
buyGems(user, {customerId: response.id, paymentMethod: 'Stripe'});
|
|
|
|
|
}
|
|
|
|
|
user.save(cb);
|
|
|
|
|
}
|
|
|
|
|
], function(err, saved){
|
|
|
|
|
if (err) return res.send(500, err.toString()); // don't json this, let toString() handle errors
|
|
|
|
|
res.send(200);
|
2014-10-04 20:29:56 +00:00
|
|
|
user = token = null;
|
2014-03-25 02:41:50 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
api.stripeSubscribeCancel = function(req, res, next) {
|
|
|
|
|
var user = res.locals.user;
|
|
|
|
|
if (!user.purchased.plan.customerId)
|
|
|
|
|
return res.json(401, {err: "User does not have a plan subscription"});
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(cb) {
|
|
|
|
|
stripe.customers.del(user.purchased.plan.customerId, cb);
|
|
|
|
|
},
|
|
|
|
|
function(response, cb) {
|
|
|
|
|
cancelSubscription(user);
|
|
|
|
|
user.save(cb);
|
|
|
|
|
}
|
|
|
|
|
], function(err, saved){
|
|
|
|
|
if (err) return res.send(500, err.toString()); // don't json this, let toString() handle errors
|
|
|
|
|
res.redirect('/');
|
2014-10-04 20:29:56 +00:00
|
|
|
user = null;
|
2014-03-25 02:41:50 +00:00
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-03-25 02:41:50 +00:00
|
|
|
|
2014-07-14 08:56:42 +00:00
|
|
|
api.stripeSubscribeEdit = function(req, res, next) {
|
|
|
|
|
var token = req.body.id;
|
|
|
|
|
var user = res.locals.user;
|
|
|
|
|
var user_id = user.purchased.plan.customerId;
|
|
|
|
|
var sub_id;
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(cb){
|
|
|
|
|
stripe.customers.listSubscriptions(user_id, cb);
|
|
|
|
|
},
|
|
|
|
|
function(response, cb) {
|
|
|
|
|
sub_id = response.data[0].id;
|
|
|
|
|
console.warn(sub_id);
|
|
|
|
|
console.warn([user_id, sub_id, { card: token }]);
|
|
|
|
|
stripe.customers.updateSubscription(user_id, sub_id, { card: token }, cb);
|
|
|
|
|
},
|
|
|
|
|
function(response, cb) {
|
|
|
|
|
user.save(cb);
|
|
|
|
|
}
|
|
|
|
|
], function(err, saved){
|
|
|
|
|
if (err) return res.send(500, err.toString()); // don't json this, let toString() handle errors
|
|
|
|
|
res.send(200);
|
2014-10-04 20:29:56 +00:00
|
|
|
token = user = user_id = sub_id;
|
2014-07-14 08:56:42 +00:00
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-07-14 08:56:42 +00:00
|
|
|
|
2014-11-15 00:47:51 +00:00
|
|
|
api.paypalSubscribe = paypal.createBillingAgreement;
|
|
|
|
|
api.paypalSubscribeSuccess = paypal.executeBillingAgreement;
|
|
|
|
|
api.paypalSubscribeCancel = paypal.cancelSubscription;
|
|
|
|
|
api.paypalCheckout = paypal.createPayment;
|
|
|
|
|
api.paypalCheckoutSuccess = paypal.executePayment;
|
|
|
|
|
api.paypalIPN = paypal.ipn;
|