diff --git a/README.md b/README.md index 094eccebca..defb19159f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ![Alt](https://lh4.googleusercontent.com/-PVw-ZUM9vV8/UuWeH51os0I/AAAAAAAAD6M/0Ikg7viJftQ/w1286-h566-no/hackathon-starter-logo.jpg) Hackathon Starter [![Dependency Status](https://david-dm.org/sahat/hackathon-starter.png?theme=shields.io)](https://david-dm.org/sahat/hackathon-starter) ================= -A kickstarter for **Node.js** web applications. +A kickstarter for **Node.js** web applications. **Live Demo**: http://hackathonstarter.herokuapp.com @@ -43,7 +43,7 @@ Features - Change Password - Link multipleOAuth 2.0 strategies to one account - Delete Account -- **API Examples**: Facebook, Foursquare, Last.fm, Tumblr, Twitter, and more. +- **API Examples**: Facebook, Foursquare, Last.fm, Tumblr, Twitter, PayPal, and more. Prerequisites ------------- @@ -112,6 +112,15 @@ Obtaining API Keys - Select **Website** - Enter `http://localhost:3000` for *Site URL* + +- Visit [PayPal Developer](https://developer.paypal.com/) +- Log in using your existing PayPal account +- Click **Applications > Create App** in the navigation bar +- Enter *Application Name*, then click **Create app** +- Copy and paste *Client ID* and *Secret* keys into `config/secrets.js` +- *App ID* is **client_id**, *App Secret* is **client_secret** +- Change **host** to api.paypal.com if you want to test against production and use the live credentials + *TODO: Add Twitter and GitHub instructions.* Project Structure diff --git a/app.js b/app.js index 1e10792ae7..4a44222054 100755 --- a/app.js +++ b/app.js @@ -97,6 +97,9 @@ app.get('/api/lastfm', apiController.getLastfm); app.get('/api/nyt', apiController.getNewYorkTimes); app.get('/api/twitter', passportConf.isAuthenticated, apiController.getTwitter); app.get('/api/aviary', apiController.getAviary); +app.get('/api/paypal', apiController.getPayPal); +app.get('/api/paypal/success', apiController.getPayPalSuccess); +app.get('/api/paypal/cancel', apiController.getPayPalCancel); app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' })); app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' })); app.get('/auth/github', passport.authenticate('github')); diff --git a/controllers/api.js b/controllers/api.js index f59aa9a2e6..71999dc18f 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -11,6 +11,7 @@ var tumblr = require('tumblr.js'); var foursquare = require('node-foursquare')({ secrets: secrets.foursquare }); var Github = require('github-api'); var Twit = require('twit'); +var paypal = require('paypal-rest-sdk'); /** * GET /api @@ -254,3 +255,77 @@ exports.getTwitter = function(req, res, next) { }); }); }; + +/** + * GET /api/paypal + * PayPal SDK example + */ +exports.getPayPal = function(req, res, next) { + paypal.configure(secrets.paypal); + var payment_details = { + 'intent': 'sale', + 'payer': { + 'payment_method': 'paypal' + }, + 'redirect_urls': { + 'return_url': secrets.paypal.returnUrl, + 'cancel_url': secrets.paypal.cancelUrl + }, + 'transactions': [{ + 'description': 'Node.js Boilerplate', + 'amount': { + 'currency': 'USD', + 'total': '2.99' + } + }] + }; + paypal.payment.create(payment_details, function (error, payment) { + if(error){ + console.log(error); + } else { + req.session.payment_id = payment.id; + var links = payment.links; + for (var i = 0; i < links.length; i++) { + if (links[i].rel === 'approval_url') { + res.render('api/paypal', { + approval_url: links[i].href + }); + } + } + } + }); +}; + +/** + * GET /api/paypal/success + * PayPal SDK example + */ +exports.getPayPalSuccess = function(req, res, next) { + var payment_id = req.session.payment_id; + var payment_details = { 'payer_id': req.query.PayerID }; + paypal.payment.execute(payment_id, payment_details, function(error, payment){ + if(error){ + res.render('api/paypal', { + result: true, + success: false + }); + } else { + res.render('api/paypal', { + result: true, + success: true + }); + } + }); +}; + +/** + * GET /api/paypal/cancel + * PayPal SDK example + */ +exports.getPayPalCancel = function(req, res, next) { + req.session.payment_id = null; + res.render('api/paypal', { + result: true, + canceled: true + }); +}; \ No newline at end of file diff --git a/package.json b/package.json index 13a2e656b2..38227e9566 100755 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "sendgrid": "~0.4.6", "tumblr.js": "~0.0.4", "twit": "~1.1.12", - "underscore": "~1.5.2" + "underscore": "~1.5.2", + "paypal-rest-sdk": "~0.6.4" } } diff --git a/public/img/paypal.png b/public/img/paypal.png new file mode 100644 index 0000000000..d69bbb60e0 Binary files /dev/null and b/public/img/paypal.png differ diff --git a/views/api/index.jade b/views/api/index.jade index 8ce290fb90..09acc68c4f 100644 --- a/views/api/index.jade +++ b/views/api/index.jade @@ -27,3 +27,5 @@ block content small (Login required) li a(href='/api/scraping') Web Scraping + li + a(href='/api/paypal') PayPal diff --git a/views/api/paypal.jade b/views/api/paypal.jade new file mode 100644 index 0000000000..3316588d61 --- /dev/null +++ b/views/api/paypal.jade @@ -0,0 +1,35 @@ +extends ../layout + +block content + .page-header + h2 + i.fa.fa-dollar(style='color: #1B4A7D') + | PayPal API + + .btn-group.btn-group-justified + a.btn.btn-primary(href='https://developer.paypal.com/docs/integration/direct/make-your-first-call/', target='_blank') + i.fa.fa-check-square-o + | Quickstart + a.btn.btn-primary(href='https://developer.paypal.com/docs/api/', target='_blank') + i.fa.fa-code + | API Reference + a.btn.btn-primary(href='https://devtools-paypal.com/hateoas/index.html', target='_blank') + i.fa.fa-gear + | API Playground + + h3 + img(src='/img/paypal.png', width=50, height=50) + span Sample Payment + + if result + if canceled + h3 Payment got canceled! + if success + h3 Payment got executed successfully! + a(href='/api/paypal') + button.btn.btn-primary New payment + else + div + p Redirects to PayPal and allows authorizing the sample payment. + a(href=approval_url) + button.btn.btn-primary Authorize payment \ No newline at end of file