tranzila 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +63 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,12 +3,16 @@ const https = require("https");
|
|
|
3
3
|
|
|
4
4
|
class Tranzila {
|
|
5
5
|
|
|
6
|
+
#terminal;
|
|
6
7
|
#publicKey;
|
|
7
8
|
#privateKey;
|
|
9
|
+
#TranzilaPW;
|
|
8
10
|
|
|
9
|
-
constructor(publicKey, privateKey) {
|
|
11
|
+
constructor(terminal, publicKey, privateKey, TranzilaPW) {
|
|
10
12
|
this.#publicKey = publicKey;
|
|
11
13
|
this.#privateKey = privateKey;
|
|
14
|
+
this.#terminal = terminal;
|
|
15
|
+
this.#TranzilaPW = TranzilaPW;
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
async makeRequest(method, url, data) {
|
|
@@ -38,7 +42,7 @@ class Tranzila {
|
|
|
38
42
|
const json = JSON.parse(responseStr);
|
|
39
43
|
resolve(json);
|
|
40
44
|
} catch (e) {
|
|
41
|
-
console.error("Unable to
|
|
45
|
+
console.error("Unable to parse response", responseStr, e);
|
|
42
46
|
reject(e);
|
|
43
47
|
}
|
|
44
48
|
});
|
|
@@ -56,6 +60,62 @@ class Tranzila {
|
|
|
56
60
|
});
|
|
57
61
|
}
|
|
58
62
|
|
|
63
|
+
async handshakeRequest(data) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
try {
|
|
66
|
+
let headers = this.#generateHeaders();
|
|
67
|
+
|
|
68
|
+
const url = new URL('https://api.tranzila.com/v1/handshake/create');
|
|
69
|
+
url.searchParams.set('supplier', this.#terminal);
|
|
70
|
+
url.searchParams.set('TranzilaPW', this.#TranzilaPW);
|
|
71
|
+
for (const key of Object.keys(data))
|
|
72
|
+
url.searchParams.set(key, data[key]);
|
|
73
|
+
|
|
74
|
+
let request = https.get({
|
|
75
|
+
hostname: url.hostname,
|
|
76
|
+
path: url.pathname + url.search,
|
|
77
|
+
headers
|
|
78
|
+
}, (res) => {
|
|
79
|
+
let responseBuffs = [];
|
|
80
|
+
let responseStr = '';
|
|
81
|
+
|
|
82
|
+
res.on('data', (chunk) => {
|
|
83
|
+
if (Buffer.isBuffer(chunk))
|
|
84
|
+
responseBuffs.push(chunk);
|
|
85
|
+
else
|
|
86
|
+
responseStr = responseStr + chunk;
|
|
87
|
+
}).on('end', () => {
|
|
88
|
+
try {
|
|
89
|
+
responseStr = responseBuffs.length > 0 ?
|
|
90
|
+
Buffer.concat(responseBuffs).toString('utf8') : responseStr;
|
|
91
|
+
const parts = responseStr.trim().split("=")
|
|
92
|
+
if (parts.length !== 2) {
|
|
93
|
+
console.error(`Unable to parse response: ${responseStr}`);
|
|
94
|
+
reject(new Error(`Unable to parse response: ${responseStr}`));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (parts[0].trim() !== "thtk") {
|
|
98
|
+
console.error(`Unable to extract thtk from response: ${responseStr}`);
|
|
99
|
+
reject(new Error(`Unable to extract thtk from response: ${responseStr}`));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
resolve(parts[1].trim());
|
|
103
|
+
} catch (e) {
|
|
104
|
+
console.error("Unable to parse response", responseStr, e);
|
|
105
|
+
reject(e);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
request.on('error', (err) => {
|
|
111
|
+
reject(err);
|
|
112
|
+
});
|
|
113
|
+
} catch (e) {
|
|
114
|
+
reject(e);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
59
119
|
#generateHeaders() {
|
|
60
120
|
const time = Math.round((new Date()).getTime() / 1000);
|
|
61
121
|
const nonce = crypto.randomBytes(40).toString('hex');
|
|
@@ -66,7 +126,7 @@ class Tranzila {
|
|
|
66
126
|
|
|
67
127
|
return {
|
|
68
128
|
'Content-Type': 'application/json',
|
|
69
|
-
'User-Agent': '
|
|
129
|
+
'User-Agent': 'Server',
|
|
70
130
|
'X-tranzila-api-app-key': this.#publicKey,
|
|
71
131
|
'X-tranzila-api-request-time': time,
|
|
72
132
|
'X-tranzila-api-nonce': nonce,
|