Add send a payment form, route and controller for Venmo API

This commit is contained in:
Sahat Yalkabov
2014-02-10 23:55:32 -05:00
parent 86801331a3
commit 5275607bdb
4 changed files with 91 additions and 31 deletions

1
app.js
View File

@ -124,6 +124,7 @@ app.get('/api/facebook', passportConf.isAuthenticated, passportConf.isAuthorized
app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub); app.get('/api/github', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getGithub);
app.get('/api/twitter', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTwitter); app.get('/api/twitter', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getTwitter);
app.get('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getVenmo); app.get('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.getVenmo);
app.post('/api/venmo', passportConf.isAuthenticated, passportConf.isAuthorized, apiController.postVenmo);
/** /**
* OAuth routes for sign-in. * OAuth routes for sign-in.

View File

@ -1,6 +1,7 @@
var secrets = require('../config/secrets'); var secrets = require('../config/secrets');
var User = require('../models/User'); var User = require('../models/User');
var querystring = require('querystring'); var querystring = require('querystring');
var validator = require('validator');
var async = require('async'); var async = require('async');
var cheerio = require('cheerio'); var cheerio = require('cheerio');
var request = require('request'); var request = require('request');
@ -417,13 +418,55 @@ exports.postTwilio = function(req, res, next) {
exports.getVenmo = function(req, res, next) { exports.getVenmo = function(req, res, next) {
var token = _.findWhere(req.user.tokens, { kind: 'venmo' }); var token = _.findWhere(req.user.tokens, { kind: 'venmo' });
var query = querystring.stringify({ access_token: token.accessToken }); var query = querystring.stringify({ access_token: token.accessToken });
// Get profile information
request.get({ url: 'https://api.venmo.com/v1/me?' + query, json: true }, function(err, request, body) { request.get({ url: 'https://api.venmo.com/v1/me?' + query, json: true }, function(err, request, body) {
if (err) return next(err); if (err) return next(err);
res.render('api/venmo', { res.render('api/venmo', {
title: 'Venmo API', title: 'Venmo API',
profile: body.data profile: body.data
}); });
}); });
};
}; exports.postVenmo = function(req, res, next) {
req.assert('user', 'Phone, Email or Venmo User ID cannot be blank').notEmpty();
req.assert('note', 'Please enter a message to accompany the payment').notEmpty();
req.assert('amount', 'They amount you want to pay cannot be blank').notEmpty();
var errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
return res.redirect('/api/venmo');
}
var token = _.findWhere(req.user.tokens, { kind: 'venmo' });
var formData = {
access_token: token.accessToken,
note: req.body.note,
amount: req.body.amount
};
if (validator.isEmail(req.body.user)) {
formData.email = req.body.user;
} else if (validator.isLength(req.body.user, 7, 10)) {
formData.phone = req.body.user;
} else {
formData.user_id = req.body.user;
}
// Send money
request.post('https://api.venmo.com/v1/payments', { form: formData }, function(err, request, body) {
if (err) return next(err);
console.log(body);
req.flash('success', 'Venmo money transfer complete');
res.redirect('/api/venmo');
});
};
// remove thumb css
// add venmo instructions
// update auth optional instructions

View File

@ -33,6 +33,7 @@
"underscore": "~1.5.2", "underscore": "~1.5.2",
"paypal-rest-sdk": "~0.6.4", "paypal-rest-sdk": "~0.6.4",
"connect-mongo": "~0.4.0", "connect-mongo": "~0.4.0",
"twilio": "~1.5.0" "twilio": "~1.5.0",
"validator": "~3.2.1"
} }
} }

View File

@ -18,32 +18,47 @@ block content
| API Endspoints | API Endspoints
h3 Venmo Profile h3 Venmo Profile
//.col-sm-2 .row
// img(src='#{profile.user.profile_picture_url}') .col-sm-2
//.col-sm-8 img(src='#{profile.user.profile_picture_url}')
// .lead Balance: .col-sm-8
// strong $#{profile.balance} .row
// .lead Display Name: .col-sm-6
// strong #{profile.user.display_name} strong #{profile.user.display_name}
// .lead Email: div Balance:
// strong #{profile.user.email} strong $#{profile.balance}
// .lead Phone: div Friends:
// strong #{profile.user.phone} strong #{profile.user.friends_count}
// .col-sm-6
div Email:
strong #{profile.user.email}
div Phone:
strong #{profile.user.phone}
div ID:
strong #{profile.user.id}
p #{profile.user.about}
br
.row
.col-sm-5
h3 Make Payment
form(role='form', method='POST')
input(type='hidden', name='_csrf', value=token)
.form-group
label.control-label(for='user') Phone, Email or Venmo User ID
input.form-control(type='text', name='user', id='user', autofocus=true)
.form-group
label.control-label(for='note') Note
input.form-control(type='text', name='note', id='note')
.form-group
label.control-label(for='amount') Amount
input.form-control(type='text', name='amount', id='amount')
.form-group
button.btn.btn-primary(type='submit')
i.fa.fa-usd
| Send
br
h3 Recent Payments
form(role='form', method='POST')
legend Make a payment
input(type='hidden', name='_csrf', value=token)
.form-group
label.control-label(for='user') Email, Phone, or User ID
input.form-control(type='text', name='user', id='user', autofocus=true)
.form-group
label.control-label(for='note') Note
input.form-control(type='text', name='note', id='note')
.form-group
label.control-label(for='amount') Amount
input.form-control(type='text', name='amount', id='amount')
.form-group
button.btn.btn-default(type='submit')
i.fa.fa-usd
| Send