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 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) { 23 function onIncomingMessage(paramsFromTransport) {
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.balance.indexOf(command) >= 0) { 28 if (config.commands.balance.indexOf(command) >= 0) {
29 executeBalanceCheck(paramsFromTransport); 29 executeBalanceCheck(paramsFromTransport);
30 } 30 }
31 else if (config.commands.price.indexOf(command) >= 0) { 31 else if (config.commands.price.indexOf(command) >= 0) {
32 executePriceCheck(paramsFromTransport); 32 executePriceCheck(paramsFromTransport);
33 } 33 }
34 else { 34 else {
35 executePrepaidBuy(paramsFromTransport); 35 executePrepaidBuy(paramsFromTransport);
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 parseBalanceResponse(body) { 66 function parseBalanceResponse(body) {
67 let result; 67 let result;
68 68
69 try { 69 try {
70 result = JSON.parse(body); 70 result = JSON.parse(body);
71 } 71 }
72 catch(e) { 72 catch(e) {
73 logger.warn('Error JSON parsing', {module_name: module_name, method_name: 'parseBalanceResponse', body: body}) 73 logger.warn('Error JSON parsing', {module_name: module_name, method_name: 'parseBalanceResponse', body: body})
74 result = null; 74 result = null;
75 } 75 }
76 return result; 76 return result;
77 } 77 }
78 78
79 function generateRequestId(req) { 79 function generateRequestId(req) {
80 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d'); 80 return 'AUTO_' + req.product_name + '_' + req.destination + '_' + strftime('%Y%m%d');
81 } 81 }
82 82
83 function executePrepaidBuy(paramsFromTransport) { 83 function executePrepaidBuy(paramsFromTransport) {
84 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/); 84 let tokens = paramsFromTransport.msg.trim().split(/[\., ]+/);
85 85
86 let qs = { 86 let qs = {
87 request_id: tokens[3], 87 request_id: tokens[3],
88 terminal_name: paramsFromTransport.partner.toLowerCase(), 88 terminal_name: paramsFromTransport.partner.toLowerCase(),
89 product_name: tokens[0].toUpperCase(), 89 product_name: tokens[0].toUpperCase(),
90 destination: tokens[1].toUpperCase(), 90 destination: tokens[1].toUpperCase(),
91 password: tokens[2], 91 password: tokens[2],
92 origin: config.origin || config.username, 92 origin: config.origin || config.username,
93 report_port: config.listen_port || '80', 93 report_port: config.listen_port || '80',
94 msg: paramsFromTransport.msg 94 msg: paramsFromTransport.msg
95 } 95 }
96 96
97 if (!qs.do_not_prefix_request_id) { 97 if (!qs.do_not_prefix_request_id) {
98 qs.request_id = generateRequestId(qs); 98 qs.request_id = generateRequestId(qs);
99 if (tokens[3]) { 99 if (tokens[3]) {
100 qs.request_id += '_' + tokens[3];
101 }
100 qs.request_id += '_' + tokens[3]; 102 }
101 } 103
102 } 104 let requestOptions = {
103 105 url: config.core_url + '/prepaid/buy',
104 let requestOptions = { 106 qs: qs
105 url: config.core_url + '/prepaid/buy', 107 }
106 qs: qs 108
107 } 109 requestToCore(requestOptions);
108 110 }
109 requestToCore(requestOptions); 111
110 } 112 function requestToCore(requestOptions, partner) {
111 113 logger.verbose('Requesting service to CORE', requestOptions);
112 function requestToCore(requestOptions, partner) { 114
113 logger.verbose('Requesting service to CORE', requestOptions); 115 request(requestOptions, function(err, res, body) {
114 116 if (err || res.statusCode != 200) {
115 request(requestOptions, function(err, res, body) { 117 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err});
116 if (err || res.statusCode != 200) { 118 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
117 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); 119 return;
118 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 120 }
119 return; 121
120 } 122 let result = parseBalanceResponse(body);
121 123 if (!result || !result.message) {
122 let result = parseBalanceResponse(body); 124 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
123 if (!result || !result.message) { 125 return;
124 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 126 }
125 return; 127
126 } 128 transport.send(requestOptions.qs.terminal_name, result.message);
127 129 })
128 transport.send(requestOptions.qs.terminal_name, result.message); 130 }
129 }) 131
130 } 132 function parseCoreMessage(body) {
131 133 let coreRes;
132 function parseCoreMessage(body) { 134
133 let coreRes; 135 try {
134 136 coreRes = JSON.parse(body)
135 try { 137 }
136 coreRes = JSON.parse(body) 138 catch(err) {
137 } 139 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err});
138 catch(err) { 140 coreRes = null;
139 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); 141 }
140 coreRes = null; 142
141 } 143 return coreRes;
142 144 }
143 return coreRes; 145
144 } 146 function setTransport(_transport) {
145 147 transport = _transport;
146 function setTransport(_transport) { 148 httpResponseServer.setTransport(transport);
147 transport = _transport; 149 }
148 httpResponseServer.setTransport(transport); 150
149 } 151 const callback = {
150 152 onOnline: onOnline,
151 const callback = { 153 onIncomingMessage: onIncomingMessage
152 onOnline: onOnline, 154 }
153 onIncomingMessage: onIncomingMessage 155
154 } 156 exports.callback = callback;
155 157 exports.setTransport = setTransport;
156 exports.callback = callback; 158
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.4.3", 3 "version": "1.4.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 "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