From a377501f64a6b02f39a1486e756a7d42c63e3415 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Tue, 9 Jun 2015 16:55:13 +0200 Subject: [PATCH] feat(amazon-payments): wip on donations --- config.json.example | 4 +- website/public/js/services/paymentServices.js | 38 ++++++++++++- website/src/controllers/payments/amazon.js | 56 +++++++++++++++++-- website/src/controllers/payments/index.js | 3 + website/src/routes/pages.js | 2 - website/src/routes/payments.js | 2 + website/views/shared/modals/buy-gems.jade | 4 +- 7 files changed, 98 insertions(+), 11 deletions(-) diff --git a/config.json.example b/config.json.example index 92ef165c17..ec7eed61f0 100644 --- a/config.json.example +++ b/config.json.example @@ -26,7 +26,9 @@ "AMAZON_PAYMENTS_CLIENT_ID": "AMAZON_PAYMENTS_CLIENT_ID", "AMAZON_PAYMENTS": { "SELLER_ID": "SELLER_ID", - "CLIENT_ID": "CLIENT_ID" + "CLIENT_ID": "CLIENT_ID", + "MWS_KEY": "", + "MWS_SECRET": "" }, "FLAG_REPORT_EMAIL": ["email@mod.com"], "EMAIL_SERVER": { diff --git a/website/public/js/services/paymentServices.js b/website/public/js/services/paymentServices.js index 14a91449e1..c4de57e7ad 100644 --- a/website/public/js/services/paymentServices.js +++ b/website/public/js/services/paymentServices.js @@ -79,7 +79,8 @@ function($rootScope, User, $http, Content) { var url = '/amazon/verifyAccessToken' $http.post(url, response).success(function(){ - console.log(arguments); + Payments.amazonDonationLoggedIn = true; + Payments.amazonDonationInitWidgets(); }).error(function(res){ alert(res.err); }); @@ -87,11 +88,44 @@ function($rootScope, User, $http, Content) { }, onError: function(error) { - console.error('amazon error ', error) + console.error('amazon error ', error); } }); } + Payments.amazonDonationLoggedIn = false; + Payments.amazonDonationPaymentSelected = false; + Payments.amazonDonationOrderReferenceId; + + Payments.amazonDonationInitWidgets = function(){ + new OffAmazonPayments.Widgets.Wallet({ + sellerId: window.env.AMAZON_PAYMENTS.SELLER_ID, + onOrderReferenceCreate: function(orderReference) { + Payments.amazonDonationOrderReferenceId = orderReference.getAmazonOrderReferenceId(); + }, + onPaymentSelect: function(orderReference) { + $rootScope.$apply(function(){ + Payments.amazonDonationPaymentSelected = true; + }); + }, + design: { + designMode: 'responsive' + }, + onError: function(error) { + console.error('amazon error ', error.getErrorMessage()); + } + }).bind('AmazonPayWalletDonation'); + } + + Payments.amazonDonationCheckout = function(){ + var url = '/amazon/checkout' + $http.post(url, {orderReferenceId: Payments.amazonDonationOrderReferenceId}).success(function(){ + console.log(arguments); + }).error(function(res){ + alert(res.err); + }); + } + // Needs to be called everytime the modal/router is accessed Payments.initAmazonSubscription = function(){ diff --git a/website/src/controllers/payments/amazon.js b/website/src/controllers/payments/amazon.js index 2484321a36..f6c79f07ea 100644 --- a/website/src/controllers/payments/amazon.js +++ b/website/src/controllers/payments/amazon.js @@ -1,13 +1,59 @@ var amazonPayments = require('amazon-payments'); +var nconf = require('nconf'); +var async = require('async'); +var isProd = nconf.get("NODE_ENV") === 'production'; var payment = amazonPayments.connect({ - environment: amazonPayments.Environment.Production, - sellerId: 'Amazon Seller ID', - mwsAccessKey: 'MWS Access Key', - mwsSecretKey: 'MWS Secret Key', - clientId: 'Client ID' + environment: amazonPayments.Environment.Sandbox, + sellerId: nconf.get('AMAZON_PAYMENTS:SELLER_ID'), + mwsAccessKey: nconf.get('AMAZON_PAYMENTS:MWS_KEY'), + mwsSecretKey: nconf.get('AMAZON_PAYMENTS:MWS_SECRET'), + clientId: nconf.get('AMAZON_PAYMENTS:CLIENT_ID') }); exports.verifyAccessToken = function(req, res, next){ + if(!req.body || !req.body['access_token']){ + return res.json(400, {err: 'Access token not supplied.'}); + } + + payment.api.getTokenInfo(req.body['access_token'], function(err, tokenInfo){ + if(err) return next(err); + + res.send(200); + }); +}; + +exports.checkout = function(req, res, next){ + if(!req.body || !req.body['orderReferenceId']){ + return res.json(400, {err: 'Order Reference Id not supplied.'}); + } + + async.series({ + setOrderReferenceDetails: function(cb){ + payment.offAmazonPayments.setOrderReferenceDetails({ + + }, cb); + }, + + confirmOrderReference: function(cb){ + payment.offAmazonPayments.confirmOrderReference({ + + }, cb); + }, + + authorize: function(cb){ + payment.offAmazonPayments.authorize({ + + }, cb); + }, + + closeOrderReference: function(cb){ + payment.offAmazonPayments.closeOrderReference({ + + }, cb); + } + }, function(err, results){ + + }); }; \ No newline at end of file diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index 3a86a101d9..51b39a6c92 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -7,6 +7,7 @@ var moment = require('moment'); var isProduction = nconf.get("NODE_ENV") === "production"; var stripe = require('./stripe'); var paypal = require('./paypal'); +var amazon = require('./amazon'); var members = require('../members') var async = require('async'); var iap = require('./iap'); @@ -169,5 +170,7 @@ exports.paypalCheckout = paypal.createPayment; exports.paypalCheckoutSuccess = paypal.executePayment; exports.paypalIPN = paypal.ipn; +exports.amazonVerifyAccessToken = amazon.verifyAccessToken; + exports.iapAndroidVerify = iap.androidVerify; exports.iapIosVerify = iap.iosVerify; diff --git a/website/src/routes/pages.js b/website/src/routes/pages.js index bf704772f4..1f9be4b624 100644 --- a/website/src/routes/pages.js +++ b/website/src/routes/pages.js @@ -12,8 +12,6 @@ router.get('/', i18n.getUserLanguage, middleware.locals, function(req, res) { if (!req.headers['x-api-user'] && !req.headers['x-api-key'] && !(req.session && req.session.userId)) return res.redirect('/static/front') - console.log('Starting to render'); - return res.render('index', { title: 'HabitRPG | Your Life, The Role Playing Game', env: res.locals.habitrpg diff --git a/website/src/routes/payments.js b/website/src/routes/payments.js index 8656db4d28..007ca2fd68 100644 --- a/website/src/routes/payments.js +++ b/website/src/routes/payments.js @@ -17,6 +17,8 @@ router.post("/stripe/subscribe/edit", auth.auth, i18n.getUserLanguage, payments. //router.get("/stripe/subscribe", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribe); // checkout route is used (above) with ?plan= instead router.get("/stripe/subscribe/cancel", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribeCancel); +router.post('/amazon/verifyAccessToken', auth.auth, i18n.getUserLanguage, payments.amazonVerifyAccessToken); + router.post("/iap/android/verify", auth.authWithUrl, /*i18n.getUserLanguage, */payments.iapAndroidVerify); router.post("/iap/ios/verify", auth.auth, /*i18n.getUserLanguage, */ payments.iapIosVerify); diff --git a/website/views/shared/modals/buy-gems.jade b/website/views/shared/modals/buy-gems.jade index 117ecfefc7..7ce184ec5b 100644 --- a/website/views/shared/modals/buy-gems.jade +++ b/website/views/shared/modals/buy-gems.jade @@ -23,7 +23,9 @@ script(id='modals/buyGems.html', type='text/ng-template') .btn.btn-primary(ng-click='Payments.showStripe({})')=env.t('card') a.btn.btn-warning(href='/paypal/checkout?_id={{user._id}}&apiToken={{user.apiToken}}') PayPal div#AmazonPayButtonDonation(ng-init="Payments.initAmazonDonation()") - + div#AmazonPayWalletDonation(ng-show="Payments.amazonDonationLoggedIn", style="width: 400px; height: 228px;") + a.btn.btn-warning(ng-show="Payments.amazonDonationPaymentSelected", ng-click="Payments.amazonDonationCheckout()") Checkout + div(ng-include="'partials/options.settings.subscription.html'") .modal-footer