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