From 900d8e7dfab87707fe43e1c5b7606832130ffba6 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Wed, 17 Sep 2014 15:55:19 +0200 Subject: [PATCH] fix(emails): re-add emails for payments, @lefnire should be ok now I had used req.user but it did not exist. There is a way to test payments locally? Because even now I can not verify it is really ok --- src/controllers/payments.js | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/controllers/payments.js b/src/controllers/payments.js index fb3f5cdf81..85ed42e7b5 100644 --- a/src/controllers/payments.js +++ b/src/controllers/payments.js @@ -10,6 +10,7 @@ var User = require('./../models/user').model; var ga = require('./../utils').ga; var logging = require('./../logging'); var userAPI = require('./user'); +var request = require('request'); var api = module.exports; var isProduction = nconf.get("NODE_ENV") === "production"; @@ -54,6 +55,37 @@ function createSubscription(user, data) { revealMysteryItems(user); user.purchased.txnCount++; ga.event('subscribe', data.paymentMethod).send() + if(isProduction){ + 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; + } + + if(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: 'subscription-begins', + to: { + name: name, + email: email + } + } + } + }); + } + } ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod + " > Stripe").send(); } @@ -74,6 +106,37 @@ function buyGems(user, data) { user.purchased.txnCount++; ga.event('checkout', data.paymentMethod).send(); ga.transaction(data.customerId, 5).item(5, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send(); + if(isProduction){ + 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; + } + + if(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: 'donation', + to: { + name: name, + email: email + } + } + } + }); + } + } } // Expose some functions for tests