diff --git a/public/js/controllers/groupsCtrl.js b/public/js/controllers/groupsCtrl.js index 9f433fdf37..a9f744839c 100644 --- a/public/js/controllers/groupsCtrl.js +++ b/public/js/controllers/groupsCtrl.js @@ -129,13 +129,14 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', ' $scope.gift = { type: 'gems', gems: {amount:0, fromBalance:true}, - subscription: {months:1}, + subscription: {months:0}, message:'' }; $scope.sendGift = function(uuid, gift){ $http.post('/api/v2/members/'+uuid+'/gift', gift).success(function(){ Notification.text('Gift sent!') $rootScope.User.sync(); + $scope.$close(); }) } } diff --git a/public/js/controllers/rootCtrl.js b/public/js/controllers/rootCtrl.js index 004660dd6a..cbce9d31fb 100644 --- a/public/js/controllers/rootCtrl.js +++ b/public/js/controllers/rootCtrl.js @@ -140,11 +140,6 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$ $rootScope.flash[type].splice($index, 1); } - $rootScope.encodeGift = function(uuid, gift){ - gift.uuid = uuid; - return JSON.stringify(gift); - } - $scope.contribText = function(contrib, backer){ if (!contrib && !backer) return; if (backer && backer.npc) return backer.npc; diff --git a/public/js/services/paymentServices.js b/public/js/services/paymentServices.js index 7b7f5baf9e..929d0e432c 100644 --- a/public/js/services/paymentServices.js +++ b/public/js/services/paymentServices.js @@ -69,5 +69,10 @@ function($rootScope, User, $http, Content) { window.location.href = '/' + plan.paymentMethod.toLowerCase() + '/subscribe/cancel?_id=' + user._id + '&apiToken=' + user.apiToken; } + Payments.encodeGift = function(uuid, gift){ + gift.uuid = uuid; + return JSON.stringify(gift); + } + return Payments; }]); diff --git a/src/controllers/payments/index.js b/src/controllers/payments/index.js index be5fdff248..176098b4c5 100644 --- a/src/controllers/payments/index.js +++ b/src/controllers/payments/index.js @@ -29,12 +29,12 @@ exports.createSubscription = function(data, cb) { var recipient = data.gift ? data.gift.member : data.user; //if (!recipient.purchased.plan) recipient.purchased.plan = {}; // FIXME double-check, this should never be the case var p = recipient.purchased.plan; - var months = data.gift ? data.gift.subscription.months : data.sub.months; + var months = +(data.gift ? data.gift.subscription.months : data.sub.months); var block = shared.content.subscriptionBlocks[months]; if (data.gift) { if (p.customerId && !p.dateTerminated) { // User has active plan - p.extraMonths += +months; + p.extraMonths += months; } else { p.dateTerminated = moment(p.dateTerminated).add({months: months}).toDate(); } @@ -65,7 +65,7 @@ exports.createSubscription = function(data, cb) { } revealMysteryItems(recipient); if(isProduction) { - data.gift && utils.txnEmail(data.user, 'subscription-begins'); + if (!data.gift) utils.txnEmail(data.user, 'subscription-begins'); utils.ga.event('subscribe', data.paymentMethod).send(); utils.ga.transaction(data.user._id, block.price).item(block.price, 1, data.paymentMethod.toLowerCase() + '-subscription', data.paymentMethod).send(); } @@ -102,7 +102,7 @@ exports.buyGems = function(data, cb) { (data.gift ? data.gift.member : data.user).balance += amt; data.user.purchased.txnCount++; if(isProduction) { - utils.txnEmail(data.user, 'donation'); + if (!data.gift) utils.txnEmail(data.user, 'donation'); utils.ga.event('checkout', data.paymentMethod).send(); //TODO ga.transaction to reflect whether this is gift or self-purchase utils.ga.transaction(data.user._id, amt).item(amt, 1, data.paymentMethod.toLowerCase() + "-checkout", "Gems > " + data.paymentMethod).send(); diff --git a/src/controllers/payments/paypal.js b/src/controllers/payments/paypal.js index 25195b5695..9c7a1d4f3a 100644 --- a/src/controllers/payments/paypal.js +++ b/src/controllers/payments/paypal.js @@ -24,12 +24,14 @@ paypal.configure({ }); var parseErr = function(res, err){ - var error = err.response ? err.response.message || err.response.details[0].issue : err; + //var error = err.response ? err.response.message || err.response.details[0].issue : err; + var error = JSON.stringify(err); return res.json(400,{err:error}); } exports.createBillingAgreement = function(req,res,next){ - var block = req.session.paypalBlock = shared.content.subscriptionBlocks[req.query.sub]; + req.session.paypalBlock = req.query.sub; + var block = shared.content.subscriptionBlocks[req.query.sub]; var billingPlanTitle = "HabitRPG Subscription" + ' ($'+block.price+' every '+block.months+' months, recurring)'; var billingAgreementAttributes = { "name": billingPlanTitle, @@ -51,29 +53,33 @@ exports.createBillingAgreement = function(req,res,next){ } exports.executeBillingAgreement = function(req,res,next){ - async.waterfall([ - function(cb){ + var block = shared.content.subscriptionBlocks[req.session.paypalBlock]; + delete req.session.paypalBlock; + async.auto({ + exec: function (cb) { paypal.billingAgreement.execute(req.query.token, {}, cb); }, - function(billingAgreement, cb){ - User.findById(req.session.userId, function(err, user){ - if (err) return cb(err); - cb(null, {billingAgreement:billingAgreement, user:user}); - }); + get_user: function (cb) { + User.findById(req.session.userId, cb); }, - function(data, cb){ - payments.createSubscription({user:data.user, customerId: data.billingAgreement.id, paymentMethod: 'Paypal', sub:req.session.paypalBlock}); - data.user.save(cb); - } - ],function(err){ + create_sub: ['exec', 'get_user', function (cb, results) { + payments.createSubscription({ + user: results.get_user, + customerId: results.exec.id, + paymentMethod: 'Paypal', + sub: block + }, cb); + }] + },function(err){ if (err) return parseErr(res, err); res.redirect('/'); }) } -exports.createPayment = function(req, res, next) { +exports.createPayment = function(req, res) { // if we're gifting to a user, put it in session for the `execute()` - var gift = req.session.gift = req.query.gift ? JSON.parse(req.query.gift) : undefined; + req.session.gift = req.query.gift || undefined; + var gift = req.query.gift ? JSON.parse(req.query.gift) : undefined; var price = !gift ? 5.00 : gift.type=='gems' ? Number(gift.gems.amount/4).toFixed(2) : Number(shared.content.subscriptionBlocks[gift.subscription.months].price).toFixed(2); @@ -113,10 +119,11 @@ exports.createPayment = function(req, res, next) { }); } -exports.executePayment = function(req, res, next) { +exports.executePayment = function(req, res) { var paymentId = req.query.paymentId, PayerID = req.query.PayerID, - gift = req.session.gift; + gift = req.session.gift ? JSON.parse(req.session.gift) : undefined; + delete req.session.gift; async.waterfall([ function(cb){ paypal.payment.execute(paymentId, {payer_id: PayerID}, cb); diff --git a/views/shared/modals/members.jade b/views/shared/modals/members.jade index b495489078..595929c819 100644 --- a/views/shared/modals/members.jade +++ b/views/shared/modals/members.jade @@ -87,5 +87,5 @@ script(type='text/ng-template', id='modals/send-gift.html') - var fromBal = "gift.type=='gems' && gift.gems.fromBalance" button.btn.btn-primary(ng-show=fromBal, ng-click='sendGift(profile._id, gift)')=env.t("send") a.btn.btn-primary(ng-hide=fromBal, ng-click='Payments.showStripe({gift:gift, uuid:profile._id})')=env.t('card') - a.btn.btn-warning(ng-hide=fromBal, href='/paypal/checkout?_id={{::user._id}}&apiToken={{::user.apiToken}}&gift={{encodeGift(profile._id, gift)}}') PayPal + a.btn.btn-warning(ng-hide=fromBal, href='/paypal/checkout?_id={{::user._id}}&apiToken={{::user.apiToken}}&gift={{Payments.encodeGift(profile._id, gift)}}') PayPal button.btn.btn-default(ng-click='$close()')=env.t('cancel') \ No newline at end of file