partner-fm.js
4.26 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var xml2js = require('xml2js');
var aaa;
var _callbackReport;
var config;
var logger;
var xmlBuilder = new xml2js.Builder();
function start(options) {
if (!options) {
console.log('Undefined options, terminating....');
process.exit(1);
}
if (options.config) {
config = options.config;
} else {
console.log('Undefined options.config, terminating....')
process.exit(1);
}
if (options.aaa) {
aaa = options.aaa;
_callbackReport = options.aaa.callbackReportWithPushToMongoDb;
} else {
console.log('Undefined options.aaa, terminating....')
process.exit(1);
}
if (options && options.logger) {
logger = options.logger;
} else {
console.log('Undefined options.logger, terminating....')
process.exit(1);
}
createServer();
/*
resendDelay.init({
config: config,
topupRequest: topupRequest,
logger: logger
});
*/
}
function topupRequest(task) {
aaa.insertTaskToMongoDb(task);
var payload = composeTopupStatusMessage(
config.h2h_out.pin,
task.remoteProduct,
task.destination,
task.requestId
);
var reqOpts = {
url: config.h2h_out.partner,
method: "POST",
headers: {
'Content-Type': 'text/xml',
'Content-Length': Buffer.byteLength(payload)
}
}
logger.verbose('Requesting to partner', {reqOpts: reqOpts, payload: payload});
var buffer = "";
var req = http.request(reqOpts, function( res ) {
logger.info('Status code: ' + res.statusCode );
var buffer = "";
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function( data ) {
logger.verbose('Got a direct response from partner', {response: buffer, task: task});
topupResponseHandler(buffer, task.requestId);
});
});
}
function topupResponseHandler(xmlResponse, _requestId, cb) {
var xmlParser = xml2js.parseString;
xmlParser(xmlResponse, function(err, data) {
var msg;
var requestId;
var rc = '68';
if (_requestId) {
requestId = _requestId;
}
if (err) {
msg = 'Error parsing xml response: ' + err;
if (logger) {
logger.warn(msg, {err: err, response: xmlResponse, task: task});
} else {
console.log(msg);
}
} else {
try {
msg = data.fm.message
}
catch(e) {
msg = 'Unknown message'
}
if (data.fm.status == '0') {
rc = '00';
msg = modifyMessageWithSn(msg);
} else if (data.fm.status == '1') {
rc = '68';
} else if (data.fm.status == '2') {
rc = '40';
} else if (data.fm.status == '3') {
rc = '40';
} else {
rc = '68';
}
if (data.fm.refTrxid) {
requestId = data.fm.refTrxid;
}
}
cb(requestId, rc, msg, xmlResponse)
});
}
function callbackReport(requestId, responseCode, msg, rawResponse) {
if (requestId) {
_callbackReport(requestId, responseCode, msg, null, rawResponse);
} else {
logger.warn('Undefined requestId, not sending callbackReport', {rc: responseCode, msg: msg, rawResponse: rawResponse});
}
}
function getSnFromMessage(msg) {
try {
var matches = msg.match(/SN:(\w+)/);
return matches[1];
}
catch(e) {
return;
}
}
function modifyMessageWithSn(msg) {
var sn = getSnFromMessage(msg);
if (sn) {
msg = 'SN=' + sn + '; ' + msg;
}
return msg;
}
function composeTopupStatusMessage(pin, product, destination, requestId) {
var data = {fm: {
command: 'TOPUP',
pin: pin,
product: product,
msisdn: destination,
refTrxid: requestId
}}
return xmlBuilder.buildObject(data);
}
exports.start = start;
exports.topupRequest = topupRequest;
exports.composeTopupStatusMessage = composeTopupStatusMessage;
exports.getSnFromMessage = getSnFromMessage;
exports.modifyMessageWithSn = modifyMessageWithSn;