PayPal api example refactoring

This commit is contained in:
Sahat Yalkabov
2014-03-16 14:07:15 -04:00
parent 0e30f2f7b3
commit 65ae9b18ad
2 changed files with 32 additions and 34 deletions

View File

@ -267,28 +267,30 @@ exports.getTwitter = function(req, res, next) {
exports.getPayPal = function(req, res, next) { exports.getPayPal = function(req, res, next) {
paypal.configure(secrets.paypal); paypal.configure(secrets.paypal);
var payment_details = {
'intent': 'sale', var paymentDetails = {
'payer': { intent: 'sale',
'payment_method': 'paypal' payer: {
payment_method: 'paypal'
}, },
'redirect_urls': { redirect_urls: {
'return_url': secrets.paypal.returnUrl, return_url: secrets.paypal.returnUrl,
'cancel_url': secrets.paypal.cancelUrl cancel_url: secrets.paypal.cancelUrl
}, },
'transactions': [ transactions: [
{ {
'description': 'Node.js Boilerplate', description: 'Node.js Boilerplate',
'amount': { amount: {
'currency': 'USD', currency: 'USD',
'total': '2.99' total: '2.99'
} }
} }
] ]
}; };
paypal.payment.create(payment_details, function(err, payment) {
paypal.payment.create(paymentDetails, function(err, payment) {
if (err) return next(err); if (err) return next(err);
req.session.payment_id = payment.id; req.session.paymentId = payment.id;
var links = payment.links; var links = payment.links;
for (var i = 0; i < links.length; i++) { for (var i = 0; i < links.length; i++) {
if (links[i].rel === 'approval_url') { if (links[i].rel === 'approval_url') {
@ -306,10 +308,10 @@ exports.getPayPal = function(req, res, next) {
*/ */
exports.getPayPalSuccess = function(req, res, next) { exports.getPayPalSuccess = function(req, res, next) {
var payment_id = req.session.payment_id; var paymentId = req.session.paymentId;
var payment_details = { 'payer_id': req.query.PayerID }; var paymentDetails = { 'payer_id': req.query.PayerID };
paypal.payment.execute(payment_id, payment_details, function(error, payment) { paypal.payment.execute(paymentId, paymentDetails, function(err, payment) {
if (error) { if (err) {
res.render('api/paypal', { res.render('api/paypal', {
result: true, result: true,
success: false success: false

View File

@ -3,7 +3,7 @@ extends ../layout
block content block content
.page-header .page-header
h2 h2
i.fa.fa-dollar(style='color: #1B4A7D') i.fa.fa-dollar
| PayPal API | PayPal API
.btn-group.btn-group-justified .btn-group.btn-group-justified
@ -17,19 +17,15 @@ block content
i.fa.fa-gear i.fa.fa-gear
| API Playground | API Playground
h3
img(src='/img/paypal.png', width=50, height=50)
span Sample Payment
if result if result
if canceled if canceled
h3 Payment got canceled! h3 Payment got canceled!
if success if success
h3 Payment got executed successfully! h3 Payment got executed successfully!
a(href='/api/paypal') a(href='/api/paypal')
button.btn.btn-primary New payment button.btn.btn-primary New payment
else else
div p.lead Redirects to PayPal and allows authorizing the sample payment.
p Redirects to PayPal and allows authorizing the sample payment. a(href=approval_url)
a(href=approval_url) button.btn.btn-primary Authorize payment
button.btn.btn-primary Authorize payment