-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate PayPal REST SDK so we can take credit card payments
- Loading branch information
lmarkus
committed
Dec 5, 2013
1 parent
5aa586f
commit 9e46ec5
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :)'}); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |