Last active
July 16, 2019 05:07
-
-
Save dsternlicht/fdef0c57f2f2561f2c6c477f81fa348e to your computer and use it in GitHub Desktop.
Creating a PayPal IPN Endpoint With NodeJS - paypal.service.js - Validating IPN messages
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
import request from 'request'; | |
import querystring from 'querystring'; | |
import Promise from 'bluebird'; | |
class PayPalService { | |
static validate(body = {}) { | |
return new Promise((resolve, reject) => { | |
// Prepend 'cmd=_notify-validate' flag to the post string | |
let postreq = 'cmd=_notify-validate'; | |
// Iterate the original request payload object | |
// and prepend its keys and values to the post string | |
Object.keys(body).map((key) => { | |
postreq = `${postreq}&${key}=${body[key]}`; | |
return key; | |
}); | |
const options = { | |
url: 'https://ipnpb.paypal.com/cgi-bin/webscr', | |
method: 'POST', | |
headers: { | |
'Content-Length': postreq.length, | |
}, | |
encoding: 'utf-8', | |
body: postreq | |
}; | |
// Make a post request to PayPal | |
request(options, (error, response, resBody) => { | |
if (error || response.statusCode !== 200) { | |
reject(new Error(error)); | |
return; | |
} | |
// Validate the response from PayPal and resolve / reject the promise. | |
if (resBody.substring(0, 8) === 'VERIFIED') { | |
resolve(true); | |
} else if (resBody.substring(0, 7) === 'INVALID') { | |
reject(new Error('IPN Message is invalid.')); | |
} else { | |
reject(new Error('Unexpected response body.')); | |
} | |
}); | |
}); | |
} | |
} | |
export default PayPalService; |
It's working, if you're using "test" or "sandbox" paypal account to check, replace https://ipnpb.paypal.com/cgi-bin/webscr by https://ipnpb.sandbox.paypal.com/cgi-bin/webscr and done ;)
@lavimalik That's right. You may follow the complete tutorial on the following blog post:
https://medium.com/@danielsternlicht/handling-paypal-ipn-messages-with-nodejs-5ccd97870c4
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is it still working?