Created
August 12, 2018 07:47
-
-
Save danielflippance/f56d48a85181e6e92316d2886fbd5351 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
const SANDBOX_VERIFY_HOSTNAME = 'ipnpb.sandbox.paypal.com'; | |
var qs = require('querystring'); | |
var https = require('https'); | |
//======================================================== | |
// PayPalIPNGateway | |
// Gateway for the PayPal IPN messages | |
//======================================================== | |
module.exports = class PayPalIPNGateway { | |
constructor() { | |
} | |
//======================================================== | |
// verify | |
// https://github.com/paypal/ipn-code-samples/blob/master/javascript/googlecloudfunctions.js | |
//======================================================== | |
verify(ipnTransactionMessage, mode, callback) { | |
console.log('+ PayPalIPNGateway.verify'); | |
console.log('++ ipnTransactionMessage: ' + JSON.stringify(ipnTransactionMessage, null, 4)); | |
console.log('++ mode: %s', mode); | |
const host = SANDBOX_VERIFY_HOSTNAME; | |
console.log('++ host: %s', host); | |
var self = this; | |
//Get the transaction message | |
// JSON object of the IPN message consisting of transaction details. | |
// Convert JSON ipn data to a query string since Google Cloud Function does not expose raw request data. | |
let formUrlEncodedBody = qs.stringify(ipnTransactionMessage); | |
// Build the body of the verification post message by prefixing 'cmd=_notify-validate'. | |
let verificationBody = `cmd=_notify-validate&${formUrlEncodedBody}`; | |
console.log(`++ verificationBody: ${verificationBody}`); | |
let options = { | |
method: 'POST', | |
port: 443, | |
uri: 'https://' + host + '/cgi-bin/webscr', | |
body: verificationBody, | |
}; | |
console.log('++ options: ' + JSON.stringify(options, null, 4)); | |
// POST verification IPN data to paypal to validate. | |
var request = https.request(options, function(response) { | |
console.log('++ response: ' + JSON.stringify(response, null, 4)); | |
console.log('++ response.headers: ' + JSON.stringify(response.headers, null, 4)); | |
console.log('++ response.statusMessage: ' + JSON.stringify(response.statusMessage, null, 4)); | |
console.log('++ response.statusCode: ' + JSON.stringify(response.statusCode, null, 4)); | |
var responseBody = ''; | |
console.log(options.host + ':' + response.statusCode); | |
response.setEncoding('utf8'); | |
response.on('data', function (chunk) { | |
console.log('++ chunk'); | |
responseBody += chunk; | |
}).on('end', function() { | |
console.log('++ end'); | |
console.log('++ response.statusCode: %s', response.statusCode); | |
console.log('++ responseBody: %s', responseBody); | |
}); | |
}); | |
request.on('error', function(err) { | |
console.log('++ M17 paypal err: ' + err); | |
console.log('++ err: ' + JSON.stringify(err, null, 4)); | |
callback('paypal error'); | |
}); | |
request.end(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output of this function is: