postpaid.js
4.57 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
const MODULE_NAME = 'HIT.POSTPAID';
const axios = require('axios').default;
const urljoin = require('url-join');
const uniqid = require('uniqid');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const translateRc = require('../translate-rc');
const composeCallbackUrl = require('./compose-callback-url');
const dumpReqRes = require('./dump-req-res');
const report = require('../report/postpaid');
const axiosErrorIsSafe = require('./axios-error-is-safe');
module.exports = async (task, isPay) => {
const xid = uniqid();
const hitType = isPay ? 'PAY' : 'INQUIRY';
logger.verbose(`${MODULE_NAME} 0EDCEB4F: Processing task`, {
xid,
hitType,
task,
});
const params = {
request_id: task.trx_id,
terminal_name: config.partner.terminal_name,
password: config.partner.password,
destination: task.destination,
product_name: task.remote_product,
reverse_url: composeCallbackUrl(xid, task, true),
};
const endpointUrl = urljoin(
config.partner.url,
isPay ? 'pay' : 'inquiry',
);
let lastResponse = null;
try {
logger.verbose(`${MODULE_NAME} EFCF6C2A: Going to HIT POSTPAID endpoint`, {
xid,
endpointUrl,
params,
});
const response = await axios.get(endpointUrl, {
headers: {
'User-Agent': 'KOMODO-GW-HTTPGETX',
},
timeout: config.partner.hit_timeout_ms || 30 * 1000,
params,
});
if (!response) {
const e = new Error(`${MODULE_NAME} 364AB160: Empty response`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
if (!response.data) {
const e = new Error(`${MODULE_NAME} E64BCD17: Empty response data`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
if (typeof response.data !== 'object') {
const e = new Error(`${MODULE_NAME} E64BCD17: Response data is not a JSON`);
e.rc = isPay ? '68' : '90';
e.response = response;
throw e;
}
lastResponse = response;
logger.verbose(`${MODULE_NAME} 924E4510: Got a direct response`, {
xid,
responseBody: response.data,
});
report(xid, {
command: response.data.command || hitType,
trx_id: task.trx_id,
rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc
: '68',
sn: response.data.sn || null,
amount: Number(response.data.amount) || undefined,
amount_to_charge: Number(response.data.amount_to_charge) || undefined,
balance: Number(response.data.ending_balance)
|| Number(response.data.balance)
|| undefined,
base_bill_amount: (response.data.base_bill_amount) || undefined,
bill_count: Number(response.data.bill_count) || undefined,
message: {
xid,
'DIRECT-RESPONSE': response.data,
},
info: response.data.info || undefined,
detail: response.data.detail || undefined,
data: response.data.data || undefined,
struk: response.data.struk || undefined,
});
} catch (e) {
const rc = e.rc
|| (axiosErrorIsSafe(e) && '91')
|| (!isPay && '91')
|| '68';
logger.warn(`${MODULE_NAME} 57764852: Exception`, {
xid,
eCode: e.code,
eMessage: e.message,
eRc: e.rc,
rc,
responseHttpStatus: e.response && e.response.status,
responseBody: e.response && e.response.data,
});
lastResponse = e.response;
report(xid, {
command: hitType,
trx_id: task.trx_id,
rc,
message: {
xid,
'KOMODO-GW-ERROR': {
eCode: e.code,
eMessage: e.message,
responseHttpStatus: e.response && e.response.status,
responseBody: e.response && e.response.data,
},
},
});
} finally {
dumpReqRes(
xid,
task,
'GET',
endpointUrl,
params,
lastResponse && lastResponse.data,
lastResponse && lastResponse.status,
lastResponse,
false,
);
}
};