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