2014-03-25 02:41:50 +00:00
|
|
|
/* @see ./routes.coffee for routing*/
|
|
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
var logger = require('../logging');
|
|
|
|
|
var ipn = require('paypal-ipn');
|
|
|
|
|
var shared = require('habitrpg-shared');
|
|
|
|
|
var nconf = require('nconf');
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var User = require('./../models/user').model;
|
|
|
|
|
var ga = require('./../utils').ga;
|
|
|
|
|
var logging = require('./../logging');
|
|
|
|
|
var userAPI = require('./user');
|
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-03-25 02:41:50 +00:00
|
|
|
|
|
|
|
|
var PaypalRecurring = require('paypal-recurring');
|
|
|
|
|
var paypalRecurring = new PaypalRecurring({
|
|
|
|
|
username: nconf.get('PAYPAL_USERNAME'),
|
|
|
|
|
password: nconf.get('PAYPAL_PASSWORD'),
|
|
|
|
|
signature: nconf.get('PAYPAL_SIGNATURE')
|
|
|
|
|
}, isProduction ? "production" : "sandbox");
|
|
|
|
|
var paypalCheckout = require('paypal-express-checkout')
|
|
|
|
|
.init(nconf.get('PAYPAL_USERNAME'), nconf.get('PAYPAL_PASSWORD'), nconf.get('PAYPAL_SIGNATURE'), nconf.get('BASE_URL'), nconf.get('BASE_URL'), !isProduction);
|
|
|
|
|
|
|
|
|
|
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-09-20 21:53:14 +00:00
|
|
|
function getMailingInfo(user) {
|
|
|
|
|
var email, name;
|
|
|
|
|
if(user.auth.local){
|
|
|
|
|
email = user.auth.local.email;
|
|
|
|
|
name = user.profile.name || user.auth.local.username;
|
|
|
|
|
}else if(user.auth.facebook && user.auth.facebook.emails && user.auth.facebook.emails[0] && user.auth.facebook.emails[0].value){
|
|
|
|
|
email = user.auth.facebook.emails[0].value;
|
|
|
|
|
name = user.auth.facebook.displayName || user.auth.facebook.username;
|
|
|
|
|
}
|
|
|
|
|
return {'email': email, 'name': name};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function emailUser(user, emailType) {
|
2014-10-04 20:29:56 +00:00
|
|
|
var mailingInfo = getMailingInfo(user);
|
2014-09-20 21:53:14 +00:00
|
|
|
if(mailingInfo.email){
|
|
|
|
|
request({
|
|
|
|
|
url: nconf.get('EMAIL_SERVER_URL') + '/job',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
auth: {
|
|
|
|
|
user: nconf.get('EMAIL_SERVER_AUTH_USER'),
|
|
|
|
|
pass: nconf.get('EMAIL_SERVER_AUTH_PASSWORD')
|
|
|
|
|
},
|
|
|
|
|
json: {
|
|
|
|
|
type: 'email',
|
|
|
|
|
data: {
|
|
|
|
|
emailType: emailType,
|
|
|
|
|
to: {
|
|
|
|
|
name: mailingInfo.name,
|
|
|
|
|
email: mailingInfo.email
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
options: {
|
|
|
|
|
attemps: 5
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 02:41:50 +00:00
|
|
|
function createSubscription(user, data) {
|
|
|
|
|
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-10-06 18:03:55 +00:00
|
|
|
if(isProduction) emailUser(user, 'subscription-begins');
|
2014-03-25 02:41:50 +00:00
|
|
|
user.purchased.txnCount++;
|
2014-09-20 21:53:14 +00:00
|
|
|
ga.event('subscribe', data.paymentMethod).send();
|
2014-03-25 02:41:50 +00:00
|
|
|
ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod + " > Stripe").send();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:18:27 +00:00
|
|
|
/**
|
|
|
|
|
* Sets their subscription to be cancelled later
|
|
|
|
|
*/
|
2014-03-25 02:41:50 +00:00
|
|
|
function cancelSubscription(user, data){
|
2014-05-06 05:18:27 +00:00
|
|
|
var du = user.purchased.plan.dateUpdated, now = moment();
|
2014-10-06 18:03:55 +00:00
|
|
|
if(isProduction) emailUser(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-01 11:01:27 +00:00
|
|
|
.add(1, 'month')
|
2014-05-06 05:18:27 +00:00
|
|
|
.toDate();
|
2014-03-25 02:41:50 +00:00
|
|
|
ga.event('unsubscribe', 'Stripe').send();
|
2014-10-06 18:03:55 +00:00
|
|
|
|
2014-03-25 02:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buyGems(user, data) {
|
|
|
|
|
user.balance += 5;
|
|
|
|
|
user.purchased.txnCount++;
|
2014-10-06 18:03:55 +00:00
|
|
|
if(isProduction) emailUser(user, 'donation');
|
2014-03-25 02:41:50 +00:00
|
|
|
ga.event('checkout', data.paymentMethod).send();
|
|
|
|
|
ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:18:27 +00:00
|
|
|
// Expose some functions for tests
|
|
|
|
|
if (nconf.get('NODE_ENV')==='testing') {
|
|
|
|
|
api.cancelSubscription = cancelSubscription;
|
|
|
|
|
api.createSubscription = createSubscription;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
plan: req.query.plan,
|
|
|
|
|
}, 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-03-25 02:41:50 +00:00
|
|
|
api.paypalSubscribe = function(req,res,next) {
|
|
|
|
|
// Authenticate a future subscription of ~5 USD
|
|
|
|
|
paypalRecurring.authenticate({
|
2014-10-04 20:29:56 +00:00
|
|
|
RETURNURL: nconf.get('BASE_URL') + '/paypal/subscribe/success?uuid=' + res.locals.user._id,
|
2014-03-25 02:41:50 +00:00
|
|
|
CANCELURL: nconf.get("BASE_URL"),
|
|
|
|
|
PAYMENTREQUEST_0_AMT: 5,
|
|
|
|
|
L_BILLINGAGREEMENTDESCRIPTION0: "HabitRPG Subscription"
|
|
|
|
|
}, function(err, data, url) {
|
|
|
|
|
// Redirect the user if everything went well with
|
|
|
|
|
// a HTTP 302 according to PayPal's guidelines
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
res.redirect(302, url);
|
|
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-03-25 02:41:50 +00:00
|
|
|
|
|
|
|
|
api.paypalSubscribeSuccess = function(req,res,next) {
|
|
|
|
|
// Create a subscription of 10 USD every month
|
|
|
|
|
var uuid = req.query.uuid;
|
|
|
|
|
if (!uuid) return next("UUID required");
|
|
|
|
|
paypalRecurring.createSubscription(req.query.token, req.query.PayerID,{
|
|
|
|
|
AMT: 5,
|
|
|
|
|
DESC: "HabitRPG Subscription",
|
|
|
|
|
BILLINGPERIOD: "Month",
|
|
|
|
|
BILLINGFREQUENCY: 1,
|
|
|
|
|
}, function(err, data) {
|
|
|
|
|
if (err) return res.next(err);
|
|
|
|
|
User.findById(uuid, function(err,user){
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
createSubscription(user, {customerId: data.PROFILEID, paymentMethod: 'Paypal'});
|
|
|
|
|
user.save(function(err,saved){
|
|
|
|
|
res.redirect('/');
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-03-25 02:41:50 +00:00
|
|
|
|
|
|
|
|
api.paypalSubscribeCancel = 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) {
|
|
|
|
|
paypalRecurring.modifySubscription(user.purchased.plan.customerId, 'cancel', cb);
|
|
|
|
|
},
|
|
|
|
|
function(response, cb) {
|
|
|
|
|
cancelSubscription(user);
|
|
|
|
|
user.save(cb);
|
|
|
|
|
}
|
|
|
|
|
], function(err, saved){
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
api.paypalCheckout = function(req, res, next) {
|
2014-10-04 20:29:56 +00:00
|
|
|
var opts = {RETURNURL:nconf.get('BASE_URL') + '/paypal/checkout/success?uuid=' + res.locals.user._id};
|
2014-09-20 21:53:14 +00:00
|
|
|
paypalCheckout.pay(+new Date(), 5, 'HabitRPG Gems', 'USD', opts, function(err, url) {
|
2014-03-25 02:41:50 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
|
res.redirect(url);
|
|
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-03-25 02:41:50 +00:00
|
|
|
|
|
|
|
|
api.paypalCheckoutSuccess = function(req,res,next) {
|
|
|
|
|
paypalCheckout.detail(req.query.token, req.query.PayerID, function(err, data, invoiceNumber, price) {
|
|
|
|
|
// see `data` vars at https://github.com/petersirka/node-paypal-express-checkout#paypal-account
|
|
|
|
|
//if (err) return next('PayPal Error: ' + msg);
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
if (data.ACK !== 'Success') return next('PayPal transaction failed, please try again');
|
|
|
|
|
|
|
|
|
|
var uuid = req.query.uuid; //, apiToken = query.apiToken;
|
|
|
|
|
User.findById(uuid , function(err, user) {
|
|
|
|
|
if (_.isEmpty(user)) err = "user not found with uuid " + uuid + " when completing paypal transaction";
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
buyGems(user, {customerId:req.query.PayerID, paymentMethod:'Paypal'});
|
|
|
|
|
user.save(function(){
|
|
|
|
|
if (err) return next(err);
|
|
|
|
|
res.redirect('/');
|
2014-10-04 20:29:56 +00:00
|
|
|
uuid = null;
|
2014-03-25 02:41:50 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|
2014-03-25 03:51:49 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* General IPN handler. We could use this for all paypal transaction handling (instead of the above functions), but I've
|
|
|
|
|
* found it extremely unreliable. Instead, here we'll cancel HabitRPG subscriptions for users who manually cancel their
|
|
|
|
|
* recurring paypal payments.
|
|
|
|
|
*/
|
|
|
|
|
api.paypalIPN = function(req, res, next) {
|
|
|
|
|
// Must respond to PayPal IPN request with an empty 200 first, if using Express uncomment the following:
|
|
|
|
|
res.send(200);
|
|
|
|
|
ipn.verify(req.body, function callback(err, msg) {
|
|
|
|
|
if (err) return logger.error(msg);
|
|
|
|
|
switch (req.body.txn_type) {
|
|
|
|
|
// TODO what's the diff b/w the two data.txn_types below? The docs recommend subscr_cancel, but I'm getting the other one instead...
|
|
|
|
|
case 'recurring_payment_profile_cancel':
|
|
|
|
|
case 'subscr_cancel':
|
|
|
|
|
User.findOne({'purchased.plan.customerId':req.body.recurring_payment_id},function(err, user){
|
|
|
|
|
if (err) return logger.error(err);
|
|
|
|
|
if (_.isEmpty(user)) return; // looks like the cancellation was already handled properly above (see api.paypalSubscribeCancel)
|
|
|
|
|
cancelSubscription(user);
|
|
|
|
|
user.save();
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-09-20 21:53:14 +00:00
|
|
|
};
|