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