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