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