From 09cd13242022a80498f544d793d181a2d4aa4595 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sun, 6 Oct 2013 16:52:54 -0700 Subject: [PATCH] paypal: begin adding paypal alternative to credit-card payments --- bower.json | 6 +++--- package.json | 3 ++- src/controllers/user.js | 26 ++++++++++++++++++++++++++ src/middleware.js | 1 + src/routes/api.js | 1 + views/shared/modals/why-ads.jade | 7 ++++--- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/bower.json b/bower.json index 60228a30c9..358ec0dfa0 100644 --- a/bower.json +++ b/bower.json @@ -32,12 +32,12 @@ "angular-route": "1.2.0-rc.2", "angular-ui-utils": "~0.0.4", "angular-sanitize": "1.2.0-rc.2", - "marked": "~0.2.9" + "marked": "~0.2.9", + "JavaScriptButtons": "git://github.com/paypal/JavaScriptButtons.git#master" }, "resolutions": { "jquery": "~2.0.3", - "angular": "1.2.0-rc.2", - "angular-mocks": "1.2.0-rc.2" + "angular": "1.2.0-rc.2" }, "devDependencies": { "angular-mocks": "1.2.0-rc.2" diff --git a/package.json b/package.json index 1939c440e2..7ef714c5fa 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,8 @@ "passport": "~0.1.17", "validator": "~1.5.1", "nodemailer": "~0.5.2", - "grunt-cli": "~0.1.9" + "grunt-cli": "~0.1.9", + "paypal-ipn": "~1.0.1" }, "private": true, "subdomain": "habitrpg", diff --git a/src/controllers/user.js b/src/controllers/user.js index c1373777d2..6970a89e35 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -3,6 +3,7 @@ // fixme remove this junk, was coffeescript compiled (probably for IE8 compat) var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; +var url = require('url'); var _ = require('lodash'); var nconf = require('nconf'); var async = require('async'); @@ -591,6 +592,31 @@ api.buyGems = function(req, res) { }); }; +api.buyGemsPaypalIPN = function(req, res) { + var ipn = require('paypal-ipn'); + + ipn.verify(req.body, function callback(err, msg) { + if (err) { + console.error(msg); + res.send(500, msg); + } else { + if (req.body.payment_status == 'Completed') { + //Payment has been confirmed as completed + var parts = url.parse(req.body.custom, true); + var uid = parts.query.uid; //, apiToken = query.apiToken; + if (!uid) throw new Error("uuid or apiToken not found when completing paypal transaction"); + User.findById(uid, function(err, user) { + if (err) throw err; + if (_.isEmpty(user)) throw "user not found with uuid " + uuid + " when completing paypal transaction" + user.balance += 5; + user.flags.ads = 'hide'; + user.save(); + }); + } + } + }); +} + /* ------------------------------------------------------------------------ Tags diff --git a/src/middleware.js b/src/middleware.js index e2e5543098..1b4cadd396 100644 --- a/src/middleware.js +++ b/src/middleware.js @@ -63,6 +63,7 @@ module.exports.locals = function(req, res, next) { res.locals.habitrpg = res.locals.habitrpg || {} _.defaults(res.locals.habitrpg, { NODE_ENV: nconf.get('NODE_ENV'), + BASE_URL: nconf.get('BASE_URL'), IS_MOBILE: /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(req.header('User-Agent')), STRIPE_PUB_KEY: nconf.get('STRIPE_PUB_KEY'), getBuildUrl: getBuildUrl diff --git a/src/routes/api.js b/src/routes/api.js index 91346a424b..86f995239c 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -47,6 +47,7 @@ router.post('/user/revive', auth.auth, cron, user.revive); router.post('/user/batch-update', auth.auth, cron, user.batchUpdate); router.post('/user/reroll', auth.auth, cron, user.reroll); router.post('/user/buy-gems', auth.auth, user.buyGems); +router.post('/user/buy-gems/paypal-ipn', user.buyGemsPaypalIPN); router.post('/user/reset', auth.auth, user.reset); router['delete']('/user', auth.auth, user['delete']); diff --git a/views/shared/modals/why-ads.jade b/views/shared/modals/why-ads.jade index 38dd5a8214..2e35033178 100644 --- a/views/shared/modals/why-ads.jade +++ b/views/shared/modals/why-ads.jade @@ -4,8 +4,9 @@ div(modal='modals.whyAds') .modal-body p. Habit is an open source project, and can use all the help it can get - consider this a donation to the contributors. You also get 20 Gems from the purchase, which you can use to buy special items. - p - | "Hey, I backed the Kickstarter!" - follow - a(target='_blank', href='http://community.habitrpg.com/node/22') these instructions. + p. + "Hey, I backed the Kickstarter!" - follow these instructions. + script(src='/bower_components/JavaScriptButtons/dist/paypal-button.min.js?merchant=tylerrenelle-facilitator@gmail.com', data-button='buynow', data-name='20 Gems, Disable Ads, Donation to the Developers', data-quantity='1', data-amount='.1', data-currency='USD', data-shipping='0', data-tax='0', data-callback='#{env.BASE_URL}/api/v1/user/buy-gems/paypal-ipn', data-env='sandbox', data-custom='?uid={{user._id}}&apiToken={{user.apiToken}}') + .modal-footer button.btn.btn-default.cancel(ng-click='modals.whyAds = false') Ok \ No newline at end of file