Compare View

switch
from
...
to
 
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 if (config.origin) { 19 if (config.origin) {
20 process.title = "KOMODO@" + config.origin.replace(/\W/g, '-').toUpperCase();
21 }
20 process.title = "KOMODO@" + config.origin.replace(/\W/g, '-').toUpperCase(); 22
21 } 23 heartbeat.setModuleType('center')
22 24
23 heartbeat.setModuleType('center') 25 function onOnline(params) {
24 26 logger.info('CENTER is ONLINE, ready to communicate');
25 function onOnline(params) { 27 }
26 logger.info('CENTER is ONLINE, ready to communicate'); 28
27 } 29 function onIncomingMessage(paramsFromTransport, cb) {
28 30 logger.verbose('Reporting message to CORE')
29 function onIncomingMessage(paramsFromTransport, cb) { 31
30 logger.verbose('Reporting message to CORE') 32 const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase();
31 33
32 const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase(); 34 if (config.commands && config.commands.balance && config.commands.balance.indexOf(command) >= 0) {
33 35 executeBalanceCheck(paramsFromTransport, cb);
34 if (config.commands && config.commands.balance && config.commands.balance.indexOf(command) >= 0) { 36 }
35 executeBalanceCheck(paramsFromTransport, cb); 37 else if (config.commands && config.commands.price && config.commands.price.indexOf(command) >= 0) {
36 } 38 executePriceCheck(paramsFromTransport, cb);
37 else if (config.commands && config.commands.price && config.commands.price.indexOf(command) >= 0) { 39 }
38 executePriceCheck(paramsFromTransport, cb); 40 else if (config.commands && config.commands.postpaid_inquiry && config.commands.postpaid_inquiry.indexOf(command) >= 0) {
39 } 41 executePostpaidInquiry(paramsFromTransport, cb);
40 else if (config.commands && config.commands.postpaid_inquiry && config.commands.postpaid_inquiry.indexOf(command) >= 0) { 42
41 executePostpaidInquiry(paramsFromTransport, cb); 43 }
42 44 else if (config.commands && config.commands.postpaid_pay && config.commands.postpaid_pay.indexOf(command) >= 0) {
43 } 45 executePostpaidPay(paramsFromTransport, cb);
44 else if (config.commands && config.commands.postpaid_pay && config.commands.postpaid_pay.indexOf(command) >= 0) { 46 }
45 executePostpaidPay(paramsFromTransport, cb); 47 else {
46 } 48 executePrepaidBuy(paramsFromTransport, cb);
47 else { 49 }
48 executePrepaidBuy(paramsFromTransport, cb); 50 }
49 } 51
50 } 52 function executeBalanceCheck(paramsFromTransport) {
51 53 const terminal_name = paramsFromTransport.partner.toLowerCase();
52 function executeBalanceCheck(paramsFromTransport) { 54 const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1];
53 const terminal_name = paramsFromTransport.partner.toLowerCase(); 55
54 const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1]; 56 const requestOptions = {
55 57 url: config.core_url + '/services/balance',
56 const requestOptions = { 58 qs: {
57 url: config.core_url + '/services/balance', 59 terminal_name: terminal_name,
58 qs: { 60 password: password,
59 terminal_name: terminal_name, 61 msg: paramsFromTransport.msg
60 password: password, 62 }
61 msg: paramsFromTransport.msg 63 }
62 } 64
63 } 65 requestToCore(requestOptions);
64 66 }
65 requestToCore(requestOptions); 67
66 } 68 function executePriceCheck(paramsFromTransport) {
67 69 const requestOptions = {
68 function executePriceCheck(paramsFromTransport) { 70 url: config.core_url + '/services/pricelist',
69 const requestOptions = { 71 qs: {
70 url: config.core_url + '/services/pricelist', 72 terminal_name: paramsFromTransport.partner.toLowerCase(),
71 qs: { 73 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1],
72 terminal_name: paramsFromTransport.partner.toLowerCase(), 74 password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2],
73 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1], 75 postpaid: 0,
74 password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2], 76 msg: paramsFromTransport.msg
75 postpaid: 0, 77 }
76 msg: paramsFromTransport.msg 78 }
77 } 79
78 } 80 requestToCore(requestOptions);
79 81 }
80 requestToCore(requestOptions); 82
81 } 83 function parseCoreMessage(body) {
82 84 let coreRes;
83 function parseCoreMessage(body) { 85
84 let coreRes; 86 try {
85 87 coreRes = JSON.parse(body)
86 try { 88 }
87 coreRes = JSON.parse(body) 89 catch(err) {
88 } 90 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err});
89 catch(err) { 91 coreRes = null;
90 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); 92 }
91 coreRes = null; 93
92 } 94 return coreRes;
93 95 }
94 return coreRes; 96
95 } 97 function generateRequestId(req) {
96 98 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d');
97 function generateRequestId(req) { 99 }
98 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); 100
99 } 101 function executePrepaidBuy(paramsFromTransport, cb) {
100 102 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
101 function executePrepaidBuy(paramsFromTransport, cb) { 103
102 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 104 let qs = {
103 105 request_id: tokens[3],
104 let qs = { 106 terminal_name: paramsFromTransport.partner.toLowerCase(),
105 request_id: tokens[3], 107 product_name: tokens[0].toUpperCase(),
106 terminal_name: paramsFromTransport.partner.toLowerCase(), 108 destination: tokens[1].toUpperCase(),
107 product_name: tokens[0].toUpperCase(), 109 password: tokens[2],
108 destination: tokens[1].toUpperCase(), 110 origin: config.origin || config.username,
109 password: tokens[2], 111 report_port: config.listen_port || '80',
110 origin: config.origin || config.username, 112 msg: paramsFromTransport.msg,
111 report_port: config.listen_port || '80', 113 reverse_url: paramsFromTransport.reverse_url
112 msg: paramsFromTransport.msg, 114 }
113 reverse_url: paramsFromTransport.reverse_url 115
114 } 116 if (!config.do_not_prefix_request_id) {
115 117 qs.request_id = generateRequestId(qs);
116 if (!config.do_not_prefix_request_id) { 118 if (tokens[3]) {
117 qs.request_id = generateRequestId(qs); 119 qs.request_id += '_' + tokens[3];
118 if (tokens[3]) { 120 }
119 qs.request_id += '_' + tokens[3]; 121 }
120 } 122
121 } 123 let requestOptions = {
122 124 url: config.core_url + '/prepaid/buy',
123 let requestOptions = { 125 qs: qs
124 url: config.core_url + '/prepaid/buy', 126 }
125 qs: qs 127
126 } 128 requestToCore(requestOptions, cb);
127 129 }
128 requestToCore(requestOptions, cb); 130
129 } 131 function executePostpaidInquiry(paramsFromTransport, cb) {
130 132 // PAY.PLN.1234567890.PIN
131 function executePostpaidInquiry(paramsFromTransport, cb) { 133
132 // PAY.PLN.1234567890.PIN 134 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
133 135
134 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 136 let qs = {
135 137 request_id: tokens[4],
136 let qs = { 138 terminal_name: paramsFromTransport.partner.toLowerCase(),
137 request_id: tokens[4], 139 product_name: tokens[1].toUpperCase(),
138 terminal_name: paramsFromTransport.partner.toLowerCase(), 140 destination: tokens[2].toUpperCase(),
139 product_name: tokens[1].toUpperCase(), 141 password: tokens[3],
140 destination: tokens[2].toUpperCase(), 142 origin: config.origin || config.username,
141 password: tokens[3], 143 report_port: config.listen_port || '80',
142 origin: config.origin || config.username, 144 msg: paramsFromTransport.msg,
143 report_port: config.listen_port || '80', 145 reverse_url: paramsFromTransport.reverse_url
144 msg: paramsFromTransport.msg, 146 }
145 reverse_url: paramsFromTransport.reverse_url 147
146 } 148 if (!config.do_not_prefix_request_id) {
147 149 qs.request_id = generateRequestId(qs) + '_INQ';
148 if (!config.do_not_prefix_request_id) { 150 if (tokens[4]) {
149 qs.request_id = generateRequestId(qs) + '_INQ'; 151 qs.request_id += '_' + tokens[4];
150 if (tokens[4]) { 152 }
151 qs.request_id += '_' + tokens[4]; 153 }
152 } 154
153 } 155 let requestOptions = {
154 156 url: config.core_url + '/postpaid/inquiry',
155 let requestOptions = { 157 qs: qs
156 url: config.core_url + '/postpaid/inquiry', 158 }
157 qs: qs 159
158 } 160 requestToCore(requestOptions, cb);
159 161 }
160 requestToCore(requestOptions, cb); 162
161 } 163 function executePostpaidPay(paramsFromTransport, cb) {
162 164 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
163 function executePostpaidPay(paramsFromTransport, cb) { 165
164 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 166 let qs = {
165 167 request_id: tokens[4],
166 let qs = { 168 terminal_name: paramsFromTransport.partner.toLowerCase(),
167 request_id: tokens[4], 169 product_name: tokens[1].toUpperCase(),
168 terminal_name: paramsFromTransport.partner.toLowerCase(), 170 destination: tokens[2].toUpperCase(),
169 product_name: tokens[1].toUpperCase(), 171 password: tokens[3],
170 destination: tokens[2].toUpperCase(), 172 origin: config.origin || config.username,
171 password: tokens[3], 173 report_port: config.listen_port || '80',
172 origin: config.origin || config.username, 174 msg: paramsFromTransport.msg,
173 report_port: config.listen_port || '80', 175 reverse_url: paramsFromTransport.reverse_url
174 msg: paramsFromTransport.msg, 176 }
175 reverse_url: paramsFromTransport.reverse_url 177
176 } 178 if (!config.do_not_prefix_request_id) {
177 179 qs.request_id = generateRequestId(qs);
178 if (!config.do_not_prefix_request_id) { 180 if (tokens[4]) {
179 qs.request_id = generateRequestId(qs); 181 qs.request_id += '_' + tokens[4];
180 if (tokens[4]) { 182 }
181 qs.request_id += '_' + tokens[4]; 183 }
182 } 184
183 } 185 let requestOptions = {
184 186 url: config.core_url + '/postpaid/pay',
185 let requestOptions = { 187 qs: qs
186 url: config.core_url + '/postpaid/pay', 188 }
187 qs: qs 189
188 } 190 requestToCore(requestOptions, cb);
189 191 }
190 requestToCore(requestOptions, cb); 192
191 } 193 function requestToCore(requestOptions, cb) {
192 194 logger.verbose('Requesting service to CORE', requestOptions);
193 function requestToCore(requestOptions, cb) { 195
194 logger.verbose('Requesting service to CORE', requestOptions); 196 request(requestOptions, function(err, res, body) {
195 197 if (err || res.statusCode != 200) {
196 request(requestOptions, function(err, res, body) { 198 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err});
197 if (err || res.statusCode != 200) { 199 let msg = "INTERNAL ERROR";
198 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); 200 if (requestOptions.qs.msg) {
199 let msg = "INTERNAL ERROR"; 201 msg = requestOptions.qs.msg + ": " + msg;
200 if (requestOptions.qs.msg) { 202 }
201 msg = requestOptions.qs.msg + ": " + msg; 203
202 } 204 if (cb) {
203 205 cb(null, {msg: msg});
204 if (cb) { 206 }
205 cb(null, {msg: msg}); 207 else if (transport.send) {
206 } 208 transport.send(requestOptions.qs.terminal_name, msg);
207 else if (transport.send) { 209 }
208 transport.send(requestOptions.qs.terminal_name, msg); 210 return;
209 } 211 }
210 return; 212
211 } 213 let result = parseCoreMessage(body);
212 214 if (!result || !result.message) {
213 let result = parseCoreMessage(body); 215 let msg = "INTERNAL ERROR";
214 if (!result || !result.message) { 216 if (requestOptions.qs.msg) {
215 let msg = "INTERNAL ERROR"; 217 msg = requestOptions.qs.msg + ": " + msg;
216 if (requestOptions.qs.msg) { 218 }
217 msg = requestOptions.qs.msg + ": " + msg; 219
218 } 220 if (cb) {
219 221 cb(null, {msg: msg});
220 if (cb) { 222 }
221 cb(null, {msg: msg}); 223 else if (transport.send) {
222 } 224 transport.send(requestOptions.qs.terminal_name, msg);
223 else if (transport.send) { 225 }
224 transport.send(requestOptions.qs.terminal_name, msg); 226 return;
225 } 227 }
226 return; 228
227 } 229 if (cb) {
228 230 cb(null, result);
229 if (cb) { 231 }
230 cb(null, result); 232 else if (transport.send) {
231 } 233 transport.send(requestOptions.qs.terminal_name, result.message);
232 else if (transport.send) { 234
233 transport.send(requestOptions.qs.terminal_name, result.message); 235 }
234 236 })
235 } 237 }
236 }) 238
237 } 239 function setTransport(_transport) {
238 240 transport = _transport;
239 function setTransport(_transport) { 241 httpResponseServer.setTransport(transport);
240 transport = _transport; 242 }
241 httpResponseServer.setTransport(transport); 243
242 } 244 const callback = {
243 245 onOnline: onOnline,
244 const callback = { 246 onIncomingMessage: onIncomingMessage
245 onOnline: onOnline, 247 }
246 onIncomingMessage: onIncomingMessage 248
247 } 249 exports.callback = callback;
248 250 exports.setTransport = setTransport;
249 exports.callback = callback; 251
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.19.2", 3 "version": "1.19.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.3", 24 "express": "^4.16.3",
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 "macaddress": "^0.2.8", 27 "macaddress": "^0.2.8",
28 "moment": "^2.19.1", 28 "moment": "^2.19.1",
29 "node-machine-id": "^1.1.10", 29 "node-machine-id": "^1.1.10",
30 "node-natural-sort": "^0.8.6", 30 "node-natural-sort": "^0.8.6",
31 "numeral": "^2.0.6", 31 "numeral": "^2.0.6",
32 "nunjucks": "^3.0.1", 32 "nunjucks": "^3.0.1",
33 "redis": "^2.8.0", 33 "redis": "^2.8.0",
34 "request": "^2.81.0", 34 "request": "^2.81.0",
35 "sha1": "^1.1.1", 35 "sha1": "^1.1.1",
36 "simple-git": "^1.80.1", 36 "simple-git": "^1.80.1",
37 "strftime": "^0.10.0", 37 "strftime": "^0.10.0",
38 "uniqid": "^4.1.1", 38 "uniqid": "^4.1.1",
39 "uuid": "^3.1.0", 39 "uuid": "^3.1.0",
40 "winston": "^2.3.1", 40 "winston": "^2.3.1",
41 "winston-circular-buffer": "^1.0.0", 41 "winston-circular-buffer": "^1.0.0",
42 "winston-daily-rotate-file": "^1.4.6" 42 "winston-daily-rotate-file": "^1.4.6"
43 } 43 }
44 } 44 }
45 45