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