From de3246178725a264f94f54b596185e8cd45cc490 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sun, 5 Jul 2015 20:49:42 +0200 Subject: [PATCH] feat(amazon-payments): WIP add donations --- website/src/controllers/payments/amazon.js | 40 ++++++++++++---------- website/src/controllers/payments/index.js | 1 + website/src/routes/payments.js | 1 + website/views/shared/modals/buy-gems.jade | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/website/src/controllers/payments/amazon.js b/website/src/controllers/payments/amazon.js index 14fa7e6417..95ba846453 100644 --- a/website/src/controllers/payments/amazon.js +++ b/website/src/controllers/payments/amazon.js @@ -29,15 +29,34 @@ exports.verifyAccessToken = function(req, res, next){ }); }; +exports.createOrderReferenceId = function(req, res, next){ + if(!req.body || !req.body.billingAgreementId){ + return res.json(400, {err: 'Billing Agreement Id not supplied.'}); + } + + amzPayment.offAmazonPayments.createOrderReferenceForId({ + Id: req.body.billingAgreementId, + IdType: 'BillingAgreement' + }, function(err, response){ + if(err) return next(err); + if(!response.OrderReferenceDetails || !response.OrderReferenceDetails.AmazonOrderReferenceId){ + return next(new Error('Missing attributes in Amazon response.')); + } + + res.json({ + orderReferenceId: response.OrderReferenceDetails.AmazonOrderReferenceId + }); + }); +}; + exports.checkout = function(req, res, next){ - if(!req.body || !req.body['billingAgreementId']){ + if(!req.body || !req.body.orderReferenceId){ return res.json(400, {err: 'Billing Agreement Id not supplied.'}); } var gift = req.body.gift; var user = res.locals.user; - var billingAgreementId = req.body.billingAgreementId; - var orderReferenceId; + var orderReferenceId = req.body.orderReferenceId; var amount = 5; if(gift){ @@ -49,21 +68,6 @@ exports.checkout = function(req, res, next){ } async.series({ - createOrderReferenceForId: function(cb){ - amzPayment.offAmazonPayments.createOrderReferenceForId({ - Id: billingAgreementId, - IdType: 'BillingAgreement' - }, function(err, response){ - if(err) return cb(err); - if(!response.OrderReferenceDetails || !response.OrderReferenceDetails.AmazonOrderReferenceId){ - return cb('Missing attributes in Amazon response.'); - } - - orderReferenceId = response.OrderReferenceDetails.AmazonOrderReferenceId; - return cb(); - }); - }, - setOrderReferenceDetails: function(cb){ amzPayment.offAmazonPayments.setOrderReferenceDetails({ AmazonOrderReferenceId: orderReferenceId, diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index 8f1144cbcc..6206bfe187 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -174,6 +174,7 @@ exports.paypalCheckoutSuccess = paypal.executePayment; exports.paypalIPN = paypal.ipn; exports.amazonVerifyAccessToken = amazon.verifyAccessToken; +exports.amazonCreateOrderReferenceId = amazon.createOrderReferenceId; exports.amazonCheckout = amazon.checkout; exports.amazonSubscribe = amazon.subscribe; exports.amazonSubscribeCancel = amazon.subscribeCancel; diff --git a/website/src/routes/payments.js b/website/src/routes/payments.js index 210240314e..3583f96adc 100644 --- a/website/src/routes/payments.js +++ b/website/src/routes/payments.js @@ -18,6 +18,7 @@ router.post("/stripe/subscribe/edit", auth.auth, i18n.getUserLanguage, payments. router.get("/stripe/subscribe/cancel", auth.authWithUrl, i18n.getUserLanguage, payments.stripeSubscribeCancel); router.post('/amazon/verifyAccessToken', auth.auth, i18n.getUserLanguage, payments.amazonVerifyAccessToken); +router.post('/amazon/createOrderReferenceId', auth.auth, i18n.getUserLanguage, payments.amazonCreateOrderReferenceId); router.post('/amazon/checkout', auth.auth, i18n.getUserLanguage, payments.amazonCheckout); router.post('/amazon/subscribe', auth.auth, i18n.getUserLanguage, payments.amazonSubscribe); router.get('/amazon/subscribe/cancel', auth.authWithUrl, i18n.getUserLanguage, payments.amazonSubscribeCancel); diff --git a/website/views/shared/modals/buy-gems.jade b/website/views/shared/modals/buy-gems.jade index 33885a451c..aaa88d89c9 100644 --- a/website/views/shared/modals/buy-gems.jade +++ b/website/views/shared/modals/buy-gems.jade @@ -22,7 +22,7 @@ script(id='modals/buyGems.html', type='text/ng-template') small.muted=env.t('paymentMethods') .btn.btn-primary(ng-click='Payments.showStripe({})')=env.t('card') a.btn.btn-warning(href='/paypal/checkout?_id={{user._id}}&apiToken={{user.apiToken}}') PayPal - //.btn.btn-default(ng-click="Payments.amazonPayments.init({type: 'donation'})") Amazon Payments + .btn.btn-default(ng-click="Payments.amazonPayments.init({type: 'donation'})") Amazon Payments div(ng-include="'partials/options.settings.subscription.html'")