partner.js
2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"use strict";
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const url = require('url');
const https = require('https');
const xmlrpc = require('xmlrpc');
const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');
const matrix = require('komodo-sdk/matrix');
const pull = require('komodo-sdk/gateway/pull');
const st24 = require('./st24');
const partnerRc = require('./partner-rc.json');
if (config.partner.use_sslv3) {
https.globalAgent.options.secureProtocol = 'SSLv3_method';
}
function buy(task) {
const partnerUrl = url.parse(config.partner.url);
const clientOptions = {
host: partnerUrl.hostname,
port: partnerUrl.port,
path: partnerUrl.pathname
};
let client;
if (partnerUrl.protocol == 'https:') {
client = xmlrpc.createSecureClient(clientOptions);
} else {
client = xmlrpc.createClient(clientOptions);
}
const params = {
MSISDN: config.partner.msisdn || config.partner.userid,
REQUESTID: task.trx_id,
PIN: config.partner.pin || config.partner.password,
NOHP: task.destination,
NOM: task.remote_product
};
const xmlrpcMethod = 'topUpRequest';
logger.info('Preparing XMLRPC request', {method: xmlrpcMethod, params: params, partnerUrl: partnerUrl.href});
client.methodCall(xmlrpcMethod, [ params ], function (err, value) {
if (err) {
let msg = 'XMLRPC Client Error: ' + err;
let rc = '68';
if (
err.code === 'ECONNREFUSED'
|| err.code === 'EHOSTUNREACH'
|| (err.code === 'ETIMEDOUT' && err.syscall === "connect")
|| (err.code === 'EPROTO' && err.syscall === "write")
) {
rc = '91';
}
logger.warn(msg, {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, err: err});
report({
trx_id: task.trx_id,
rc: rc,
message: 'INTERNAL: ' + msg,
misc: {
task: task
}
});
return;
}
logger.info('Got XMLRPC response from partner for', {method: xmlrpcMethod, trx_id: task.trx_id, destination: task.destination, response: value});
matrix.last_topupRequest_ack = value;
report({
trx_id: task.trx_id,
rc: partnerRc[value.RESPONSECODE] || '40',
message: value.MESSAGE,
sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE),
amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE),
raw: value,
misc: {
task: task
}
});
});
}
function advice(task) {
}
function report(data) {
matrix.last_report_to_core = data;
pull.report(data);
}
exports.buy = buy;
exports.advice = advice;
exports.report = report;