Skip to content

Instantly share code, notes, and snippets.

@thlinux1107
Last active April 20, 2021 14:17
Show Gist options
  • Save thlinux1107/3472ee1f009ac063abf152cfbae7bfd3 to your computer and use it in GitHub Desktop.
Save thlinux1107/3472ee1f009ac063abf152cfbae7bfd3 to your computer and use it in GitHub Desktop.
Paya Gateway - Node.js request
let crypto = require('crypto');
var https = require('follow-redirects').https;
var fs = require('fs');
var merchantId = '173859436515';
var merchantKey = 'P1J2V8P2Q3D8';
var clientId = 'W8yvKQ5XbvAn7dUDJeAnaWCEwA4yXEgd';
var clientSecret = 'iLzODV5AUsCGWGkr';
var host = 'https://api-cert.sagepayments.com';
var endpoint = '/bankcard/v1/charges';
var action = '?type=Authorization';
var url = host + endpoint + action;
var timestamp = Math.floor(new Date().getTime()/1000.0);
var nonce = timestamp;
console.log('timestamp: ' + timestamp);
console.log('');
var verb = 'POST';
var postData = JSON.stringify({
"ecommerce": {
"amounts": {
"total": 1
},
"cardData": {
"number": "4111111111111111",
"expiration": "1230"
},
"orderNumber": "SDK" + timestamp
}
});
var authString = verb + url + postData + merchantId + nonce + timestamp;
console.log('authString: ' + authString);
console.log('');
function computeHMAC(stringToBeHashed, key) {
var hmac = crypto.createHmac("sha512", key);
var hash = hmac.update(new Buffer.from(stringToBeHashed, 'utf-8')).digest("base64");
return hash;
}
var authKey = computeHMAC(authString, clientSecret);
console.log('AuthKey: ' + authKey);
console.log('');
var options = {
'method': verb,
'hostname': 'api-cert.sagepayments.com',
'path': endpoint + action,
'headers': {
'clientId': clientId,
'merchantId': merchantId,
'merchantKey': merchantKey,
'nonce': nonce,
'timestamp': timestamp,
'Authorization': authKey,
'Content-Type': 'application/json'
},
'maxRedirects': 20
};
console.log('Options: ' + JSON.stringify(options));
console.log('');
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
console.log(body.toString());
console.log('');
});
res.on("error", function (error) {
console.error(error);
console.log('');
});
});
req.write(postData);
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment