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