Skip to content

Instantly share code, notes, and snippets.

@dsternlicht
Last active July 16, 2019 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsternlicht/fdef0c57f2f2561f2c6c477f81fa348e to your computer and use it in GitHub Desktop.
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
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;
@checho221
Copy link

is it still working?

@tokyo786
Copy link

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 ;)

@dsternlicht
Copy link
Author

@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