Commit 0c6ed7320b04b88a97d4cf4ed62af115853406be
1 parent
faf6218953
Exists in
master
auto advice on error
Showing 1 changed file with 12 additions and 0 deletions Inline Diff
lib/partner.js
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 url = require('url'); | 5 | const url = require('url'); |
6 | const https = require('https'); | 6 | const https = require('https'); |
7 | const xmlrpc = require('xmlrpc'); | 7 | const xmlrpc = require('xmlrpc'); |
8 | 8 | ||
9 | const config = require('komodo-sdk/config'); | 9 | const config = require('komodo-sdk/config'); |
10 | const logger = require('komodo-sdk/logger'); | 10 | const logger = require('komodo-sdk/logger'); |
11 | const matrix = require('komodo-sdk/matrix'); | 11 | const matrix = require('komodo-sdk/matrix'); |
12 | const pull = require('komodo-sdk/gateway/pull'); | 12 | const pull = require('komodo-sdk/gateway/pull'); |
13 | 13 | ||
14 | const st24 = require('./st24'); | 14 | const st24 = require('./st24'); |
15 | const partnerRc = require('./partner-rc.json'); | 15 | const partnerRc = require('./partner-rc.json'); |
16 | 16 | ||
17 | if (config.partner.use_sslv3) { | 17 | if (config.partner.use_sslv3) { |
18 | https.globalAgent.options.secureProtocol = 'SSLv3_method'; | 18 | https.globalAgent.options.secureProtocol = 'SSLv3_method'; |
19 | } | 19 | } |
20 | 20 | ||
21 | function createXmlRpcClient(endpoint) { | 21 | function createXmlRpcClient(endpoint) { |
22 | const partnerUrl = url.parse(endpoint); | 22 | const partnerUrl = url.parse(endpoint); |
23 | const clientOptions = { | 23 | const clientOptions = { |
24 | host: partnerUrl.hostname, | 24 | host: partnerUrl.hostname, |
25 | port: partnerUrl.port, | 25 | port: partnerUrl.port, |
26 | path: partnerUrl.pathname | 26 | path: partnerUrl.pathname |
27 | }; | 27 | }; |
28 | 28 | ||
29 | return partnerUrl.protocol === 'https' ? xmlrpc.createSecureClient(clientOptions) : xmlrpc.createClient(clientOptions); | 29 | return partnerUrl.protocol === 'https' ? xmlrpc.createSecureClient(clientOptions) : xmlrpc.createClient(clientOptions); |
30 | } | 30 | } |
31 | 31 | ||
32 | function buy(task) { | 32 | function buy(task) { |
33 | const params = { | 33 | const params = { |
34 | MSISDN: config.partner.msisdn || config.partner.userid, | 34 | MSISDN: config.partner.msisdn || config.partner.userid, |
35 | REQUESTID: task.trx_id, | 35 | REQUESTID: task.trx_id, |
36 | PIN: config.partner.pin || config.partner.password, | 36 | PIN: config.partner.pin || config.partner.password, |
37 | NOHP: task.destination, | 37 | NOHP: task.destination, |
38 | NOM: task.remote_product | 38 | NOM: task.remote_product |
39 | }; | 39 | }; |
40 | 40 | ||
41 | const xmlrpcMethod = 'topUpRequest'; | 41 | const xmlrpcMethod = 'topUpRequest'; |
42 | logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); | 42 | logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); |
43 | 43 | ||
44 | const client = createXmlRpcClient(config.partner.url); | 44 | const client = createXmlRpcClient(config.partner.url); |
45 | client.methodCall(xmlrpcMethod, [ params ], function (err, value) { | 45 | client.methodCall(xmlrpcMethod, [ params ], function (err, value) { |
46 | 46 | ||
47 | if (err) { | 47 | if (err) { |
48 | 48 | ||
49 | let msg = 'XMLRPC Client Error: ' + err; | 49 | let msg = 'XMLRPC Client Error: ' + err; |
50 | let rc = '68'; | 50 | let rc = '68'; |
51 | 51 | ||
52 | if ( | 52 | if ( |
53 | err.code === 'ECONNREFUSED' | 53 | err.code === 'ECONNREFUSED' |
54 | || err.code === 'EHOSTUNREACH' | 54 | || err.code === 'EHOSTUNREACH' |
55 | || (err.code === 'ETIMEDOUT' && err.syscall === "connect") | 55 | || (err.code === 'ETIMEDOUT' && err.syscall === "connect") |
56 | || (err.code === 'EPROTO' && err.syscall === "write") | 56 | || (err.code === 'EPROTO' && err.syscall === "write") |
57 | ) { | 57 | ) { |
58 | rc = '91'; | 58 | rc = '91'; |
59 | } | 59 | } |
60 | 60 | ||
61 | logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); | 61 | logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); |
62 | report({ | 62 | report({ |
63 | trx_id: task.trx_id, | 63 | trx_id: task.trx_id, |
64 | rc: rc, | 64 | rc: rc, |
65 | message: 'INTERNAL: ' + msg, | 65 | message: 'INTERNAL: ' + msg, |
66 | misc: { | 66 | misc: { |
67 | task: task | 67 | task: task |
68 | } | 68 | } |
69 | }); | 69 | }); |
70 | 70 | ||
71 | if (rc === '68') { | ||
72 | setTimeout( | ||
73 | function() { advice(task); }, | ||
74 | 5 * 60 * 1000 | ||
75 | ); | ||
76 | } | ||
77 | |||
71 | return; | 78 | return; |
72 | } | 79 | } |
73 | 80 | ||
74 | logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); | 81 | logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); |
75 | matrix.last_topupRequest_ack = value; | 82 | matrix.last_topupRequest_ack = value; |
76 | 83 | ||
77 | report({ | 84 | report({ |
78 | trx_id: task.trx_id, | 85 | trx_id: task.trx_id, |
79 | rc: partnerRc[value.RESPONSECODE] || '40', | 86 | rc: partnerRc[value.RESPONSECODE] || '40', |
80 | message: value.MESSAGE, | 87 | message: value.MESSAGE, |
81 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), | 88 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), |
82 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), | 89 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), |
83 | raw: value, | 90 | raw: value, |
84 | misc: { | 91 | misc: { |
85 | task: task | 92 | task: task |
86 | } | 93 | } |
87 | }); | 94 | }); |
88 | }); | 95 | }); |
89 | } | 96 | } |
90 | 97 | ||
91 | function advice(task) { | 98 | function advice(task) { |
92 | const params = { | 99 | const params = { |
93 | REQUESTID: task.trx_id, | 100 | REQUESTID: task.trx_id, |
94 | MSISDN: config.partner.msisdn || config.partner.userid, | 101 | MSISDN: config.partner.msisdn || config.partner.userid, |
95 | PIN: config.partner.pin || config.partner.password, | 102 | PIN: config.partner.pin || config.partner.password, |
96 | NOHP: task.destination | 103 | NOHP: task.destination |
97 | }; | 104 | }; |
98 | 105 | ||
99 | const xmlrpcMethod = 'topUpInquiry'; | 106 | const xmlrpcMethod = 'topUpInquiry'; |
100 | logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); | 107 | logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: config.partner.url}); |
101 | 108 | ||
102 | const client = createXmlRpcClient(config.partner.url); | 109 | const client = createXmlRpcClient(config.partner.url); |
103 | client.methodCall(xmlrpcMethod, [ params ], function (err, value) { | 110 | client.methodCall(xmlrpcMethod, [ params ], function (err, value) { |
104 | 111 | ||
105 | if (err) { | 112 | if (err) { |
106 | 113 | ||
107 | const msg = 'XMLRPC Client Error: ' + err; | 114 | const msg = 'XMLRPC Client Error: ' + err; |
108 | 115 | ||
109 | logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); | 116 | logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err}); |
110 | report({ | 117 | report({ |
111 | trx_id: task.trx_id, | 118 | trx_id: task.trx_id, |
112 | rc: '68', | 119 | rc: '68', |
113 | message: 'INTERNAL: ' + msg, | 120 | message: 'INTERNAL: ' + msg, |
114 | misc: { | 121 | misc: { |
115 | task: task | 122 | task: task |
116 | } | 123 | } |
117 | }); | 124 | }); |
118 | 125 | ||
126 | setTimeout( | ||
127 | function() { advice(task); }, | ||
128 | 60 * 1000 | ||
129 | ); | ||
130 | |||
119 | return; | 131 | return; |
120 | } | 132 | } |
121 | 133 | ||
122 | logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); | 134 | logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value}); |
123 | //matrix.last_topupRequest_ack = value; | 135 | //matrix.last_topupRequest_ack = value; |
124 | 136 | ||
125 | report({ | 137 | report({ |
126 | trx_id: task.trx_id, | 138 | trx_id: task.trx_id, |
127 | rc: partnerRc[value.RESPONSECODE] || '40', | 139 | rc: partnerRc[value.RESPONSECODE] || '40', |
128 | message: value.MESSAGE, | 140 | message: value.MESSAGE, |
129 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), | 141 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), |
130 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), | 142 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), |
131 | raw: value, | 143 | raw: value, |
132 | misc: { | 144 | misc: { |
133 | task: task | 145 | task: task |
134 | } | 146 | } |
135 | }); | 147 | }); |
136 | }); | 148 | }); |
137 | } | 149 | } |
138 | 150 | ||
139 | function report(data) { | 151 | function report(data) { |
140 | if (!data) { | 152 | if (!data) { |
141 | return; | 153 | return; |
142 | } | 154 | } |
143 | 155 | ||
144 | matrix.last_report_to_core = data; | 156 | matrix.last_report_to_core = data; |
145 | pull.report(data); | 157 | pull.report(data); |
146 | } | 158 | } |
147 | 159 | ||
148 | exports.buy = buy; | 160 | exports.buy = buy; |
149 | exports.advice = advice; | 161 | exports.advice = advice; |
150 | exports.report = report; | 162 | exports.report = report; |
151 | 163 |