Adds PayPal SDK sample to the API controller
This commit is contained in:
		@@ -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
 | 
			
		||||
  });
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user