Commit 5a60c2c73f88ba436cf5a85918c75e3a6e2667e4

Authored by Adhidarma Hadiwinoto
1 parent 1fd905ddde
Exists in master

custom and sample

Showing 4 changed files with 23 additions and 12 deletions Inline Diff

examples/kopnus/config.json
File was created 1 {
2 "auto_resend": {
3 "delay_ms": 60000,
4 "max_retry": 1,
5 "max_age_ms": 240000
6 },
7 "sn_pattern": {
8 "pattern": "SN=(.*?)\\.",
9 "match_idx": 1
10 }
11 }
12
examples/kopnus/rc-local.json
File was created 1 {
2 "00": "00",
3 "14": "14",
4 "15": "88",
5 "68": "68",
6 "74": "77",
7 "82": "94",
8 "I8": "14",
9 "I9": "14",
10 "A0": "68"
11 }
12
1 "use strict"; 1 "use strict";
2 2
3 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 3 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
4 4
5 const fs = require('fs'); 5 const fs = require('fs');
6 const url = require('url'); 6 const url = require('url');
7 const https = require('https'); 7 const https = require('https');
8 const xmlrpc = require('xmlrpc'); 8 const xmlrpc = require('xmlrpc');
9 9
10 const config = require('komodo-sdk/config'); 10 const config = require('komodo-sdk/config');
11 const logger = require('komodo-sdk/logger'); 11 const logger = require('komodo-sdk/logger');
12 const matrix = require('komodo-sdk/matrix'); 12 const matrix = require('komodo-sdk/matrix');
13 const pull = require('komodo-sdk/gateway/pull'); 13 const pull = require('komodo-sdk/gateway/pull');
14 14
15 const st24 = require('./st24'); 15 const st24 = require('./st24');
16 16
17 const partnerRc = fs.existsSync(__dirname + '/../rc-local.json') ? require('../rc-local.json') : require('./partner-rc.json'); 17 const partnerRc = fs.existsSync(__dirname + '/../rc-local.json') ? require('../rc-local.json') : require('./partner-rc.json');
18 18
19 if (config.partner.use_sslv3) { 19 if (config.partner.use_sslv3) {
20 https.globalAgent.options.secureProtocol = 'SSLv3_method'; 20 https.globalAgent.options.secureProtocol = 'SSLv3_method';
21 } 21 }
22 22
23 function createXmlRpcClient(endpoint) { 23 function createXmlRpcClient(endpoint) {
24 const partnerUrl = url.parse(endpoint); 24 const partnerUrl = url.parse(endpoint);
25 const clientOptions = { 25 const clientOptions = {
26 host: partnerUrl.hostname, 26 host: partnerUrl.hostname,
27 port: partnerUrl.port, 27 port: partnerUrl.port,
28 path: partnerUrl.pathname 28 path: partnerUrl.pathname
29 }; 29 };
30 30
31 logger.verbose('Creating XML-RPC client using ' + partnerUrl.protocol, clientOptions); 31 logger.verbose('Creating XML-RPC client using ' + partnerUrl.protocol, clientOptions);
32 32
33 return (partnerUrl.protocol === 'https:') ? xmlrpc.createSecureClient(clientOptions) : xmlrpc.createClient(clientOptions); 33 return (partnerUrl.protocol === 'https:') ? xmlrpc.createSecureClient(clientOptions) : xmlrpc.createClient(clientOptions);
34 } 34 }
35 35
36 function buy(task) { 36 function buy(task) {
37 _topUpRequest(task); 37 _topUpRequest(task);
38 } 38 }
39 39
40 function _topUpRequest(task, isAdvice) { 40 function _topUpRequest(task, isAdvice) {
41 const params = { 41 const params = {
42 MSISDN: config.partner.msisdn || config.partner.userid, 42 MSISDN: config.partner.msisdn || config.partner.userid,
43 REQUESTID: task.trx_id, 43 REQUESTID: task.trx_id,
44 PIN: config.partner.pin || config.partner.password, 44 PIN: config.partner.pin || config.partner.password,
45 NOHP: task.destination, 45 NOHP: task.destination,
46 NOM: task.remote_product 46 NOM: task.remote_product
47 }; 47 };
48 48
49 const xmlrpcMethod = 'topUpRequest'; 49 const xmlrpcMethod = 'topUpRequest';
50 logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); 50 logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url});
51 51
52 const client = createXmlRpcClient(config.partner.url); 52 const client = createXmlRpcClient(config.partner.url);
53 client.methodCall(xmlrpcMethod, [ params ], function (err, value) { 53 client.methodCall(xmlrpcMethod, [ params ], function (err, value) {
54 54
55 if (err) { 55 if (err) {
56 56
57 let msg = 'XMLRPC Client Error: ' + err; 57 let msg = 'XMLRPC Client Error: ' + err;
58 let rc = '68'; 58 let rc = '68';
59 59
60 if ( 60 if (
61 !isAdvice && 61 !isAdvice &&
62 ( 62 (
63 err.code === 'ECONNREFUSED' 63 err.code === 'ECONNREFUSED'
64 || err.code === 'EHOSTUNREACH' 64 || err.code === 'EHOSTUNREACH'
65 || (err.code === 'ETIMEDOUT' && err.syscall === "connect") 65 || (err.code === 'ETIMEDOUT' && err.syscall === "connect")
66 || (err.code === 'EPROTO' && err.syscall === "write") 66 || (err.code === 'EPROTO' && err.syscall === "write")
67 ) 67 )
68 ) { 68 ) {
69 rc = '91'; 69 rc = '91';
70 } 70 }
71 71
72 logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); 72 logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err});
73 report({ 73 report({
74 trx_id: task.trx_id, 74 trx_id: task.trx_id,
75 rc: rc, 75 rc: rc,
76 message: 'INTERNAL: ' + msg, 76 message: 'INTERNAL: ' + msg,
77 misc: { 77 misc: {
78 task: task 78 task: task
79 } 79 }
80 }); 80 });
81 81
82 if (rc === '68') { 82 if (rc === '68') {
83 setTimeout( 83 setTimeout(
84 function() { advice(task); }, 84 function() { advice(task); },
85 5 * 60 * 1000 85 5 * 60 * 1000
86 ); 86 );
87 } 87 }
88 88
89 return; 89 return;
90 } 90 }
91 91
92 logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); 92 logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value});
93 matrix.last_topupRequest_ack = value; 93 matrix.last_topupRequest_ack = value;
94 94
95 report({ 95 report({
96 trx_id: task.trx_id, 96 trx_id: task.trx_id,
97 rc: partnerRc[value.RESPONSECODE] || '40', 97 rc: partnerRc[value.RESPONSECODE] || '40',
98 message: value.MESSAGE, 98 message: value.MESSAGE,
99 sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), 99 sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE),
100 amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), 100 amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE),
101 raw: value, 101 raw: value,
102 misc: { 102 misc: {
103 task: task 103 task: task
104 } 104 }
105 }); 105 });
106 }); 106 });
107 } 107 }
108 108
109 function _topUpInquiry(task) { 109 function _topUpInquiry(task) {
110 const params = { 110 const params = {
111 REQUESTID: task.trx_id, 111 REQUESTID: task.trx_id,
112 MSISDN: config.partner.msisdn || config.partner.userid, 112 MSISDN: config.partner.msisdn || config.partner.userid,
113 PIN: config.partner.pin || config.partner.password, 113 PIN: config.partner.pin || config.partner.password,
114 NOHP: task.destination 114 NOHP: task.destination
115 }; 115 };
116 116
117 const xmlrpcMethod = 'topUpInquiry'; 117 const xmlrpcMethod = 'topUpInquiry';
118 logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); 118 logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url});
119 119
120 const client = createXmlRpcClient(config.partner.url); 120 const client = createXmlRpcClient(config.partner.url);
121 client.methodCall(xmlrpcMethod, [ params ], function (err, value) { 121 client.methodCall(xmlrpcMethod, [ params ], function (err, value) {
122 122
123 if (err) { 123 if (err) {
124 124
125 const msg = 'XMLRPC Client Error: ' + err; 125 const msg = 'XMLRPC Client Error: ' + err;
126 126
127 logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); 127 logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err});
128 report({ 128 report({
129 trx_id: task.trx_id, 129 trx_id: task.trx_id,
130 rc: '68', 130 rc: '68',
131 message: 'INTERNAL: ' + msg, 131 message: 'INTERNAL: ' + msg,
132 misc: { 132 misc: {
133 task: task 133 task: task
134 } 134 }
135 }); 135 });
136 136
137 return; 137 return;
138 } 138 }
139 139
140 logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); 140 logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value});
141 //matrix.last_topupRequest_ack = value; 141 //matrix.last_topupRequest_ack = value;
142 142
143 report({ 143 report({
144 trx_id: task.trx_id, 144 trx_id: task.trx_id,
145 rc: partnerRc[value.RESPONSECODE] || '40', 145 rc: partnerRc[value.RESPONSECODE] || '40',
146 message: value.MESSAGE, 146 message: value.MESSAGE,
147 sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), 147 sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE, config.sn_pattern),
148 amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), 148 amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE),
149 raw: value, 149 raw: value,
150 misc: { 150 misc: {
151 task: task 151 task: task
152 } 152 }
153 }); 153 });
154 }); 154 });
155 } 155 }
156 156
157 function advice(task) { 157 function advice(task) {
158 if (config && advice_is_not_allowed) { 158 if (config && advice_is_not_allowed) {
159 return; 159 return;
160 } 160 }
161 161
162 if (config && advice_is_topuprequest) { 162 if (config && advice_is_topuprequest) {
163 _topUpRequest(task, true); 163 _topUpRequest(task, true);
164 } 164 }
165 else { 165 else {
166 _topUpInquiry(task); 166 _topUpInquiry(task);
167 } 167 }
168 } 168 }
169 169
170 function report(data) { 170 function report(data) {
171 if (!data) { 171 if (!data) {
172 return; 172 return;
173 } 173 }
174 174
175 matrix.last_report_to_core = data; 175 matrix.last_report_to_core = data;
176 pull.report(data); 176 pull.report(data);
177 } 177 }
178 178
179 exports.buy = buy; 179 exports.buy = buy;
180 exports.advice = advice; 180 exports.advice = advice;
181 exports.report = report; 181 exports.report = report;
182 182
rc-local.sample.kopnus.json
1 { File was deleted
2 "00": "00",
3 "14": "14",
4 "15": "88",
5 "68": "68",
6 "74": "77",
7 "82": "94",
8 "I8": "14",
9 "I9": "14",
10 "A0": "68"
11 }
12 1 {