Code snippets
Example code snippets fo signing and sending requests
PHP
JS
1
class Finrax
2
{
3
private $apiKey;
4
private $apiSecret;
5
private $baseUrl = 'https://sandbox-payments.finrax.com';
6
7
8
public function __construct(string $apiKey, string $apiSecret)
9
{
10
$this->apiKey = $apiKey;
11
$this->apiSecret = $apiSecret;
12
}
13
14
public function makeRequest($method, $endpoint, array $body = [], array $query = [])
15
{
16
$method = strtoupper($method);
17
$qs = http_build_query($query, '', '&');
18
$path = ($qs == '') ? $endpoint : $endpoint . '?' . $qs;
19
$ch = curl_init();
20
$jsonBody = '';
21
if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH') {
22
$jsonBody = json_encode($body, JSON_UNESCAPED_SLASHES);
23
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonBody);
24
}
25
curl_setopt_array($ch, [
26
CURLOPT_URL => $this->baseUrl . $path,
27
CURLOPT_CUSTOMREQUEST => $method,
28
CURLOPT_RETURNTRANSFER => true,
29
CURLOPT_HTTPHEADER => [
30
'Content-Type: application/json',
31
'Authorization: ' . $this->buildAuthorizationHeaderValue($path, $jsonBody)
32
]
33
]);
34
$response = curl_exec($ch);
35
curl_close($ch);
36
return json_decode($response, true);
37
}
38
39
private function buildAuthorizationHeaderValue($path, $jsonBody = '')
40
{
41
$timestamp = intval(microtime(true) * 1000);
42
$signaturePayload = $path . $timestamp . $jsonBody;
43
$signature = hash_hmac('sha256', $signaturePayload, $this->apiSecret);
44
return "FRX-API API-Key={$this->apiKey}," .
45
"Signature={$signature}," .
46
"Timestamp={$timestamp}";
47
}
48
}
Copied!
1
'use strict';
2
const CryptoJS = require('crypto-js');
3
const fetch = require('node-fetch');
4
const Headers = fetch.Headers;
5
const API_KEY = process.env.API_KEY;
6
const API_SECRET = process.env.API_SECRET;
7
const BASE_URL = process.env.BASE_URL || 'https://sandbox-payments.finrax.com';
8
9
function buildAuthorizationHeaderValue(path, jsonBody = '') {
10
let timestamp = Date.now();
11
let signaturePayload = path + timestamp + jsonBody;
12
let signature = CryptoJS.HmacSHA256(signaturePayload, API_SECRET);
13
14
return `FRX-API API-Key=${API_KEY},` +
15
`Signature=${signature},` +
16
`Timestamp=${timestamp}`;
17
}
18
19
function stringifyBody(jsonBody) {
20
if (typeof jsonBody === 'string') {
21
if (jsonBody === '') {
22
throw "Body is requried for POST, PATCH, PUT requests.";
23
}
24
jsonBody = JSON.stringify(JSON.parse(jsonBody));
25
} else {
26
jsonBody = JSON.stringify(jsonBody);
27
}
28
29
return jsonBody;
30
}
31
32
function buildRequestOptions(method, path, jsonBody) {
33
let requestOptions = {};
34
let body = '';
35
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
36
body = stringifyBody(jsonBody);
37
requestOptions.body = body;
38
}
39
40
let authorizatonHeader = buildAuthorizationHeaderValue(path, body);
41
let headers = new Headers();
42
headers.append('Authorization', authorizatonHeader);
43
headers.append('Content-type', 'application/json');
44
requestOptions.method = method;
45
requestOptions.headers = headers;
46
47
return requestOptions;
48
}
49
50
function makeRequest(method, path, jsonBody = '') {
51
let url = BASE_URL + path;
52
let requestOptions = buildRequestOptions(method, path, jsonBody);
53
54
fetch(url, requestOptions)
55
.then(response => response.text())
56
.then(result => console.log(result))
57
.catch(error => console.log('error', error));
58
}
Copied!
Last modified 1yr ago
Copy link