Skip to content

Instantly share code, notes, and snippets.

@danielflippance
Created August 12, 2018 07:47
Show Gist options
  • Save danielflippance/f56d48a85181e6e92316d2886fbd5351 to your computer and use it in GitHub Desktop.
Save danielflippance/f56d48a85181e6e92316d2886fbd5351 to your computer and use it in GitHub Desktop.
'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();
}
}
@danielflippance
Copy link
Author

The output of this function is:

12:46:11 AM web.1   |  + PayPalIPNGateway.verify
12:46:11 AM web.1   |  ++ ipnTransactionMessage: {
12:46:11 AM web.1   |      "payment_type": "instant",
12:46:11 AM web.1   |      "payment_date": "Sun Aug 12 2018 00:32:10 GMT-0700 (Pacific Daylight Time)",
12:46:11 AM web.1   |      "payment_status": "Completed",
12:46:11 AM web.1   |      "address_status": "confirmed",
12:46:11 AM web.1   |      "payer_status": "verified",
12:46:11 AM web.1   |      "first_name": "John",
12:46:11 AM web.1   |      "last_name": "Smith",
12:46:11 AM web.1   |      "payer_email": "[email protected]",
12:46:11 AM web.1   |      "payer_id": "TESTBUYERID01",
12:46:11 AM web.1   |      "address_name": "John Smith",
12:46:11 AM web.1   |      "address_country": "United States",
12:46:11 AM web.1   |      "address_country_code": "US",
12:46:11 AM web.1   |      "address_zip": "95131",
12:46:11 AM web.1   |      "address_state": "CA",
12:46:11 AM web.1   |      "address_city": "San Jose",
12:46:11 AM web.1   |      "address_street": "123 any street",
12:46:11 AM web.1   |      "business": "[email protected]",
12:46:11 AM web.1   |      "receiver_email": "[email protected]",
12:46:11 AM web.1   |      "receiver_id": "[email protected]",
12:46:11 AM web.1   |      "residence_country": "US",
12:46:11 AM web.1   |      "item_name1": "something",
12:46:11 AM web.1   |      "item_number1": "AK-1234",
12:46:11 AM web.1   |      "tax": "2.02",
12:46:11 AM web.1   |      "mc_currency": "USD",
12:46:11 AM web.1   |      "mc_fee": "0.44",
12:46:11 AM web.1   |      "mc_gross": "12.34",
12:46:11 AM web.1   |      "mc_gross_1": "12.34",
12:46:11 AM web.1   |      "mc_handling": "2.06",
12:46:11 AM web.1   |      "mc_handling1": "1.67",
12:46:11 AM web.1   |      "mc_shipping": "3.02",
12:46:11 AM web.1   |      "mc_shipping1": "1.02",
12:46:11 AM web.1   |      "txn_type": "cart",
12:46:11 AM web.1   |      "txn_id": "770956045",
12:46:11 AM web.1   |      "notify_version": "2.1",
12:46:11 AM web.1   |      "custom": "xyz123",
12:46:11 AM web.1   |      "invoice": "abc1234",
12:46:11 AM web.1   |      "test_ipn": "1",
12:46:11 AM web.1   |      "verify_sign": "undefined"
12:46:11 AM web.1   |  }
12:46:11 AM web.1   |  ++ mode: test
12:46:11 AM web.1   |  ++ host: ipnpb.sandbox.paypal.com
12:46:11 AM web.1   |  ++ verificationBody: cmd=_notify-validate&payment_type=instant&payment_date=Sun%20Aug%2012%202018%2000%3A32%3A10%20GMT-0700%20(Pacific%20Daylight%20Time)&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&busine
12:46:11 AM web.1   |  >  ss=seller%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=seller%40paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=770956045&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=undefined
12:46:11 AM web.1   |  ++ options: {
12:46:11 AM web.1   |      "method": "POST",
12:46:11 AM web.1   |      "port": 443,
12:46:11 AM web.1   |      "uri": "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr",
12:46:11 AM web.1   |      "body": "cmd=_notify-validate&payment_type=instant&payment_date=Sun%20Aug%2012%202018%2000%3A32%3A10%20GMT-0700%20(Pacific%20Daylight%20Time)&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer%40paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=selle
12:46:11 AM web.1   |  >  r%40paypalsandbox.com&receiver_email=seller%40paypalsandbox.com&receiver_id=seller%40paypalsandbox.com&residence_country=US&item_name1=something&item_number1=AK-1234&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&mc_handling=2.06&mc_handling1=1.67&mc_shipping=3.02&mc_shipping1=1.02&txn_type=cart&txn_id=770956045&notify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=undefined"
12:46:11 AM web.1   |  }
12:46:11 AM web.1   |  ++ M17 paypal err: Error: unable to verify the first certificate
12:46:11 AM web.1   |  ++ err: {
12:46:11 AM web.1   |      "code": "UNABLE_TO_VERIFY_LEAF_SIGNATURE"
12:46:11 AM web.1   |  }
12:46:11 AM web.1   |  ++ M17 errrr: paypal error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment