Commit 6a81df702f5a4cd4d543f48cffd9d2751edc3a52
1 parent
817940d4de
Exists in
master
penanganan rc14 dari partner
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 cleanBalance(balance) { | 57 | function cleanBalance(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 responseAmount; | 114 | let responseAmount; |
115 | if (response.data && response.data.info && response.data.info.amount) { | 115 | if (response.data && response.data.info && response.data.info.amount) { |
116 | responseAmount = response.data.info.amount; | 116 | responseAmount = response.data.info.amount; |
117 | 117 | ||
118 | if (responseAmount) { | 118 | if (responseAmount) { |
119 | messages.push('Amount: ' + responseAmount); | 119 | messages.push('Amount: ' + responseAmount); |
120 | } | 120 | } |
121 | } | 121 | } |
122 | 122 | ||
123 | let responseBalance; | 123 | let responseBalance; |
124 | if (response.data && response.data.balance) { | 124 | if (response.data && response.data.balance) { |
125 | responseBalance = response.data.balance; | 125 | responseBalance = response.data.balance; |
126 | messages.push('Balance: ' + responseBalance); | 126 | messages.push('Balance: ' + responseBalance); |
127 | if (responseBalance) { | 127 | if (responseBalance) { |
128 | responseBalance = cleanBalance(responseBalance); | 128 | responseBalance = cleanBalance(responseBalance); |
129 | } | 129 | } |
130 | } | 130 | } |
131 | 131 | ||
132 | if (messages.length <= 0) { messages.push('Transaksi anda sedang diproses'); } | 132 | if (messages.length <= 0) { messages.push('Transaksi anda sedang diproses'); } |
133 | 133 | ||
134 | let coreReportData = { | 134 | let coreReportData = { |
135 | trx_id: taskTrxId || responseRequestId, | 135 | trx_id: taskTrxId || responseRequestId, |
136 | rc: '68', | 136 | rc: '68', |
137 | message: messages.join('. ') + '.', | 137 | message: messages.join('. ') + '.', |
138 | sn: '', | 138 | sn: '', |
139 | handler: config.handler_name, | 139 | handler: config.handler_name, |
140 | amount: responseAmount, | 140 | amount: responseAmount, |
141 | balance: responseBalance, | 141 | balance: responseBalance, |
142 | core_task: task, | 142 | core_task: task, |
143 | raw: body | 143 | raw: body |
144 | } | 144 | } |
145 | 145 | ||
146 | if (responseStatus == 'Error') { | 146 | if (responseStatus == 'Error') { |
147 | if (responseInfo == 'insufficient balance') { | 147 | if (responseInfo == 'insufficient balance') { |
148 | coreReportData.rc = '91'; | 148 | coreReportData.rc = '91'; |
149 | } | 149 | } |
150 | 150 | ||
151 | coreReportData.message = [responseStatus, responseInfo].join(': '); | 151 | coreReportData.message = [responseStatus, responseInfo].join(': '); |
152 | 152 | ||
153 | partner.reportToCore(coreReportData); | 153 | partner.reportToCore(coreReportData); |
154 | return; | 154 | return; |
155 | } | 155 | } |
156 | 156 | ||
157 | 157 | ||
158 | if (responseTrxStatus == 'P') { | 158 | if (responseTrxStatus == 'P') { |
159 | logger.verbose('Got pending trx response', {response: response.data}); | 159 | logger.verbose('Got pending trx response', {response: response.data}); |
160 | coreReportData.rc = '68'; | 160 | coreReportData.rc = '68'; |
161 | } | 161 | } |
162 | else if (responseTrxStatus == 'S') { | 162 | else if (responseTrxStatus == 'S') { |
163 | logger.verbose('Got succcess trx response', {response: response.data}); | 163 | logger.verbose('Got succcess trx response', {response: response.data}); |
164 | 164 | ||
165 | coreReportData.rc = '00' | 165 | coreReportData.rc = '00' |
166 | 166 | ||
167 | coreReportData.sn = composeSn(response); | 167 | coreReportData.sn = composeSn(response); |
168 | coreReportData.message += ' SN=' + coreReportData.sn + '.'; | 168 | coreReportData.message += ' SN=' + coreReportData.sn + '.'; |
169 | } | 169 | } |
170 | else if (responseTrxStatus == 'R') { | 170 | else if (responseTrxStatus == 'R') { |
171 | logger.verbose('Got rejected trx response', {response: response.data}); | 171 | logger.verbose('Got rejected trx response', {response: response.data}); |
172 | 172 | ||
173 | const partnerRC = getPartnerRCFromDiagMessage(responseDiag); | 173 | const partnerRC = getPartnerRCFromDiagMessage(responseDiag); |
174 | if (partnerRC == '15') { | 174 | if (['14', '15'].indexOf(partnerRC) >= 0) { |
175 | coreReportData.rc = '14'; | 175 | coreReportData.rc = '14'; |
176 | } | 176 | } |
177 | else { | 177 | else { |
178 | coreReportData.rc = '40'; | 178 | coreReportData.rc = '40'; |
179 | } | 179 | } |
180 | } | 180 | } |
181 | 181 | ||
182 | partner.reportToCore(coreReportData); | 182 | partner.reportToCore(coreReportData); |
183 | } | 183 | } |
184 | 184 | ||
185 | function composeSn(response) { | 185 | function composeSn(response) { |
186 | if (!response && !response.data) { return; } | 186 | if (!response && !response.data) { return; } |
187 | 187 | ||
188 | if (!response.data.info) { | 188 | if (!response.data.info) { |
189 | return response.data.serial; | 189 | return response.data.serial; |
190 | } | 190 | } |
191 | 191 | ||
192 | let token = response.data.serial; | 192 | let token = response.data.serial; |
193 | let cust_name = response.data.info.cust_name; | 193 | let cust_name = response.data.info.cust_name; |
194 | let tariff = response.data.info.kelas; | 194 | let tariff = response.data.info.kelas; |
195 | let total_kwh = response.data.info.size; | 195 | let total_kwh = response.data.info.size; |
196 | 196 | ||
197 | if (tariff.indexOf('VA') < 0) { | 197 | if (tariff.indexOf('VA') < 0) { |
198 | tariff += 'VA'; | 198 | tariff += 'VA'; |
199 | } | 199 | } |
200 | 200 | ||
201 | if (cust_name) { | 201 | if (cust_name) { |
202 | cust_name = cust_name.replace(/\W+/g, ' ').trim().replace(/\W+/g, '-').toUpperCase(); | 202 | cust_name = cust_name.replace(/\W+/g, ' ').trim().replace(/\W+/g, '-').toUpperCase(); |
203 | } | 203 | } |
204 | 204 | ||
205 | return [ token, cust_name, tariff, total_kwh ].join('/'); | 205 | return [ token, cust_name, tariff, total_kwh ].join('/'); |
206 | } | 206 | } |
207 | 207 | ||
208 | function getPartnerRCFromDiagMessage(diag) { | 208 | function getPartnerRCFromDiagMessage(diag) { |
209 | let matches = diag.match(/^\s*\[(.*)\]/); | 209 | let matches = diag.match(/^\s*\[(.*)\]/); |
210 | if (!matches || matches.length < 2) { | 210 | if (!matches || matches.length < 2) { |
211 | return; | 211 | return; |
212 | } | 212 | } |
213 | 213 | ||
214 | return matches[1]; | 214 | return matches[1]; |
215 | } | 215 | } |
216 | 216 | ||
217 | 217 | ||
218 | exports.calculateSign = calculateSign; | 218 | exports.calculateSign = calculateSign; |
219 | exports.createRequestOptions = createRequestOptions; | 219 | exports.createRequestOptions = createRequestOptions; |
220 | exports.processPartnerResponseBody = processPartnerResponseBody; | 220 | exports.processPartnerResponseBody = processPartnerResponseBody; |
221 | 221 |