partner-misc.js
5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";
const crypto = require('crypto');
const moment = require('moment');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const pull = require('komodo-sdk/gateway/pull');
const partner = require('./partner');
function calculateSign(dt, trx_ref_id, cust_num, username, password) {
const cryptoHashPassword = crypto.createHash('sha1');
const cryptoHashUsernamePassword = crypto.createHash('sha1');
const cryptoHashOutest = crypto.createHash('sha1');
cryptoHashPassword.update(password);
const hashPassword = cryptoHashPassword.digest('hex');
cryptoHashUsernamePassword.update(username + hashPassword);
const hashUsernamePassword = cryptoHashUsernamePassword.digest('hex');
cryptoHashOutest.update(dt + trx_ref_id + cust_num + hashUsernamePassword);
return cryptoHashOutest.digest('hex');
}
function createRequestOptions(task, config) {
const dt = moment().format('YYYY-MM-DD HH:mm:ss');
const sign = calculateSign(dt, task.trx_id, task.destination, config.partner.username, config.partner.password);
return {
url: config.partner.url,
form: {
username: config.partner.username,
datetime: dt,
code: task.remote_product,
trx_ref_id: task.trx_id,
cust_num: task.destination,
sign: sign
}
}
}
function decodeResponseBody(responseBody) {
let response;
try {
response = JSON.parse(responseBody);
}
catch(e) {
logger.warn('Error parsing response body');
}
return response;
}
function cleanNumber(balance) {
try {
balance = balance.replace(/\D/g, '');
}
catch(e) { };
return balance;
}
function processPartnerResponseBody(body, task) {
let response = decodeResponseBody(body);
let messages = [];
if (!response) {
return;
}
logger.verbose('RESPONSE', {response: response});
const responseStatus = response.status;
const responseInfo = response.info;
let taskTrxId;
if (task && task.trx_id) {
taskTrxId = task.trx_id;
}
let responseRequestId;
if (response.data && response.data.request_id) {
responseRequestId = response.data.request_id;
}
let responseTrxStatus;
if (response.data && response.data.trx_status) {
responseTrxStatus = response.data.trx_status;
}
let responseTimestamp;
if (response.data && response.data.timestamp) {
responseTimestamp = response.data.timestamp;
messages.push(responseTimestamp);
}
let responseDiag;
if (response.data && response.data.diag) {
responseDiag = response.data.diag;
messages.push(responseDiag);
}
let responseMessage;
if (response.data && response.data.message) {
responseMessage = response.data.message;
messages.push(responseMessage);
}
let responseHarga;
if (response.data && response.data.harga) {
responseHarga = response.data.harga;
messages.push('Price: ' + responseHarga);
if (responseHarga) {
responseHarga = cleanNumber(responseHarga);
}
}
let responseBalance;
if (response.data && response.data.balance) {
responseBalance = response.data.balance;
messages.push('Balance: ' + responseBalance);
if (responseBalance) {
responseBalance = cleanNumber(responseBalance);
}
}
if (messages.length <= 0) { messages.push('Transaksi anda sedang diproses'); }
let coreReportData = {
trx_id: taskTrxId || responseRequestId,
rc: '68',
message: messages.join('. ') + '.',
sn: null,
handler: config.handler_name,
amount: responseHarga,
balance: responseBalance,
task: task,
raw: body
}
if (responseStatus == 'Error') {
if (responseInfo == ['insufficient balance', 'System Cut-Off'] >= 0) {
coreReportData.rc = '91';
}
coreReportData.message = [responseStatus, responseInfo].join(': ');
partner.reportToCore(coreReportData);
return;
}
if (responseTrxStatus == 'P') {
logger.verbose('Got pending trx response', {response: response.data});
coreReportData.rc = '68';
}
else if (responseTrxStatus == 'S') {
logger.verbose('Got succcess trx response', {response: response.data});
coreReportData.rc = '00'
coreReportData.sn = composeSn(response);
coreReportData.message += ' SN=' + coreReportData.sn + '.';
}
else if (responseTrxStatus == 'R') {
logger.verbose('Got rejected trx response', {response: response.data});
const partnerRC = getPartnerRCFromDiagMessage(responseDiag);
if (['14', '15'].indexOf(partnerRC) >= 0) {
coreReportData.rc = '14';
}
else {
coreReportData.rc = '40';
}
}
partner.reportToCore(coreReportData);
}
function composeSn(response) {
if (!response && !response.data) { return; }
if (!response.data.info) {
return response.data.serial;
}
let token = response.data.serial;
let cust_name = response.data.info.cust_name;
let tariff = response.data.info.kelas;
let total_kwh = response.data.info.size;
if (tariff.indexOf('VA') < 0) {
tariff += 'VA';
}
if (cust_name) {
cust_name = cust_name.replace(/\W+/g, ' ').trim().replace(/\W+/g, '-').toUpperCase();
}
return [ token, cust_name, tariff, total_kwh ].join('/');
}
function getPartnerRCFromDiagMessage(diag) {
let matches = diag.match(/^\s*\[(.*)\]/);
if (!matches || matches.length < 2) {
return;
}
return matches[1];
}
exports.calculateSign = calculateSign;
exports.createRequestOptions = createRequestOptions;
exports.processPartnerResponseBody = processPartnerResponseBody;