partner-bayarkilat.js
2.39 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
var request = require('request');
var url = require('url');
var winston = require('winston');
var config;
var aaa;
var callbackReport;
var logger;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
function start(_config, _callbackReport, options) {
config = _config;
callbackReport = _callbackReport;
if (options && options.aaa) {
aaa = options.aaa;
}
if (options && options.logger) {
logger = options.logger;
} else {
logger = new winston.Logger({
transports: [
new (winston.transports.Console)()
]
});
}
}
function topupRequest(task, retry) {
aaa.insertTaskToMongoDb(task);
var partnerUrl = url.parse(config.h2h_out.partner);
var product = prepareRemoteProductCode(task.remoteProduct);
var options = {
url: config.h2h_out.partner,
qs: {
request: 'PURCHASE*'
+ task.requestId + '*'
+ product.product + '*'
+ product.productDetail + '*'
+ task.destination + '*'
+ product.nominal + '*'
+ '0*'
+ config.h2h_out.noid + '*'
+ config.h2h_out.userid + '*'
+ config.h2h_out.password
}
};
logger.verbose('Requesting to partner', {requestOption: options});
request(options, function(error, response, body) {
if (error) {
logger.warn('Error requesting to partner', {error: error});
callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error);
return;
}
if (response.statusCode != 200) {
var message = 'Partner response with http status code other that 200 (' + response.statusCode +')';
logger.warn(message);
callbackReport(task.requestId, '68', message);
return;
}
logger.verbose('Got response', {requestId: task.requestId, responseBody: body});
callbackReport(task.requestId, '68', body);
});
}
function prepareRemoteProductCode(remoteProduct) {
var product = remoteProduct.split(',');
if (product.length != 3) {
return;
}
return {
product: product[0],
productDetail: product[1],
nominal: product[2]
}
}
exports.start = start;
exports.topupRequest = topupRequest;
exports.prepareRemoteProductCode = prepareRemoteProductCode;