Commit 92940f844e854b741aef41946ba8937da93d3f07
1 parent
fd23c5a272
Exists in
master
Postpaid ready to test
Showing 5 changed files with 181 additions and 11 deletions Side-by-side Diff
lib/hit/postpaid-inquiry.js
lib/hit/postpaid.js
... | ... | @@ -0,0 +1,152 @@ |
1 | +const MODULE_NAME = 'HIT.POSTPAID'; | |
2 | + | |
3 | +const axios = require('axios').default; | |
4 | +const urljoin = require('url-join'); | |
5 | +const uniqid = require('uniqid'); | |
6 | + | |
7 | +const config = require('komodo-sdk/config'); | |
8 | +const logger = require('komodo-sdk/logger'); | |
9 | + | |
10 | +const translateRc = require('../translate-rc'); | |
11 | +const composeCallbackUrl = require('./compose-callback-url'); | |
12 | +const dumpReqRes = require('./dump-req-res'); | |
13 | +const report = require('../report/postpaid'); | |
14 | +const axiosErrorIsSafe = require('./axios-error-is-safe'); | |
15 | + | |
16 | +module.exports = async (task, isPay) => { | |
17 | + const xid = uniqid(); | |
18 | + const hitType = isPay ? 'PAY' : 'INQUIRY'; | |
19 | + | |
20 | + logger.verbose(`${MODULE_NAME} 0EDCEB4F: Processing task`, { | |
21 | + xid, | |
22 | + hitType, | |
23 | + task, | |
24 | + }); | |
25 | + | |
26 | + const params = { | |
27 | + request_id: task.trx_id, | |
28 | + terminal_name: config.partner.terminal_name, | |
29 | + password: config.partner.password, | |
30 | + destination: task.destination, | |
31 | + product_name: task.remote_product, | |
32 | + reverse_url: composeCallbackUrl(xid, true), | |
33 | + }; | |
34 | + | |
35 | + const endpointUrl = urljoin( | |
36 | + config.partner.url, | |
37 | + isPay ? 'pay' : 'inquiry', | |
38 | + ); | |
39 | + | |
40 | + let lastResponse = null; | |
41 | + | |
42 | + try { | |
43 | + logger.verbose(`${MODULE_NAME} EFCF6C2A: Going to HIT POSTPAID endpoint`, { | |
44 | + xid, | |
45 | + endpointUrl, | |
46 | + params, | |
47 | + }); | |
48 | + | |
49 | + const response = await axios.get(endpointUrl, { | |
50 | + headers: { | |
51 | + 'User-Agent': 'KOMODO-GW-HTTPGETX', | |
52 | + }, | |
53 | + timeout: config.partner.hit_timeout_ms || 30 * 1000, | |
54 | + params, | |
55 | + }); | |
56 | + | |
57 | + if (!response) { | |
58 | + const e = new Error(`${MODULE_NAME} 364AB160: Empty response`); | |
59 | + e.rc = isPay ? '68' : '90'; | |
60 | + e.response = response; | |
61 | + throw e; | |
62 | + } | |
63 | + | |
64 | + if (!response.data) { | |
65 | + const e = new Error(`${MODULE_NAME} E64BCD17: Empty response data`); | |
66 | + e.rc = isPay ? '68' : '90'; | |
67 | + e.response = response; | |
68 | + throw e; | |
69 | + } | |
70 | + | |
71 | + if (typeof response.data !== 'object') { | |
72 | + const e = new Error(`${MODULE_NAME} E64BCD17: Response data is not a JSON`); | |
73 | + e.rc = isPay ? '68' : '90'; | |
74 | + e.response = response; | |
75 | + throw e; | |
76 | + } | |
77 | + | |
78 | + lastResponse = response; | |
79 | + | |
80 | + logger.verbose(`${MODULE_NAME} 924E4510: Got a direct response`, { | |
81 | + xid, | |
82 | + responseBody: response.data, | |
83 | + }); | |
84 | + | |
85 | + report(xid, { | |
86 | + command: response.data.command || hitType, | |
87 | + trx_id: task.trx_id, | |
88 | + rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc | |
89 | + : '68', | |
90 | + sn: response.data.sn || null, | |
91 | + amount: Number(response.data.amount) || undefined, | |
92 | + amount_to_charge: Number(response.data.amount_to_charge) || undefined, | |
93 | + balance: Number(response.data.ending_balance) | |
94 | + || Number(response.data.balance) | |
95 | + || undefined, | |
96 | + base_bill_amount: (response.data.base_bill_amount) || undefined, | |
97 | + bill_count: Number(response.data.bill_count) || undefined, | |
98 | + message: { | |
99 | + xid, | |
100 | + 'DIRECT-RESPONSE': response.data, | |
101 | + }, | |
102 | + info: response.data.info || undefined, | |
103 | + detail: response.data.detail || undefined, | |
104 | + data: response.data.data || undefined, | |
105 | + struk: response.data.struk || undefined, | |
106 | + }); | |
107 | + } catch (e) { | |
108 | + const rc = e.rc | |
109 | + || (axiosErrorIsSafe(e) && '91') | |
110 | + || (!isPay && '91') | |
111 | + || '68'; | |
112 | + | |
113 | + logger.warn(`${MODULE_NAME} 57764852: Exception`, { | |
114 | + xid, | |
115 | + eCode: e.code, | |
116 | + eMessage: e.message, | |
117 | + eRc: e.rc, | |
118 | + rc, | |
119 | + responseHttpStatus: e.response && e.response.status, | |
120 | + responseBody: e.response && e.response.data, | |
121 | + }); | |
122 | + | |
123 | + lastResponse = e.response; | |
124 | + | |
125 | + report(xid, { | |
126 | + command: hitType, | |
127 | + trx_id: task.trx_id, | |
128 | + rc, | |
129 | + message: { | |
130 | + xid, | |
131 | + 'KOMODO-GW-ERROR': { | |
132 | + eCode: e.code, | |
133 | + eMessage: e.message, | |
134 | + responseHttpStatus: e.response && e.response.status, | |
135 | + responseBody: e.response && e.response.data, | |
136 | + }, | |
137 | + }, | |
138 | + }); | |
139 | + } finally { | |
140 | + dumpReqRes( | |
141 | + xid, | |
142 | + task, | |
143 | + 'GET', | |
144 | + endpointUrl, | |
145 | + params, | |
146 | + lastResponse && lastResponse.data, | |
147 | + lastResponse && lastResponse.status, | |
148 | + lastResponse, | |
149 | + false, | |
150 | + ); | |
151 | + } | |
152 | +}; |
lib/partner-postpaid.js
1 | -const hitInquiry = require('./hit/postpaid-inquiry'); | |
2 | -const hitPay = require('./hit/postpaid-pay'); | |
1 | +const hit = require('./hit/postpaid'); | |
3 | 2 | |
4 | 3 | exports.inquiry = (task) => { |
5 | - hitInquiry(task); | |
4 | + hit(task, false); | |
6 | 5 | }; |
7 | 6 | |
8 | 7 | exports.pay = (task) => { |
9 | - hitPay(task); | |
8 | + hit(task, true); | |
10 | 9 | }; |
lib/report/postpaid.js
... | ... | @@ -0,0 +1,22 @@ |
1 | +const MODULE_NAME = 'REPORT.POSTPAID'; | |
2 | + | |
3 | +const stringify = require('json-stringify-pretty-compact'); | |
4 | + | |
5 | +const logger = require('komodo-sdk/logger'); | |
6 | +const postpaidSdk = require('komodo-sdk-postpaid'); | |
7 | + | |
8 | +module.exports = (xid, data) => { | |
9 | + if (!data) return; | |
10 | + | |
11 | + logger.verbose(`${MODULE_NAME} 0682477A: Reporting to CORE`, { | |
12 | + xid, | |
13 | + data, | |
14 | + }); | |
15 | + | |
16 | + const reportData = JSON.parse(JSON.stringify(data)); | |
17 | + if (typeof reportData.message !== 'string') { | |
18 | + reportData.message = stringify(reportData.message); | |
19 | + } | |
20 | + | |
21 | + postpaidSdk.report(reportData, xid); | |
22 | +}; |
lib/report/prepaid.js
... | ... | @@ -13,10 +13,10 @@ module.exports = (xid, data) => { |
13 | 13 | data, |
14 | 14 | }); |
15 | 15 | |
16 | - const reportMsg = JSON.parse(JSON.stringify(data)); | |
17 | - if (typeof reportMsg.message !== 'string') { | |
18 | - reportMsg.message = stringify(reportMsg.message); | |
16 | + const reportData = JSON.parse(JSON.stringify(data)); | |
17 | + if (typeof reportData.message !== 'string') { | |
18 | + reportData.message = stringify(reportData.message); | |
19 | 19 | } |
20 | 20 | |
21 | - pull.report(reportMsg); | |
21 | + pull.report(reportData); | |
22 | 22 | }; |