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: null,
87 request_id: null, 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 qs.request_id = generateRequestId(qs);
97 qs.request_id = generateRequestId(qs); 98 if (tokens[3]) {
98 if (tokens[3]) { 99 qs.request_id += '_' + tokens[3];
99 qs.request_id += '_' + tokens[3]; 100 }
100 } 101
101 102 let requestOptions = {
102 let requestOptions = { 103 url: config.core_url + '/prepaid/buy',
103 url: config.core_url + '/prepaid/buy', 104 qs: qs
104 qs: qs 105 }
105 } 106
106 107 requestToCore(requestOptions);
107 requestToCore(requestOptions); 108 }
108 } 109
109 110 function requestToCore(requestOptions, partner) {
110 function requestToCore(requestOptions, partner) { 111 logger.verbose('Requesting service to CORE', requestOptions);
111 logger.verbose('Requesting service to CORE', requestOptions); 112
112 113 request(requestOptions, function(err, res, body) {
113 request(requestOptions, function(err, res, body) { 114 if (err || res.statusCode != 200) {
114 if (err || res.statusCode != 200) { 115 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err});
115 logger.warn('Error requesting to CORE', {module_name: module_name, method_name: 'requestToCore', requestOptions: requestOptions, err: err}); 116 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
116 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 117 return;
117 return; 118 }
118 } 119
119 120 let result = parseBalanceResponse(body);
120 let result = parseBalanceResponse(body); 121 if (!result || !result.message) {
121 if (!result || !result.message) { 122 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR');
122 transport.send(requestOptions.qs.terminal_name, 'INTERNAL ERROR'); 123 return;
123 return; 124 }
124 } 125
125 126 transport.send(requestOptions.qs.terminal_name, result.message);
126 transport.send(requestOptions.qs.terminal_name, result.message); 127 })
127 }) 128 }
128 } 129
129 130 function parseCoreMessage(body) {
130 function parseCoreMessage(body) { 131 let coreRes;
132
131 let coreRes; 133 try {
132 134 coreRes = JSON.parse(body)
133 try { 135 }
134 coreRes = JSON.parse(body) 136 catch(err) {
135 } 137 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err});
136 catch(err) { 138 coreRes = null;
137 logger.warn('Exception on parsing CORE response as JSON', {body: body, err: err}); 139 }
140
138 coreRes = null; 141 return coreRes;
139 } 142 }
140 143
144 function setTransport(_transport) {
145 transport = _transport;
146 httpResponseServer.setTransport(transport);
147 }
148
141 return coreRes; 149 const callback = {
142 } 150 onOnline: onOnline,
143 151 onIncomingMessage: onIncomingMessage
144 function setTransport(_transport) { 152 }
145 transport = _transport; 153
146 httpResponseServer.setTransport(transport);
147 }
148
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.3.0", 3 "version": "1.4.0",
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 "strftime": "^0.10.0", 22 "strftime": "^0.10.0",
23 "winston": "^2.3.1", 23 "winston": "^2.3.1",
24 "winston-daily-rotate-file": "^1.4.6" 24 "winston-daily-rotate-file": "^1.4.6"
25 } 25 }
26 } 26 }
27 27