hit.js
3.86 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
const MODULE_NAME = 'HIT';
const axios = require('axios').default;
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const composePayload = require('./generic-xmlrpc/compose-payload');
const report = require('./report');
const dumper = require('./dumper/request');
const axiosSafeFailed = require('./axios-safe-failed');
const parseResult = require('./parse-result');
const defaultAxiosConfig = {
headers: {
'User-Agent': 'Komodo-GW-ST24B',
'Content-Type': 'text/xml',
},
timeout: config.partner.hit_timeout = 2 * 60 * 120,
};
module.exports = async (xid, task) => {
const methodName = config.partner.topuprequest_method_name || 'topUpRequest';
logger.verbose(`${MODULE_NAME} E4F52474: Processing task`, {
xid, methodName, task,
});
const params = {
MSISDN: config.partner.msisdn,
REQUESTID: task.trx_id,
PIN: config.partner.pin,
NOHP: task.destination,
NOM: task.remote_product,
};
const payload = composePayload(methodName, params);
const axiosConfig = JSON.parse(JSON.stringify(defaultAxiosConfig));
axiosConfig.headers['Content-length'] = payload.length;
let response;
let eToDump;
const endpointUrl = config.partner.url;
report(xid, {
trx_id: task.trx_id,
rc: '68',
message: {
xid,
msg: `Sending request to ${config.partner.url}`,
},
});
try {
logger.verbose(`${MODULE_NAME} 47FDCA85: Going to HIT partner`, {
xid,
endpointUrl,
methodName,
params,
});
response = await axios.post(endpointUrl, payload, axiosConfig);
if (!response) {
const e = new Error(`${MODULE_NAME} BAACC918: Empty response`);
e.rc = '90';
logger.warn(e.message, { xid });
throw e;
}
if (!response.data) {
const e = new Error(`${MODULE_NAME} C816D842: Empty response data`);
e.rc = '90';
logger.warn(e.message, { xid });
throw e;
}
if (typeof response.data !== 'string') {
const e = new Error(`${MODULE_NAME} 32A75E8E: Response data is not a string`);
e.rc = '90';
logger.warn(e.message, { xid });
throw e;
}
logger.verbose(`${MODULE_NAME} D0CBD82A: Parsing response`, { xid, responseBody: response.data });
parseResult(xid, task.trx_id, response.data, false);
} catch (e) {
eToDump = e;
logger.warn(`${MODULE_NAME} 2653B932: Got an exception`, {
xid,
eCode: e.code,
eMessage: e.message,
eRc: e.rc,
responseStatus: e.response && e.response.status,
responseStatusText: e.response && e.response.statusText,
responseContentType: e.response && e.response.headers && e.response.headers['content-type'],
responseBody: e.response && e.response.data,
});
if (e.response) response = e.response;
const rc = e.rc
|| (axiosSafeFailed(e) || '91')
|| '68';
report(xid, {
trx_id: task.trx_id,
rc,
message: {
xid,
'GW-ERROR': {
code: e.code,
message: e.message,
endpointUrl,
httpStatus: (e.response && e.response.status) || null,
httpStatusText: (e.response && e.response.statusText) || null,
responseBody: e.response && e.response.data,
},
},
});
} finally {
dumper(
xid,
task,
endpointUrl,
payload,
response.config || axiosConfig,
response,
eToDump,
);
}
};