Commit 975661c396c0c27844a994ee101467992671e251

Authored by Adhidarma Hadiwinoto
1 parent 60fa6ddb46
Exists in master

config.h2h_out.partner

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 var xml2js = require('xml2js'); 1 var xml2js = require('xml2js');
2 2
3 var aaa; 3 var aaa;
4 var _callbackReport; 4 var _callbackReport;
5 var config; 5 var config;
6 var logger; 6 var logger;
7 7
8 var xmlBuilder = new xml2js.Builder(); 8 var xmlBuilder = new xml2js.Builder();
9 9
10 function start(options) { 10 function start(options) {
11 if (!options) { 11 if (!options) {
12 console.log('Undefined options, terminating....'); 12 console.log('Undefined options, terminating....');
13 process.exit(1); 13 process.exit(1);
14 } 14 }
15 15
16 if (options.config) { 16 if (options.config) {
17 config = options.config; 17 config = options.config;
18 } else { 18 } else {
19 console.log('Undefined options.config, terminating....') 19 console.log('Undefined options.config, terminating....')
20 process.exit(1); 20 process.exit(1);
21 } 21 }
22 22
23 if (options.aaa) { 23 if (options.aaa) {
24 aaa = options.aaa; 24 aaa = options.aaa;
25 _callbackReport = options.aaa.callbackReportWithPushToMongoDb; 25 _callbackReport = options.aaa.callbackReportWithPushToMongoDb;
26 } else { 26 } else {
27 console.log('Undefined options.aaa, terminating....') 27 console.log('Undefined options.aaa, terminating....')
28 process.exit(1); 28 process.exit(1);
29 } 29 }
30 30
31 if (options && options.logger) { 31 if (options && options.logger) {
32 logger = options.logger; 32 logger = options.logger;
33 } else { 33 } else {
34 console.log('Undefined options.logger, terminating....') 34 console.log('Undefined options.logger, terminating....')
35 process.exit(1); 35 process.exit(1);
36 } 36 }
37 37
38 createServer(); 38 createServer();
39 39
40 /* 40 /*
41 resendDelay.init({ 41 resendDelay.init({
42 config: config, 42 config: config,
43 topupRequest: topupRequest, 43 topupRequest: topupRequest,
44 logger: logger 44 logger: logger
45 }); 45 });
46 */ 46 */
47 } 47 }
48 48
49 function topupRequest(task) { 49 function topupRequest(task) {
50 aaa.insertTaskToMongoDb(task); 50 aaa.insertTaskToMongoDb(task);
51 51
52 var payload = composeTopupStatusMessage( 52 var payload = composeTopupStatusMessage(
53 config.h2h_out.pin, 53 config.h2h_out.pin,
54 task.remoteProduct, 54 task.remoteProduct,
55 task.destination, 55 task.destination,
56 task.requestId 56 task.requestId
57 ); 57 );
58 58
59 var reqOpts = { 59 var reqOpts = {
60 url: config.h2h_out.partner_url, 60 url: config.h2h_out.partner,
61 method: "POST", 61 method: "POST",
62 headers: { 62 headers: {
63 'Content-Type': 'text/xml', 63 'Content-Type': 'text/xml',
64 'Content-Length': Buffer.byteLength(payload) 64 'Content-Length': Buffer.byteLength(payload)
65 } 65 }
66 } 66 }
67 67
68 logger.verbose('Requesting to partner', {reqOpts: reqOpts, payload: payload}); 68 logger.verbose('Requesting to partner', {reqOpts: reqOpts, payload: payload});
69 var buffer = ""; 69 var buffer = "";
70 var req = http.request(reqOpts, function( res ) { 70 var req = http.request(reqOpts, function( res ) {
71 71
72 logger.info('Status code: ' + res.statusCode ); 72 logger.info('Status code: ' + res.statusCode );
73 var buffer = ""; 73 var buffer = "";
74 res.on( "data", function( data ) { buffer = buffer + data; } ); 74 res.on( "data", function( data ) { buffer = buffer + data; } );
75 res.on( "end", function( data ) { 75 res.on( "end", function( data ) {
76 logger.verbose('Got a direct response from partner', {response: buffer, task: task}); 76 logger.verbose('Got a direct response from partner', {response: buffer, task: task});
77 topupResponseHandler(buffer, task.requestId); 77 topupResponseHandler(buffer, task.requestId);
78 }); 78 });
79 }); 79 });
80 } 80 }
81 81
82 function topupResponseHandler(xmlResponse, _requestId, cb) { 82 function topupResponseHandler(xmlResponse, _requestId, cb) {
83 var xmlParser = xml2js.parseString; 83 var xmlParser = xml2js.parseString;
84 xmlParser(xmlResponse, function(err, data) { 84 xmlParser(xmlResponse, function(err, data) {
85 var msg; 85 var msg;
86 var requestId; 86 var requestId;
87 var rc = '68'; 87 var rc = '68';
88 88
89 if (_requestId) { 89 if (_requestId) {
90 requestId = _requestId; 90 requestId = _requestId;
91 } 91 }
92 92
93 if (err) { 93 if (err) {
94 msg = 'Error parsing xml response: ' + err; 94 msg = 'Error parsing xml response: ' + err;
95 95
96 if (logger) { 96 if (logger) {
97 logger.warn(msg, {err: err, response: xmlResponse, task: task}); 97 logger.warn(msg, {err: err, response: xmlResponse, task: task});
98 } else { 98 } else {
99 console.log(msg); 99 console.log(msg);
100 } 100 }
101 } else { 101 } else {
102 102
103 try { 103 try {
104 msg = data.fm.message 104 msg = data.fm.message
105 } 105 }
106 catch(e) { 106 catch(e) {
107 msg = 'Unknown message' 107 msg = 'Unknown message'
108 } 108 }
109 109
110 if (data.fm.status == '0') { 110 if (data.fm.status == '0') {
111 111
112 rc = '00'; 112 rc = '00';
113 msg = modifyMessageWithSn(msg); 113 msg = modifyMessageWithSn(msg);
114 114
115 } else if (data.fm.status == '1') { 115 } else if (data.fm.status == '1') {
116 rc = '68'; 116 rc = '68';
117 } else if (data.fm.status == '2') { 117 } else if (data.fm.status == '2') {
118 rc = '40'; 118 rc = '40';
119 } else if (data.fm.status == '3') { 119 } else if (data.fm.status == '3') {
120 rc = '40'; 120 rc = '40';
121 } else { 121 } else {
122 rc = '68'; 122 rc = '68';
123 } 123 }
124 124
125 if (data.fm.refTrxid) { 125 if (data.fm.refTrxid) {
126 requestId = data.fm.refTrxid; 126 requestId = data.fm.refTrxid;
127 } 127 }
128 128
129 } 129 }
130 130
131 cb(requestId, rc, msg, xmlResponse) 131 cb(requestId, rc, msg, xmlResponse)
132 }); 132 });
133 } 133 }
134 134
135 function callbackReport(requestId, responseCode, msg, rawResponse) { 135 function callbackReport(requestId, responseCode, msg, rawResponse) {
136 if (requestId) { 136 if (requestId) {
137 _callbackReport(requestId, responseCode, msg, null, rawResponse); 137 _callbackReport(requestId, responseCode, msg, null, rawResponse);
138 } else { 138 } else {
139 logger.warn('Undefined requestId, not sending callbackReport', {rc: responseCode, msg: msg, rawResponse: rawResponse}); 139 logger.warn('Undefined requestId, not sending callbackReport', {rc: responseCode, msg: msg, rawResponse: rawResponse});
140 } 140 }
141 141
142 } 142 }
143 143
144 function getSnFromMessage(msg) { 144 function getSnFromMessage(msg) {
145 try { 145 try {
146 var matches = msg.match(/SN:(\w+)/); 146 var matches = msg.match(/SN:(\w+)/);
147 return matches[1]; 147 return matches[1];
148 } 148 }
149 catch(e) { 149 catch(e) {
150 return; 150 return;
151 } 151 }
152 } 152 }
153 153
154 function modifyMessageWithSn(msg) { 154 function modifyMessageWithSn(msg) {
155 var sn = getSnFromMessage(msg); 155 var sn = getSnFromMessage(msg);
156 if (sn) { 156 if (sn) {
157 msg = 'SN=' + sn + '; ' + msg; 157 msg = 'SN=' + sn + '; ' + msg;
158 } 158 }
159 return msg; 159 return msg;
160 } 160 }
161 161
162 function composeTopupStatusMessage(pin, product, destination, requestId) { 162 function composeTopupStatusMessage(pin, product, destination, requestId) {
163 var data = {fm: { 163 var data = {fm: {
164 command: 'TOPUP', 164 command: 'TOPUP',
165 pin: pin, 165 pin: pin,
166 product: product, 166 product: product,
167 msisdn: destination, 167 msisdn: destination,
168 refTrxid: requestId 168 refTrxid: requestId
169 }} 169 }}
170 170
171 return xmlBuilder.buildObject(data); 171 return xmlBuilder.buildObject(data);
172 } 172 }
173 173
174 exports.start = start; 174 exports.start = start;
175 exports.topupRequest = topupRequest; 175 exports.topupRequest = topupRequest;
176 exports.composeTopupStatusMessage = composeTopupStatusMessage; 176 exports.composeTopupStatusMessage = composeTopupStatusMessage;
177 exports.getSnFromMessage = getSnFromMessage; 177 exports.getSnFromMessage = getSnFromMessage;
178 exports.modifyMessageWithSn = modifyMessageWithSn; 178 exports.modifyMessageWithSn = modifyMessageWithSn;
179 179