const graph = require('fbgraph');
const Twit = require('twit');
const paypal = require('paypal-rest-sdk');
/**
* GET /api/facebook
* Facebook API example.
*/
exports.getFacebook = (req, res, next) => {
const token = req.user.tokens.find(token => token.kind === 'facebook');
graph.setAccessToken(token.accessToken);
graph.get(`${req.user.facebook}?fields=id,name,email,first_name,last_name,gender,link,locale,timezone`, (err, results) => {
if (err) { return next(err); }
res.render('api/facebook', {
title: 'Facebook API',
profile: results
});
});
};
/**
* GET /api/twitter
* Twitter API example.
*/
exports.getTwitter = (req, res, next) => {
const token = req.user.tokens.find(token => token.kind === 'twitter');
const T = new Twit({
consumer_key: process.env.TWITTER_KEY,
consumer_secret: process.env.TWITTER_SECRET,
access_token: token.accessToken,
access_token_secret: token.tokenSecret
});
T.get('search/tweets', { q: 'nodejs since:2013-01-01', geocode: '40.71448,-74.00598,5mi', count: 10 }, (err, reply) => {
if (err) { return next(err); }
res.render('api/twitter', {
title: 'Twitter API',
tweets: reply.statuses
});
});
};
/**
* POST /api/twitter
* Post a tweet.
*/
exports.postTwitter = (req, res, next) => {
req.assert('tweet', 'Tweet cannot be empty').notEmpty();
const errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
return res.redirect('/api/twitter');
}
const token = req.user.tokens.find(token => token.kind === 'twitter');
const T = new Twit({
consumer_key: process.env.TWITTER_KEY,
consumer_secret: process.env.TWITTER_SECRET,
access_token: token.accessToken,
access_token_secret: token.tokenSecret
});
T.post('statuses/update', { status: req.body.tweet }, (err) => {
if (err) { return next(err); }
req.flash('success', { msg: 'Your tweet has been posted.' });
res.redirect('/api/twitter');
});
};
/**
* GET /api/paypal
* PayPal SDK example.
*/
exports.getPayPal = (req, res, next) => {
paypal.configure({
mode: 'sandbox',
client_id: process.env.PAYPAL_ID,
client_secret: process.env.PAYPAL_SECRET
});
const paymentDetails = {
intent: 'sale',
payer: {
payment_method: 'paypal'
},
redirect_urls: {
return_url: process.env.PAYPAL_RETURN_URL,
cancel_url: process.env.PAYPAL_CANCEL_URL
},
transactions: [{
description: 'Hackathon Starter',
amount: {
currency: 'USD',
total: '1.99'
}
}]
};
paypal.payment.create(paymentDetails, (err, payment) => {
if (err) { return next(err); }
req.session.paymentId = payment.id;
const links = payment.links;
for (let i = 0; i < links.length; i++) {
if (links[i].rel === 'approval_url') {
res.render('api/paypal', {
approvalUrl: links[i].href
});
}
}
});
};
/**
* GET /api/paypal/success
* PayPal SDK example.
*/
exports.getPayPalSuccess = (req, res) => {
const paymentId = req.session.paymentId;
const paymentDetails = { payer_id: req.query.PayerID };
paypal.payment.execute(paymentId, paymentDetails, (err) => {
res.render('api/paypal', {
result: true,
success: !err
});
});
};
/**
* GET /api/paypal/cancel
* PayPal SDK example.
*/
exports.getPayPalCancel = (req, res) => {
req.session.paymentId = null;
res.render('api/paypal', {
result: true,
canceled: true
});
};
/**
* GET /api/upload
* File Upload API example.
*/
exports.getFileUpload = (req, res) => {
res.render('api/upload', {
title: 'File Upload'
});
};
exports.postFileUpload = (req, res) => {
req.flash('success', { msg: 'File was uploaded successfully.' });
res.redirect('/api/upload');
};
exports.getGoogleMaps = (req, res) => {
res.render('api/google-maps', {
title: 'Google Maps API'
});
};