Skip to content

Instantly share code, notes, and snippets.

@jkresner
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkresner/8a581e50dcac58edbd10 to your computer and use it in GitHub Desktop.
Save jkresner/8a581e50dcac58edbd10 to your computer and use it in GitHub Desktop.
Node.js Express Login with PayPal Integration (ES6)
<h1>Paypal Login</h1>
<a href="{{paypalLoginUrl}}"><img src="https://www.paypalobjects.com/webstatic/en_US/developer/docs/lipp/loginwithpaypalbutton.png" /></a>
var app = require('express')();
var pp = require('paypalWrapper');
app.get('/', (req,res,next) => {
res.render('index',{paypalLoginUrl:pp.herlpers.getLoginUrl()});
});
app.get('/auth/paypal/callback', pp.handlers.handleAuthCallback);
var paypal = require('paypal-rest-sdk')
paypal.configure({
'mode': config.auth.paypal.mode,
'client_id': config.auth.paypal.clientID,
'client_secret': config.auth.paypal.clientSecret,
'openid_client_id': config.auth.paypal.clientID,
'openid_client_secret': config.auth.paypal.clientSecret,
'openid_redirect_uri': `${config.auth.oAuth.callbackHost}/auth/paypal/callback`
});
module.exports = {
helpers: {
getLoginUrl() {
//-- you'll have to configure express sessions scope might look like 'openid profile email'
return paypal.openIdConnect.authorizeUrl({'scope': config.auth.paypal.scope});
}
},
handlers: {
handleAuthCallback(req,res,next) {
var authCode = req.query.code;
var openIdConnect = paypal.openIdConnect;
openIdConnect.tokeninfo.create(authCode, function (e, tokeninfo) {
if (e) return next(e);
paypal.openIdConnect.userinfo.get(tokeninfo.access_token, function (ee, userinfo) {
if (e) return next(e);
//-- Got it, now do what you need to
console.log(tokeninfo);
console.log(userinfo);
next()
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment