Created
January 15, 2017 12:51
-
-
Save mgcm/3c6e8806773e9f2af3c493741e9eb39a to your computer and use it in GitHub Desktop.
PayPal IPN / PDT Handler
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
var express = require('express'); | |
var request = require('request'); | |
var app = express(); | |
var ipn = require('paypal-ipn'); | |
var bodyParser = require('body-parser'); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})); | |
app.post('/paypal', function(req, res) { | |
var params = req.body; | |
res.sendStatus(200); | |
ipn.verify(params, { | |
'allow_sandbox': true | |
}, function callback(err, msg) { | |
if (err) { | |
console.log(err); | |
} else { | |
console.log(params); | |
console.log('Verify no error.'); | |
if (params.payment_status == 'Completed') { | |
console.log('Payment completed.'); | |
} | |
} | |
}); | |
}); | |
app.get('/paypal', function(req, res) { | |
res.sendStatus(200); | |
console.log('received GET (PDT)'); | |
console.log(req.query); | |
var tx = req.query.tx; | |
var token = "ZT9dZUWueDV9hxqfGr_A8cE7bXvrmONGk-mTFxIay8LirIBG5t0V1T0v2tC"; | |
request.post({ | |
url: 'https://www.sandbox.paypal.com/cgi-bin/webscr', | |
form: { | |
'cmd': '_notify-synch', | |
'tx': tx, | |
'at': token | |
} | |
}, function(err, httpResponse, body) { | |
console.log(body); | |
}); | |
}); | |
app.listen(9090, function() { | |
console.log('PayPal handler running...') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment