Compare View

switch
from
...
to
 
Commits (4)

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 path = require('path'); 7 const path = require('path');
8 const request = require('request'); 8 const request = require('request');
9 const strftime = require('strftime'); 9 const strftime = require('strftime');
10 const config = require('../../config'); 10 const config = require('../../config');
11 const logger = require('../../logger'); 11 const logger = require('../../logger');
12 const httpResponseServer = require('../http-response-server'); 12 const httpResponseServer = require('../http-response-server');
13 13
14 const module_name = path.basename(__filename); 14 const module_name = path.basename(__filename);
15 15
16 let transport; 16 let transport;
17 17
18 function onOnline(params) { 18 function onOnline(params) {
19 logger.info('CENTER is ONLINE, ready to communicate'); 19 logger.info('CENTER is ONLINE, ready to communicate');
20 20
21 } 21 }
22 22
23 function onIncomingMessage(paramsFromTransport, cb) { 23 function onIncomingMessage(paramsFromTransport, cb) {
24 logger.verbose('Reporting message to CORE') 24 logger.verbose('Reporting message to CORE')
25 25
26 const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase(); 26 const command = paramsFromTransport.msg.split(/[\., ]+/)[0].toUpperCase();
27 27
28 if (config.commands && config.commands.balance.indexOf(command) >= 0) { 28 if (config.commands && config.commands.balance.indexOf(command) >= 0) {
29 executeBalanceCheck(paramsFromTransport, cb); 29 executeBalanceCheck(paramsFromTransport, cb);
30 } 30 }
31 else if (config.commands && config.commands.price.indexOf(command) >= 0) { 31 else if (config.commands && config.commands.price.indexOf(command) >= 0) {
32 executePriceCheck(paramsFromTransport, cb); 32 executePriceCheck(paramsFromTransport, cb);
33 } 33 }
34 else { 34 else {
35 executePrepaidBuy(paramsFromTransport, cb); 35 executePrepaidBuy(paramsFromTransport, cb);
36 } 36 }
37 } 37 }
38 38
39 function executeBalanceCheck(paramsFromTransport) { 39 function executeBalanceCheck(paramsFromTransport) {
40 const terminal_name = paramsFromTransport.partner.toLowerCase(); 40 const terminal_name = paramsFromTransport.partner.toLowerCase();
41 const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1]; 41 const password = paramsFromTransport.msg.trim().split(/[\., ]+/)[1];
42 42
43 const requestOptions = { 43 const requestOptions = {
44 url: config.core_url + '/services/balance', 44 url: config.core_url + '/services/balance',
45 qs: { 45 qs: {
46 terminal_name: terminal_name, 46 terminal_name: terminal_name,
47 password: password 47 password: password
48 } 48 }
49 } 49 }
50 50
51 requestToCore(requestOptions); 51 requestToCore(requestOptions);
52 } 52 }
53 53
54 function executePriceCheck(paramsFromTransport) { 54 function executePriceCheck(paramsFromTransport) {
55 const requestOptions = { 55 const requestOptions = {
56 url: config.core_url + '/services/pricelist', 56 url: config.core_url + '/services/pricelist',
57 qs: { 57 qs: {
58 terminal_name: paramsFromTransport.partner.toLowerCase(), 58 terminal_name: paramsFromTransport.partner.toLowerCase(),
59 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1] 59 keyword: paramsFromTransport.msg.trim().split(/[\., ]+/)[1]
60 } 60 }
61 } 61 }
62 62
63 requestToCore(requestOptions); 63 requestToCore(requestOptions);
64 } 64 }
65 65
66 function parseCoreMessage(body) { 66 function parseCoreMessage(body) {
67 let coreRes; 67 let coreRes;
68 68
69 try { 69 try {
70 coreRes = JSON.parse(body) 70 coreRes = JSON.parse(body)
71 } 71 }
72 catch(err) { 72 catch(err) {
73 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); 73 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err});
74 coreRes = null; 74 coreRes = null;
75 } 75 }
76 76
77 return coreRes; 77 return coreRes;
78 } 78 }
79 79
80 function generateRequestId(req) { 80 function generateRequestId(req) {
81 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); 81 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d');
82 } 82 }
83 83
84 function executePrepaidBuy(paramsFromTransport, cb) { 84 function executePrepaidBuy(paramsFromTransport, cb) {
85 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 85 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
86 86
87 let qs = { 87 let qs = {
88 request_id: tokens[3], 88 request_id: tokens[3],
89 terminal_name: paramsFromTransport.partner.toLowerCase(), 89 terminal_name: paramsFromTransport.partner.toLowerCase(),
90 product_name: tokens[0].toUpperCase(), 90 product_name: tokens[0].toUpperCase(),
91 destination: tokens[1].toUpperCase(), 91 destination: tokens[1].toUpperCase(),
92 password: tokens[2], 92 password: tokens[2],
93 origin: config.origin || config.username, 93 origin: config.origin || config.username,
94 report_port: config.listen_port || '80', 94 report_port: config.listen_port || '80',
95 msg: paramsFromTransport.msg 95 msg: paramsFromTransport.msg
96 } 96 }
97 97
98 if (!config.do_not_prefix_request_id) { 98 if (!config.do_not_prefix_request_id) {
99 qs.request_id = generateRequestId(qs); 99 qs.request_id = generateRequestId(qs);
100 if (tokens[3]) { 100 if (tokens[3]) {
101 qs.request_id += '_' + tokens[3]; 101 qs.request_id += '_' + tokens[3];
102 } 102 }
103 } 103 }
104 104
105 let requestOptions = { 105 let requestOptions = {
106 url: config.core_url + '/prepaid/buy', 106 url: config.core_url + '/prepaid/buy',
107 qs: qs 107 qs: qs
108 } 108 }
109 109
110 requestToCore(requestOptions, cb); 110 requestToCore(requestOptions, cb);
111 } 111 }
112 112
113 function requestToCore(requestOptions, cb) { 113 function requestToCore(requestOptions, cb) {
114 logger.verbose('Requesting service to CORE', requestOptions); 114 logger.verbose('Requesting service to CORE', requestOptions);
115 115
116 request(requestOptions, function(err, res, body) { 116 request(requestOptions, function(err, res, body) {
117 if (err || res.statusCode != 200) { 117 if (err || res.statusCode != 200) {
118 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); 118 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err});
119 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 119 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
120 if (cb) { cb(null, {msg: 'INTERNAL ERROR'}); }
120 if (cb) { cb(null, {msg: 'INTERNAL ERROR'}); } 121 return;
121 return; 122 }
122 } 123
123 124 let result = parseCoreMessage(body);
124 let result = parseCoreMessage(body); 125 if (!result || !result.message) {
125 if (!result || !result.message) { 126 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
127 if (cb) { cb(null, {msg: 'INTERNAL ERROR'}); }
126 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 128 return;
127 if (cb) { cb(null, {msg: 'INTERNAL ERROR'}); } 129 }
128 return; 130
129 } 131 transport.send(requestOptions.qs.terminal_name, result.message);
132 if (cb) {
133 cb(null, result);
134 }
130 135 })
131 transport.send(requestOptions.qs.terminal_name, result.message); 136 }
132 if (cb) { 137
133 cb(null, result); 138 function setTransport(_transport) {
134 } 139 transport = _transport;
135 }) 140 httpResponseServer.setTransport(transport);
136 } 141 }
137 142
138 function setTransport(_transport) { 143 const callback = {
139 transport = _transport; 144 onOnline: onOnline,
140 httpResponseServer.setTransport(transport); 145 onIncomingMessage: onIncomingMessage
141 } 146 }
142 147
143 const callback = { 148 exports.callback = callback;
144 onOnline: onOnline, 149 exports.setTransport = setTransport;
145 onIncomingMessage: onIncomingMessage 150
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.4.6", 3 "version": "1.4.7",
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 "request": "^2.81.0", 22 "request": "^2.81.0",
23 "strftime": "^0.10.0", 23 "strftime": "^0.10.0",
24 "winston": "^2.3.1", 24 "winston": "^2.3.1",
25 "winston-daily-rotate-file": "^1.4.6" 25 "winston-daily-rotate-file": "^1.4.6"
26 } 26 }
27 } 27 }
28 28