Skip to content

Commit

Permalink
Integrate PayPal REST SDK so we can take credit card payments
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarkus committed Dec 5, 2013
1 parent 5aa586f commit 9e46ec5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/app.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"paypalConfig": {
"host": "api.sandbox.paypal.com",
"port": "",
"client_id": "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM",
"client_secret": "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM"
},

"databaseConfig": {
"host": "localhost",
"database": "test"
Expand Down
70 changes: 70 additions & 0 deletions controllers/pay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';
var paypal = require('paypal-rest-sdk');

module.exports = function (server) {

/**
* Send information to PayPal
*/
server.post('/pay', function (req, res) {

//Read the incoming product data
var cc = req.param('cc'),
firstName = req.param('firstName'),
lastName = req.param('lastName'),
expMonth = req.param('expMonth'),
expYear = req.param('expYear'),
cvv = req.param('cvv');

//Ready the payment information to pass to the PayPal library
var payment = {
'intent': 'sale',
'payer': {
'payment_method': 'credit_card',
'funding_instruments': []
},
'transactions': []
};

// Identify credit card type. Patent pending. Credit cards starting with 3 = amex, 4 = visa, 5 = mc , 6 = discover
var ccType = (['amex','visa','mastercard','discover'])[parseInt(cc.slice(0,1),10)-3];

//Set the credit card
payment.payer.funding_instruments[0] =
{
'credit_card': {
'number': cc,
'type': ccType,
'expire_month': expMonth,
'expire_year': expYear,
'cvv2': cvv,
'first_name': firstName,
'last_name': lastName
}
};

//Set the total to charge the customer
payment.transactions[0] = {
amount: {
total: req.session.total,
currency: 'USD'
},
description: 'Your Kraken Store Purchase'
};

//Execute the payment.
paypal.payment.create(payment, {}, function (err, resp) {
if (err) {
console.log(err);
res.render('result',{result:'Error :('});
return;
}

if (resp) {
delete req.session.cart;
delete req.session.displayCart;
res.render('result',{result:'Success :)'});
}
});
});
};
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ var kraken = require('kraken-js'),
db = require('./lib/database'),
language = require('./lib/language'),
express = require('express'),
paypal = require('paypal-rest-sdk'),
app = {};


app.configure = function configure(nconf, next) {
// Fired when an app configures itself
//Configure the database
db.config(nconf.get('databaseConfig'));

//Configure the PayPal SDK
paypal.configure(nconf.get('paypalConfig'));
next(null);
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"dustjs-linkedin": "~2.0.3",
"dustjs-helpers": "~1.1.1",
"makara": "~0.3.0",
"mongoose": "~3.8.1"
"mongoose": "~3.8.1",
"paypal-rest-sdk": "~0.6.4"
},
"devDependencies": {
"mocha": "~1.10.0",
Expand Down
8 changes: 8 additions & 0 deletions public/templates/result.dust
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{>"layouts/master" /}

{<body}
<main role="main">
Result: {.result}
<h3><a href="/">Keep on buying!</a></h3>
</main>
{/body}

0 comments on commit 9e46ec5

Please sign in to comment.