partner-bnisp.js
5.97 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
223
224
225
226
227
228
229
230
"use strict";
const http = require('http');
http.globalAgent.maxSockets = Infinity;
const request = require('request');
var config;
var aaa;
var logger;
function start(options) {
if (!options) {
console.log('Undefined options, terminating....');
process.exit(1);
}
if (options.config) {
config = options.config;
} else {
console.log('Undefined options.config, terminating....')
process.exit(1);
}
if (options.aaa) {
aaa = options.aaa;
} else {
console.log('Undefined options.aaa, terminating....')
process.exit(1);
}
if (options && options.logger) {
logger = options.logger;
} else {
console.log('Undefined options.logger, terminating....')
process.exit(1);
}
}
function callbackReport(requestId, rc, message, options) {
aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
}
function topupRequest(task) {
aaa.insertTaskToMongoDb(task);
_hitTopup(task);
}
function _hitTopup(task, isCheckStatus) {
const remoteProduct = task.remoteProduct.split('/');
if (remoteProduct.length < 4) {
callbackReport(task.requestId, '40', 'INTERNAL_MSG: Invalid remoteProduct', task)
return;
}
let pathParams = {
request_type: "purchase",
noid: config.h2h_out.noid,
token: config.h2h_out.token,
product: remoteProduct[0],
product_type: remoteProduct[1],
id_pel: task.destination,
nominal: remoteProduct[2],
admin: remoteProduct[3],
trace_id: task.requestId
}
const requestOptions = {
url: config.h2h_out.partner + createUrlPath(pathParams)
}
logger.verbose('Requeting to partner', {requestOptions: requestOptions});
request(requestOptions, function(error, response, body) {
if (error) {
let rc = '68';
if (!isCheckStatus && (error.syscall == 'connect')) {
rc = '91';
}
logger.warn('Error requesting to partner', {task: task, rc: rc, error: error});
callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error requesting to partner. ' + error, {task: task});
return;
}
if (response.statusCode != 200) {
let rc = '68';
logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode});
callbackReport(task.requestId, rc, 'INTERNAL_MSG: HTTP status code ' + response.statusCode, {task: task});
return;
}
if (!body) {
let rc = '68';
logger.warn('Error processing response body', {task: task, responseBody: body});
callbackReport(task.requestId, rc, 'INTERNAL_MSG: Error processing response body', {task: task});
return;
}
if (body.trim() == 'invalid specs') {
let rc = '40';
logger.warn('Invalid specs', {task: task, responseBody: body});
callbackReport(task.requestId, rc, body);
return;
}
logger.verbose('Got response from partner', {task: task, responseBody: body});
const responseData = parseResponseBody(body);
logger.verbose('Response body parsed as json value', {responseData: responseData});
const data = responseDataProcessor(responseData);
callbackReport(task.requestId, data.rc, data.combinedMessage, {task: task});
})
}
function createUrlPath(options) {
let urlPath = [
"get",
options.request_type || "purchase",
"json",
options.noid,
options.token,
options.product,
options.product_type,
options.id_pel,
options.nominal || 0,
options.admin || 0,
options.trace_id
].join('/');
return '/' + urlPath;
}
function parseResponseBody(body) {
let data;
try {
data = JSON.parse(body);
}
catch(e) {
if (logger) {
logger.warn('Exception on parsing result body: ' + e);
return;
}
}
return data;
}
function responseDataProcessor(responseData) {
let retval = {
rc: '68',
sn: '',
responseMessage: '',
amount: 0,
balance: 0,
ts: '',
product: '',
productType: ''
}
let combinedMessage = [];
if (responseData.response_code == '0000') {
retval.rc = '00';
}
else if (['9990','0099','0068','0063','0005','9997','9999','9998','9990','0600','1003'].indexOf(responseData.response_code) >= 0) {
retval.rc = '68';
}
else {
retval.rc = '40';
}
if (responseData.response_code) {
combinedMessage.push(responseData.response_code);
}
if (responseData.product) {
combinedMessage.push(responseData.product)
}
if (responseData.produk_tipe) {
combinedMessage.push(responseData.produk_tipe)
}
if (responseData.idpel) {
combinedMessage.push(responseData.idpel)
}
retval.responseMessage = responseData.response_message || '';
if (retval.responseMessage) {
combinedMessage.push(retval.responseMessage);
}
if (responseData.detail) {
retval.sn = responseData.detail.voucherSerialNumber || '';
}
retval.amount = responseData.amount || 0;
combinedMessage.push('amount: ' + retval.amount);
retval.balance = responseData.saldo || 0;
combinedMessage.push('balance: ' + retval.balance);
retval.ts = responseData.waktu || '';
combinedMessage.push(retval.ts);
retval.loadTime = responseData.loadTime || '';
combinedMessage.push('load time: ' + retval.loadTime);
retval.combinedMessage = combinedMessage.join(' ');
if (retval.sn) {
retval.combinedMessage = 'SN=' + retval.sn + '; ' + retval.combinedMessage;
}
return retval;
}
exports.start = start;
exports.createUrlPath = createUrlPath;
exports.parseResponseBody = parseResponseBody;
exports.responseDataProcessor = responseDataProcessor;
exports.topupRequest = topupRequest;