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 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 executePostpaidPay(paramsFromTransport, cb); 41 executePostpaidPay(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 msg: paramsFromTransport.msg 57 msg: paramsFromTransport.msg
58 } 58 }
59 } 59 }
60 60
61 requestToCore(requestOptions); 61 requestToCore(requestOptions);
62 } 62 }
63 63
64 function executePriceCheck(paramsFromTransport) { 64 function executePriceCheck(paramsFromTransport) {
65 const requestOptions = { 65 const requestOptions = {
66 url: config.core_url + '/services/pricelist', 66 url: config.core_url + '/services/pricelist',
67 qs: { 67 qs: {
68 terminal_name: paramsFromTransport.partner.toLowerCase(), 68 terminal_name: paramsFromTransport.partner.toLowerCase(),
69 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1], 69 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1],
70 password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2], 70 password: paramsFromTransport.msg.trim().split(/[\., ]+/)[2],
71 postpaid: 0, 71 postpaid: 0,
72 msg: paramsFromTransport.msg 72 msg: paramsFromTransport.msg
73 } 73 }
74 } 74 }
75 75
76 requestToCore(requestOptions); 76 requestToCore(requestOptions);
77 } 77 }
78 78
79 function parseCoreMessage(body) { 79 function parseCoreMessage(body) {
80 let coreRes; 80 let coreRes;
81 81
82 try { 82 try {
83 coreRes = JSON.parse(body) 83 coreRes = JSON.parse(body)
84 } 84 }
85 catch(err) { 85 catch(err) {
86 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); 86 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err});
87 coreRes = null; 87 coreRes = null;
88 } 88 }
89 89
90 return coreRes; 90 return coreRes;
91 } 91 }
92 92
93 function generateRequestId(req) { 93 function generateRequestId(req) {
94 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); 94 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d');
95 } 95 }
96 96
97 function executePrepaidBuy(paramsFromTransport, cb) { 97 function executePrepaidBuy(paramsFromTransport, cb) {
98 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 98 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
99 99
100 let qs = { 100 let qs = {
101 request_id: tokens[3], 101 request_id: tokens[3],
102 terminal_name: paramsFromTransport.partner.toLowerCase(), 102 terminal_name: paramsFromTransport.partner.toLowerCase(),
103 product_name: tokens[0].toUpperCase(), 103 product_name: tokens[0].toUpperCase(),
104 destination: tokens[1].toUpperCase(), 104 destination: tokens[1].toUpperCase(),
105 password: tokens[2], 105 password: tokens[2],
106 origin: config.origin || config.username, 106 origin: config.origin || config.username,
107 report_port: config.listen_port || '80', 107 report_port: config.listen_port || '80',
108 msg: paramsFromTransport.msg, 108 msg: paramsFromTransport.msg,
109 reverse_url: paramsFromTransport.reverse_url 109 reverse_url: paramsFromTransport.reverse_url
110 } 110 }
111 111
112 if (!config.do_not_prefix_request_id) { 112 if (!config.do_not_prefix_request_id) {
113 qs.request_id = generateRequestId(qs); 113 qs.request_id = generateRequestId(qs);
114 if (tokens[3]) { 114 if (tokens[3]) {
115 qs.request_id += '_' + tokens[3]; 115 qs.request_id += '_' + tokens[3];
116 } 116 }
117 } 117 }
118 118
119 let requestOptions = { 119 let requestOptions = {
120 url: config.core_url + '/prepaid/buy', 120 url: config.core_url + '/prepaid/buy',
121 qs: qs 121 qs: qs
122 } 122 }
123 123
124 requestToCore(requestOptions, cb); 124 requestToCore(requestOptions, cb);
125 } 125 }
126 126
127 function executePostpaidInquiry(paramsFromTransport, cb) { 127 function executePostpaidInquiry(paramsFromTransport, cb) {
128 // PAY.PLN.1234567890.PIN 128 // PAY.PLN.1234567890.PIN
129 129
130 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 130 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
131 131
132 let qs = { 132 let qs = {
133 request_id: tokens[4], 133 request_id: tokens[4],
134 terminal_name: paramsFromTransport.partner.toLowerCase(), 134 terminal_name: paramsFromTransport.partner.toLowerCase(),
135 product_name: tokens[1].toUpperCase(), 135 product_name: tokens[1].toUpperCase(),
136 destination: tokens[2].toUpperCase(), 136 destination: tokens[2].toUpperCase(),
137 password: tokens[3], 137 password: tokens[3],
138 origin: config.origin || config.username, 138 origin: config.origin || config.username,
139 report_port: config.listen_port || '80', 139 report_port: config.listen_port || '80',
140 msg: paramsFromTransport.msg, 140 msg: paramsFromTransport.msg,
141 reverse_url: paramsFromTransport.reverse_url 141 reverse_url: paramsFromTransport.reverse_url
142 } 142 }
143 143
144 if (!config.do_not_prefix_request_id) { 144 if (!config.do_not_prefix_request_id) {
145 qs.request_id = generateRequestId(qs) + '_INQ'; 145 qs.request_id = generateRequestId(qs) + '_INQ';
146 if (tokens[3]) { 146 if (tokens[3]) {
147 qs.request_id += '_' + tokens[3]; 147 qs.request_id += '_' + tokens[3];
148 } 148 }
149 } 149 }
150 150
151 let requestOptions = { 151 let requestOptions = {
152 url: config.core_url + '/postpaid/inquiry', 152 url: config.core_url + '/postpaid/inquiry',
153 qs: qs 153 qs: qs
154 } 154 }
155 155
156 requestToCore(requestOptions, cb); 156 requestToCore(requestOptions, cb);
157 } 157 }
158 158
159 function executePostpaidPay(paramsFromTransport, cb) { 159 function executePostpaidPay(paramsFromTransport, cb) {
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/pay', 182 url: config.core_url + '/postpaid/pay',
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 let msg = "INTERNAL ERROR"; 195 let msg = "INTERNAL ERROR";
196 if (requestOptions.qs.msg) { 196 if (requestOptions.qs.msg) {
197 msg = requestOptions.qs.msg + ": " + msg; 197 msg = requestOptions.qs.msg + ": " + msg;
198 } 198 }
199 199
200 if (cb) { 200 if (cb) {
201 cb(null, {msg: msg}); 201 cb(null, {msg: msg});
202 } 202 }
203 else if (transport.send) { 203 else if (transport.send) {
204 transport.send(requestOptions.qs.terminal_name, msg); 204 transport.send(requestOptions.qs.terminal_name, msg);
205 } 205 }
206 return; 206 return;
207 } 207 }
208 208
209 let result = parseCoreMessage(body); 209 let result = parseCoreMessage(body);
210 if (!result || !result.message) { 210 if (!result || !result.message) {
211 let msg = "INTERNAL ERROR"; 211 let msg = "INTERNAL ERROR";
212 if (requestOptions.qs.msg) { 212 if (requestOptions.qs.msg) {
213 msg = requestOptions.qs.msg + ": " + msg; 213 msg = requestOptions.qs.msg + ": " + msg;
214 } 214 }
215 215
216 if (cb) { 216 if (cb) {
217 cb(null, {msg: msg}); 217 cb(null, {msg: msg});
218 } 218 }
219 else if (transport.send) { 219 else if (transport.send) {
220 transport.send(requestOptions.qs.terminal_name, msg); 220 transport.send(requestOptions.qs.terminal_name, msg);
221 } 221 }
222 return; 222 return;
223 } 223 }
224 224
225 if (cb) { 225 if (cb) {
226 cb(null, result); 226 cb(null, result);
227 } 227 }
228 else if (transport.send) { 228 else if (transport.send) {
229 transport.send(requestOptions.qs.terminal_name, result.message); 229 transport.send(requestOptions.qs.terminal_name, result.message);
230 230
231 } 231 }
232 }) 232 })
233 } 233 }
234 234
235 function setTransport(_transport) { 235 function setTransport(_transport) {
236 transport = _transport; 236 transport = _transport;
237 httpResponseServer.setTransport(transport); 237 httpResponseServer.setTransport(transport);
238 } 238 }
239 239
240 const callback = { 240 const callback = {
241 onOnline: onOnline, 241 onOnline: onOnline,
242 onIncomingMessage: onIncomingMessage 242 onIncomingMessage: onIncomingMessage
243 } 243 }
244 244
245 exports.callback = callback; 245 exports.callback = callback;
246 exports.setTransport = setTransport; 246 exports.setTransport = setTransport;
247 247
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.13.7", 3 "version": "1.13.8",
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