Merge pull request #9 from SeraphimSerapis/paypal-rest-sdk

PayPal REST SDK sample
This commit is contained in:
Sahat Yalkabov
2014-01-27 18:16:15 -08:00
7 changed files with 128 additions and 3 deletions

View File

@ -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*
<img src="https://www.paypalobjects.com/webstatic/developer/logo_paypal-developer_beta.png" width="200">
- 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

3
app.js
View File

@ -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'));

View File

@ -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
});
};

View File

@ -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"
}
}

BIN
public/img/paypal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -27,3 +27,5 @@ block content
small (Login required)
li
a(href='/api/scraping') Web Scraping
li
a(href='/api/paypal') PayPal

35
views/api/paypal.jade Normal file
View File

@ -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