Skip to content

Instantly share code, notes, and snippets.

@chrisjensen
Last active October 17, 2019 01:24
Show Gist options
  • Save chrisjensen/c19cf0c46eeedec225d333c258e34aa6 to your computer and use it in GitHub Desktop.
Save chrisjensen/c19cf0c46eeedec225d333c258e34aa6 to your computer and use it in GitHub Desktop.
Rapyd API attempt
to_sign: get/v1/payment_methods/country?country=SG&currency=SGD<redacted>
{ timestamp: '1571275418',
salt: 1249378312,
'Content-Type': 'application/json',
signature: 'ODEzOTlmNGRkMDE4MWFkYjFlOGRiYThlYTA3YzUyMjBkZmM5ZjY4YWQzNmZlN2M1ZWI3MWUzZjRjZmFjMjM4YQ==',
access_key: 'D7DF0851A8CB803DC3D2',
'User-Agent': 'Request-Promise' }
(node:27267) UnhandledPromiseRejectionWarning: StatusCodeError: 401 - {"status":{"error_code":"UNAUTHENTICATED_API_CALL","status":"ERROR","message":"Invalid signature","response_code":"UNAUTHENTICATED_API_CALL","operation_id":"4f59bef8-9383-4015-8487-5278b19c81db"}}
at new StatusCodeError (/Users/chrisjensen/Sites/test/node_modules/request-promise-core/lib/errors.js:32:15)
const CryptoJS = require('crypto-js');
const request = require('request-promise-native');
const util = require('util');
const http_method = 'get'; // Lower case.
var url_path = process.argv[2]; // Portion after the base URL.
var salt = Math.abs(CryptoJS.lib.WordArray.random(12).words[0]); // Randomly generated for each request.
var timestamp = (Math.floor(new Date().getTime() / 1000) - 10).toString();
// Current Unix time.
var access_key = 'D7DF0851A8CB803DC3D2'; // The access key received from Rapyd.
var secret_key = '4d8910774d667b9a07aa9446824cb59d3834d65e14b5b7793daa196ac1504ccad9e71ca1742c13d9'; // Never transmit the secret key by itself.
var body = ''; // JSON body goes here.
const data = '';
if (JSON.stringify(data) !== '{}' && data !== '') {
body = JSON.stringify(JSON.parse(data));
}
var to_sign = http_method + url_path + salt + timestamp + access_key + secret_key + body;
console.log(to_sign);
var signature = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA256(to_sign, secret_key));
signature = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(signature));
var options = {
uri: 'https://sandboxapi.rapyd.net' + url_path,
qs: {
access_token: 'xxxxx xxxxx' // -> uri + '?access_token=xxxxx%20xxxxx'
},
headers: {
timestamp,
salt,
'Content-Type': 'application/json',
signature,
access_key,
'User-Agent': 'Request-Promise'
},
json: true // Automatically parses the JSON string in the response
};
if (data) options.body = data;
console.log(options.headers)
request(options)
.then(response =>
console.log(util.inspect(response, null, 0, true))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment