Compare View
Commits (2)
Changes
Showing 2 changed files Inline Diff
center/messaging/trx-center.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | /** | 3 | /** |
4 | * Trx Handler untuk center messaging | 4 | * Trx Handler untuk center messaging |
5 | */ | 5 | */ |
6 | 6 | ||
7 | const module_name = require('path').basename(__filename); | 7 | const module_name = require('path').basename(__filename); |
8 | 8 | ||
9 | const request = require('request'); | 9 | const request = require('request'); |
10 | const strftime = require('strftime'); | 10 | const strftime = require('strftime'); |
11 | const config = require('../../config'); | 11 | const config = require('../../config'); |
12 | const logger = require('../../logger'); | 12 | const logger = require('../../logger'); |
13 | const httpResponseServer = require('../http-response-server'); | 13 | const httpResponseServer = require('../http-response-server'); |
14 | const controlPanel = require('../../control-panel'); | 14 | const controlPanel = require('../../control-panel'); |
15 | const heartbeat = require('../../heartbeat'); | 15 | const heartbeat = require('../../heartbeat'); |
16 | 16 | ||
17 | let transport; | 17 | let transport; |
18 | 18 | ||
19 | heartbeat.setModuleType('center') | 19 | heartbeat.setModuleType('center') |
20 | 20 | ||
21 | function onOnline(params) { | 21 | function onOnline(params) { |
22 | logger.info('CENTER is ONLINE, ready to communicate'); | 22 | logger.info('CENTER is ONLINE, ready to communicate'); |
23 | } | 23 | } |
24 | 24 | ||
25 | function onIncomingMessage(paramsFromTransport, cb) { | 25 | function onIncomingMessage(paramsFromTransport, cb) { |
26 | logger.verbose('Reporting message to CORE') | 26 | logger.verbose('Reporting message to CORE') |
27 | 27 | ||
28 | const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase(); | 28 | const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase(); |
29 | 29 | ||
30 | if (config.commands && config.commands.balance && config.commands.balance.indexOf(command) >= 0) { | 30 | if (config.commands && config.commands.balance && config.commands.balance.indexOf(command) >= 0) { |
31 | executeBalanceCheck(paramsFromTransport, cb); | 31 | executeBalanceCheck(paramsFromTransport, cb); |
32 | } | 32 | } |
33 | else if (config.commands && config.commands.price && config.commands.price.indexOf(command) >= 0) { | 33 | else if (config.commands && config.commands.price && config.commands.price.indexOf(command) >= 0) { |
34 | executePriceCheck(paramsFromTransport, cb); | 34 | executePriceCheck(paramsFromTransport, cb); |
35 | } | 35 | } |
36 | else if (config.commands && config.commands.postpaid_inquiry && config.commands.postpaid_inquiry.indexOf(command) >= 0) { | 36 | else if (config.commands && config.commands.postpaid_inquiry && config.commands.postpaid_inquiry.indexOf(command) >= 0) { |
37 | executePostpaidInquiry(paramsFromTransport, cb); | ||
37 | executePostpaidInquiry(paramsFromTransport, cb); | 38 | |
38 | 39 | } | |
39 | } | 40 | else if (config.commands && config.commands.postpaid_pay && config.commands.postpaid_pay.indexOf(command) >= 0) { |
40 | else if (config.commands && config.commands.postpaid_pay && config.commands.postpaid_pay.indexOf(command) >= 0) { | 41 | executePostpaidInquiry(paramsFromTransport, cb); |
41 | executePostpaidInquiry(paramsFromTransport, cb); | 42 | } |
42 | } | 43 | else { |
43 | else { | 44 | executePrepaidBuy(paramsFromTransport, cb); |
44 | executePrepaidBuy(paramsFromTransport, cb); | 45 | } |
45 | } | 46 | } |
46 | } | 47 | |
47 | 48 | function executeBalanceCheck(paramsFromTransport) { | |
48 | function executeBalanceCheck(paramsFromTransport) { | 49 | const terminal_name = paramsFromTransport.partner.toLowerCase(); |
49 | const terminal_name = paramsFromTransport.partner.toLowerCase(); | 50 | const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1]; |
50 | const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1]; | 51 | |
51 | 52 | const requestOptions = { | |
52 | const requestOptions = { | 53 | url: config.core_url + '/services/balance', |
53 | url: config.core_url + '/services/balance', | 54 | qs: { |
54 | qs: { | 55 | terminal_name: terminal_name, |
55 | terminal_name: terminal_name, | 56 | password: password |
56 | password: password | 57 | } |
57 | } | 58 | } |
58 | } | 59 | |
59 | 60 | requestToCore(requestOptions); | |
60 | requestToCore(requestOptions); | 61 | } |
61 | } | 62 | |
62 | 63 | function executePriceCheck(paramsFromTransport) { | |
63 | function executePriceCheck(paramsFromTransport) { | 64 | const requestOptions = { |
64 | const requestOptions = { | 65 | url: config.core_url + '/services/pricelist', |
65 | url: config.core_url + '/services/pricelist', | 66 | qs: { |
66 | qs: { | 67 | terminal_name: paramsFromTransport.partner.toLowerCase(), |
67 | terminal_name: paramsFromTransport.partner.toLowerCase(), | 68 | keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1], |
68 | keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1], | 69 | password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2], |
69 | password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2], | 70 | postpaid: 0, |
70 | postpaid: 0, | 71 | } |
71 | } | 72 | } |
72 | } | 73 | |
73 | 74 | requestToCore(requestOptions); | |
74 | requestToCore(requestOptions); | 75 | } |
75 | } | 76 | |
76 | 77 | function parseCoreMessage(body) { | |
77 | function parseCoreMessage(body) { | 78 | let coreRes; |
78 | let coreRes; | 79 | |
79 | 80 | try { | |
80 | try { | 81 | coreRes = JSON.parse(body) |
81 | coreRes = JSON.parse(body) | 82 | } |
82 | } | 83 | catch(err) { |
83 | catch(err) { | 84 | logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); |
84 | logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); | 85 | coreRes = null; |
85 | coreRes = null; | 86 | } |
86 | } | 87 | |
87 | 88 | return coreRes; | |
88 | return coreRes; | 89 | } |
89 | } | 90 | |
90 | 91 | function generateRequestId(req) { | |
91 | function generateRequestId(req) { | 92 | return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); |
92 | return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); | 93 | } |
93 | } | 94 | |
94 | 95 | function executePrepaidBuy(paramsFromTransport, cb) { | |
95 | function executePrepaidBuy(paramsFromTransport, cb) { | 96 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); |
96 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); | 97 | |
97 | 98 | let qs = { | |
98 | let qs = { | 99 | request_id: tokens[3], |
99 | request_id: tokens[3], | 100 | terminal_name: paramsFromTransport.partner.toLowerCase(), |
100 | terminal_name: paramsFromTransport.partner.toLowerCase(), | 101 | product_name: tokens[0].toUpperCase(), |
101 | product_name: tokens[0].toUpperCase(), | 102 | destination: tokens[1].toUpperCase(), |
102 | destination: tokens[1].toUpperCase(), | 103 | password: tokens[2], |
103 | password: tokens[2], | 104 | origin: config.origin || config.username, |
104 | origin: config.origin || config.username, | 105 | report_port: config.listen_port || '80', |
105 | report_port: config.listen_port || '80', | 106 | msg: paramsFromTransport.msg, |
106 | msg: paramsFromTransport.msg, | 107 | reverse_url: paramsFromTransport.reverse_url |
107 | reverse_url: paramsFromTransport.reverse_url | 108 | } |
108 | } | 109 | |
109 | 110 | if (!config.do_not_prefix_request_id) { | |
110 | if (!config.do_not_prefix_request_id) { | 111 | qs.request_id = generateRequestId(qs); |
111 | qs.request_id = generateRequestId(qs); | 112 | if (tokens[3]) { |
112 | if (tokens[3]) { | 113 | qs.request_id += '_' + tokens[3]; |
113 | qs.request_id += '_' + tokens[3]; | 114 | } |
114 | } | 115 | } |
115 | } | 116 | |
116 | 117 | let requestOptions = { | |
117 | let requestOptions = { | 118 | url: config.core_url + '/prepaid/buy', |
118 | url: config.core_url + '/prepaid/buy', | 119 | qs: qs |
119 | qs: qs | 120 | } |
120 | } | 121 | |
121 | 122 | requestToCore(requestOptions, cb); | |
122 | requestToCore(requestOptions, cb); | 123 | } |
123 | } | 124 | |
124 | 125 | function executePostpaidInquiry(paramsFromTransport, cb) { | |
125 | function executePostpaidInquiry(paramsFromTransport, cb) { | 126 | // PAY.PLN.1234567890.PIN |
126 | // PAY.PLN.1234567890.PIN | 127 | |
127 | 128 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); | |
128 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); | 129 | |
129 | 130 | let qs = { | |
130 | let qs = { | 131 | request_id: tokens[4], |
131 | request_id: tokens[4], | 132 | terminal_name: paramsFromTransport.partner.toLowerCase(), |
132 | terminal_name: paramsFromTransport.partner.toLowerCase(), | 133 | product_name: tokens[1].toUpperCase(), |
133 | product_name: tokens[1].toUpperCase(), | 134 | destination: tokens[2].toUpperCase(), |
134 | destination: tokens[2].toUpperCase(), | 135 | password: tokens[3], |
135 | password: tokens[3], | 136 | origin: config.origin || config.username, |
136 | origin: config.origin || config.username, | 137 | report_port: config.listen_port || '80', |
137 | report_port: config.listen_port || '80', | 138 | msg: paramsFromTransport.msg, |
138 | msg: paramsFromTransport.msg, | 139 | reverse_url: paramsFromTransport.reverse_url |
139 | reverse_url: paramsFromTransport.reverse_url | 140 | } |
140 | } | 141 | |
141 | 142 | if (!config.do_not_prefix_request_id) { | |
142 | if (!config.do_not_prefix_request_id) { | 143 | qs.request_id = generateRequestId(qs); |
143 | qs.request_id = generateRequestId(qs); | 144 | if (tokens[3]) { |
144 | if (tokens[3]) { | 145 | qs.request_id += '_' + tokens[3]; |
145 | qs.request_id += '_' + tokens[3]; | 146 | } |
146 | } | 147 | } |
147 | } | 148 | |
148 | 149 | let requestOptions = { | |
149 | let requestOptions = { | 150 | url: config.core_url + '/postpaid/pay', |
150 | url: config.core_url + '/postpaid/pay', | 151 | qs: qs |
151 | qs: qs | 152 | } |
152 | } | 153 | |
153 | 154 | requestToCore(requestOptions, cb); | |
154 | requestToCore(requestOptions, cb); | 155 | } |
155 | } | 156 | |
156 | 157 | function executePostpaidPay(paramsFromTransport, cb) { | |
157 | function executePostpaidPay(paramsFromTransport, cb) { | 158 | // INQUIRY.PLN.1234567890.PIN |
158 | // INQUIRY.PLN.1234567890.PIN | 159 | |
159 | 160 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); | |
160 | let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); | 161 | |
161 | 162 | let qs = { | |
162 | let qs = { | 163 | request_id: tokens[4], |
163 | request_id: tokens[4], | 164 | terminal_name: paramsFromTransport.partner.toLowerCase(), |
164 | terminal_name: paramsFromTransport.partner.toLowerCase(), | 165 | product_name: tokens[1].toUpperCase(), |
165 | product_name: tokens[1].toUpperCase(), | 166 | destination: tokens[2].toUpperCase(), |
166 | destination: tokens[2].toUpperCase(), | 167 | password: tokens[3], |
167 | password: tokens[3], | 168 | origin: config.origin || config.username, |
168 | origin: config.origin || config.username, | 169 | report_port: config.listen_port || '80', |
169 | report_port: config.listen_port || '80', | 170 | msg: paramsFromTransport.msg, |
170 | msg: paramsFromTransport.msg, | 171 | reverse_url: paramsFromTransport.reverse_url |
171 | reverse_url: paramsFromTransport.reverse_url | 172 | } |
172 | } | 173 | |
173 | 174 | if (!config.do_not_prefix_request_id) { | |
174 | if (!config.do_not_prefix_request_id) { | 175 | qs.request_id = generateRequestId(qs); |
175 | qs.request_id = generateRequestId(qs); | 176 | if (tokens[3]) { |
176 | if (tokens[3]) { | 177 | qs.request_id += '_' + tokens[3]; |
177 | qs.request_id += '_' + tokens[3]; | 178 | } |
178 | } | 179 | } |
179 | } | 180 | |
180 | 181 | let requestOptions = { | |
181 | let requestOptions = { | 182 | url: config.core_url + '/postpaid/inquiry', |
182 | url: config.core_url + '/postpaid/inquiry', | 183 | qs: qs |
183 | qs: qs | 184 | } |
184 | } | 185 | |
185 | 186 | requestToCore(requestOptions, cb); | |
186 | requestToCore(requestOptions, cb); | 187 | } |
187 | } | 188 | |
188 | 189 | function requestToCore(requestOptions, cb) { | |
189 | function requestToCore(requestOptions, cb) { | 190 | logger.verbose('Requesting service to CORE', requestOptions); |
190 | logger.verbose('Requesting service to CORE', requestOptions); | 191 | |
191 | 192 | request(requestOptions, function(err, res, body) { | |
192 | request(requestOptions, function(err, res, body) { | 193 | if (err || res.statusCode != 200) { |
193 | if (err || res.statusCode != 200) { | 194 | logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); |
194 | logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); | 195 | if (cb) { |
195 | if (cb) { | 196 | cb(null, {msg: 'INTERNAL ERROR'}); |
196 | cb(null, {msg: 'INTERNAL ERROR'}); | 197 | } |
197 | } | 198 | else if (transport.send) { |
198 | else if (transport.send) { | 199 | transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); |
199 | transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); | 200 | } |
200 | } | 201 | return; |
201 | return; | 202 | } |
202 | } | 203 | |
203 | 204 | let result = parseCoreMessage(body); | |
204 | let result = parseCoreMessage(body); | 205 | if (!result || !result.message) { |
205 | if (!result || !result.message) { | 206 | if (cb) { |
206 | if (cb) { | 207 | cb(null, {msg: 'INTERNAL ERROR'}); |
207 | cb(null, {msg: 'INTERNAL ERROR'}); | 208 | } |
208 | } | 209 | else if (transport.send) { |
209 | else if (transport.send) { | 210 | transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); |
210 | transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); | 211 | } |
211 | } | 212 | return; |
212 | return; | 213 | } |
213 | } | 214 | |
214 | 215 | if (cb) { | |
215 | if (cb) { | 216 | cb(null, result); |
216 | cb(null, result); | 217 | } |
217 | } | 218 | else if (transport.send) { |
218 | else if (transport.send) { | 219 | transport.send(requestOptions.qs.terminal_name, result.message); |
219 | transport.send(requestOptions.qs.terminal_name, result.message); | 220 | |
220 | 221 | } | |
221 | } | 222 | }) |
222 | }) | 223 | } |
223 | } | 224 | |
224 | 225 | function setTransport(_transport) { | |
225 | function setTransport(_transport) { | 226 | transport = _transport; |
226 | transport = _transport; | 227 | httpResponseServer.setTransport(transport); |
227 | httpResponseServer.setTransport(transport); | 228 | } |
228 | } | 229 | |
229 | 230 | const callback = { | |
230 | const callback = { | 231 | onOnline: onOnline, |
231 | onOnline: onOnline, | 232 | onIncomingMessage: onIncomingMessage |
232 | onIncomingMessage: onIncomingMessage | 233 | } |
233 | } | 234 | |
234 | 235 | exports.callback = callback; | |
235 | exports.callback = callback; | 236 | exports.setTransport = setTransport; |
236 | exports.setTransport = setTransport; | 237 |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.13.2", | 3 | "version": "1.13.3", |
4 | "description": "SDK for Komodo", | 4 | "description": "SDK for Komodo", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha", | 7 | "test": "mocha", |
8 | "postversion": "git push && git push --tags" | 8 | "postversion": "git push && git push --tags" |
9 | }, | 9 | }, |
10 | "repository": { | 10 | "repository": { |
11 | "type": "git", | 11 | "type": "git", |
12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" | 12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" |
13 | }, | 13 | }, |
14 | "keywords": [ | 14 | "keywords": [ |
15 | "ppob", | 15 | "ppob", |
16 | "payment", | 16 | "payment", |
17 | "komodo" | 17 | "komodo" |
18 | ], | 18 | ], |
19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
20 | "license": "ISC", | 20 | "license": "ISC", |
21 | "dependencies": { | 21 | "dependencies": { |
22 | "basic-auth": "^2.0.0", | 22 | "basic-auth": "^2.0.0", |
23 | "body-parser": "^1.18.2", | 23 | "body-parser": "^1.18.2", |
24 | "express": "^4.16.2", | 24 | "express": "^4.16.2", |
25 | "express-session": "^1.15.6", | 25 | "express-session": "^1.15.6", |
26 | "lru-cache": "^4.1.1", | 26 | "lru-cache": "^4.1.1", |
27 | "moment": "^2.19.1", | 27 | "moment": "^2.19.1", |
28 | "numeral": "^2.0.6", | 28 | "numeral": "^2.0.6", |
29 | "nunjucks": "^3.0.1", | 29 | "nunjucks": "^3.0.1", |
30 | "request": "^2.81.0", | 30 | "request": "^2.81.0", |
31 | "simple-git": "^1.80.1", | 31 | "simple-git": "^1.80.1", |
32 | "strftime": "^0.10.0", | 32 | "strftime": "^0.10.0", |
33 | "uniqid": "^4.1.1", | 33 | "uniqid": "^4.1.1", |
34 | "uuid": "^3.1.0", | 34 | "uuid": "^3.1.0", |
35 | "winston": "^2.3.1", | 35 | "winston": "^2.3.1", |
36 | "winston-circular-buffer": "^1.0.0", | 36 | "winston-circular-buffer": "^1.0.0", |
37 | "winston-daily-rotate-file": "^1.4.6" | 37 | "winston-daily-rotate-file": "^1.4.6" |
38 | } | 38 | } |
39 | } | 39 | } |
40 | 40 |