Commit c8f25c5626bbd907524e9e30eece89596d6e52bb

Authored by Adhidarma Hadiwinoto
1 parent 41a511b3c3
Exists in master

tidak init antiSameDayDupe saat init partner

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

1 "use strict"; 1 "use strict";
2 2
3 var request = require('request'); 3 var request = require('request');
4 var crypto = require('crypto'); 4 var crypto = require('crypto');
5 var url = require('url'); 5 var url = require('url');
6 var http = require('http'); 6 var http = require('http');
7 7
8 var resendDelay = require('sate24/resend-delay'); 8 var resendDelay = require('sate24/resend-delay');
9 var taskHistory = require('sate24/task-history'); 9 var taskHistory = require('sate24/task-history');
10 var AntiSameDayDupe = require('sate24/anti-same-day-dupe-oo'); 10 var AntiSameDayDupe = require('sate24/anti-same-day-dupe-oo');
11 var antiSameDayDupe; 11 var antiSameDayDupe;
12 12
13 var config; 13 var config;
14 var aaa; 14 var aaa;
15 var logger; 15 var logger;
16 16
17 http.globalAgent.maxSockets = Infinity; 17 http.globalAgent.maxSockets = Infinity;
18 18
19 function start(options) { 19 function start(options) {
20 if (!options) { 20 if (!options) {
21 console.log('Undefined options, terminating....'); 21 console.log('Undefined options, terminating....');
22 process.exit(1); 22 process.exit(1);
23 } 23 }
24 24
25 if (options.config) { 25 if (options.config) {
26 config = options.config; 26 config = options.config;
27 } else { 27 } else {
28 console.log('Undefined options.config, terminating....') 28 console.log('Undefined options.config, terminating....')
29 process.exit(1); 29 process.exit(1);
30 } 30 }
31 31
32 if (options.aaa) { 32 if (options.aaa) {
33 aaa = options.aaa; 33 aaa = options.aaa;
34 } else { 34 } else {
35 console.log('Undefined options.aaa, terminating....') 35 console.log('Undefined options.aaa, terminating....')
36 process.exit(1); 36 process.exit(1);
37 } 37 }
38 38
39 if (options && options.logger) { 39 if (options && options.logger) {
40 logger = options.logger; 40 logger = options.logger;
41 } else { 41 } else {
42 console.log('Undefined options.logger, terminating....') 42 console.log('Undefined options.logger, terminating....')
43 process.exit(1); 43 process.exit(1);
44 } 44 }
45 45
46 resendDelay.init({ 46 resendDelay.init({
47 config: config, 47 config: config,
48 topupRequest: checkStatus, 48 topupRequest: checkStatus,
49 logger: logger 49 logger: logger
50 }); 50 });
51 51
52 taskHistory.init(options); 52 taskHistory.init(options);
53 antiSameDayDupe.init(options); 53 //antiSameDayDupe.init(options);
54 54
55 createReverseHttpServer(); 55 createReverseHttpServer();
56 } 56 }
57 57
58 function callbackReport(requestId, rc, message) { 58 function callbackReport(requestId, rc, message) {
59 if (rc != '68') { 59 if (rc != '68') {
60 resendDelay.cancel(requestId); 60 resendDelay.cancel(requestId);
61 } else { 61 } else {
62 taskHistory.get(requestId, function(err, archivedTask) { 62 taskHistory.get(requestId, function(err, archivedTask) {
63 if (archivedTask) { 63 if (archivedTask) {
64 resendDelay.register(archivedTask); 64 resendDelay.register(archivedTask);
65 } else { 65 } else {
66 logger.warn('Can not find task from history', {request_id: requestId}); 66 logger.warn('Can not find task from history', {request_id: requestId});
67 } 67 }
68 }); 68 });
69 } 69 }
70 70
71 aaa.callbackReportWithPushToMongoDb(requestId, rc, message); 71 aaa.callbackReportWithPushToMongoDb(requestId, rc, message);
72 } 72 }
73 73
74 function _initSameDayDupe() { 74 function _initSameDayDupe() {
75 logger.info('Initializing antiSameDayDupe'); 75 logger.info('Initializing antiSameDayDupe');
76 antiSameDayDupe = new AntiSameDayDupe({ 76 antiSameDayDupe = new AntiSameDayDupe({
77 config: config, 77 config: config,
78 logger: logger, 78 logger: logger,
79 keyPrefix: 'otomax.', 79 keyPrefix: 'otomax.',
80 redisClient: redisClient 80 redisClient: redisClient
81 }); 81 });
82 } 82 }
83 83
84 function topupRequest(task) { 84 function topupRequest(task) {
85 if (!aaa.isTodayTrx(task)) { 85 if (!aaa.isTodayTrx(task)) {
86 logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan'); 86 logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan');
87 callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan'); 87 callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan');
88 resendDelay.cancel(task); 88 resendDelay.cancel(task);
89 return; 89 return;
90 } 90 }
91 91
92 aaa.insertTaskToMongoDb(task); 92 aaa.insertTaskToMongoDb(task);
93 93
94 if (!antiSameDayDupe) { 94 if (!antiSameDayDupe) {
95 _initSameDayDupe(); 95 _initSameDayDupe();
96 } 96 }
97 antiSameDayDupe.check(task, _topupRequest, onSameDayDupe, checkStatus); 97 antiSameDayDupe.check(task, _topupRequest, onSameDayDupe, checkStatus);
98 } 98 }
99 99
100 function _topupRequest(task) { 100 function _topupRequest(task) {
101 taskHistory.put(task, function() { 101 taskHistory.put(task, function() {
102 requestToPartner(task);; 102 requestToPartner(task);;
103 }); 103 });
104 } 104 }
105 105
106 function checkStatus(task) { 106 function checkStatus(task) {
107 logger.verbose('Going to request checkStatus', {task: task}); 107 logger.verbose('Going to request checkStatus', {task: task});
108 taskHistory.put(task, function() { 108 taskHistory.put(task, function() {
109 requestToPartner(task, true); 109 requestToPartner(task, true);
110 }); 110 });
111 } 111 }
112 112
113 function requestToPartner(task, pendingOnErrorConnect) { 113 function requestToPartner(task, pendingOnErrorConnect) {
114 let requestOptions = createRequestOptions(task); 114 let requestOptions = createRequestOptions(task);
115 115
116 logger.info('Requesting to partner', {task: task, request_options: requestOptions}); 116 logger.info('Requesting to partner', {task: task, request_options: requestOptions});
117 117
118 request(requestOptions, function(error, response, body) { 118 request(requestOptions, function(error, response, body) {
119 if (error) { 119 if (error) {
120 let rc = '68'; 120 let rc = '68';
121 121
122 if (!pendingOnErrorConnect && (error.syscall == 'connect')) { 122 if (!pendingOnErrorConnect && (error.syscall == 'connect')) {
123 rc = '91'; 123 rc = '91';
124 } 124 }
125 125
126 logger.warn('Error requesting to partner', {task: task, rc: rc, error: error}); 126 logger.warn('Error requesting to partner', {task: task, rc: rc, error: error});
127 callbackReport(task.requestId, rc, 'Error requesting to partner. ' + error); 127 callbackReport(task.requestId, rc, 'Error requesting to partner. ' + error);
128 return; 128 return;
129 } 129 }
130 130
131 if (response.statusCode != 200) { 131 if (response.statusCode != 200) {
132 logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); 132 logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode});
133 callbackReport(task.requestId, '40', 'HTTP status code ' + response.statusCode); 133 callbackReport(task.requestId, '40', 'HTTP status code ' + response.statusCode);
134 return; 134 return;
135 } 135 }
136 136
137 logger.verbose('Got response from partner', {task: task, body: body}); 137 logger.verbose('Got response from partner', {task: task, body: body});
138 138
139 parseMessage(task, body); 139 parseMessage(task, body);
140 }); 140 });
141 } 141 }
142 142
143 function getRequestIdFromTask(task) { 143 function getRequestIdFromTask(task) {
144 if (typeof task == 'string') { 144 if (typeof task == 'string') {
145 return task; 145 return task;
146 } 146 }
147 147
148 try { 148 try {
149 let requestId = task.requestId; 149 let requestId = task.requestId;
150 return requestId; 150 return requestId;
151 } 151 }
152 catch(e) {return}; 152 catch(e) {return};
153 } 153 }
154 154
155 function parseMessage(task, message) { 155 function parseMessage(task, message) {
156 let requestId = getRequestIdFromTask(task); 156 let requestId = getRequestIdFromTask(task);
157 157
158 if (!requestId) { 158 if (!requestId) {
159 logger.warn('Invalid requestId on parseMessage', {task: task, message: message}); 159 logger.warn('Invalid requestId on parseMessage', {task: task, message: message});
160 return; 160 return;
161 } 161 }
162 162
163 let rc = '68'; 163 let rc = '68';
164 164
165 if (message.indexOf('SUKSES') >= 0) { 165 if (message.indexOf('SUKSES') >= 0) {
166 rc = '00'; 166 rc = '00';
167 } 167 }
168 else if (message.indexOf('GAGAL. Nomor tujuan salah') >= 0) { 168 else if (message.indexOf('GAGAL. Nomor tujuan salah') >= 0) {
169 rc = '14'; 169 rc = '14';
170 } 170 }
171 else if (message.indexOf('NO HP TIDAK TERDAFTAR ATAU BUKAN NOMOR PRA-BAYAR') >= 0) { 171 else if (message.indexOf('NO HP TIDAK TERDAFTAR ATAU BUKAN NOMOR PRA-BAYAR') >= 0) {
172 rc = '14'; 172 rc = '14';
173 } 173 }
174 else if (message.indexOf('Gagal.') >= 0) { 174 else if (message.indexOf('Gagal.') >= 0) {
175 rc = '40'; 175 rc = '40';
176 } 176 }
177 else if (message.indexOf('GAGAL') >= 0) { 177 else if (message.indexOf('GAGAL') >= 0) {
178 rc = '40'; 178 rc = '40';
179 } 179 }
180 else if (message.indexOf('gagal, dibatalkan SILAHKAN DICOBA KEMBALI') >= 0) { 180 else if (message.indexOf('gagal, dibatalkan SILAHKAN DICOBA KEMBALI') >= 0) {
181 rc = '40'; 181 rc = '40';
182 } 182 }
183 else if (message.indexOf('Invalid Signature') >= 0) { 183 else if (message.indexOf('Invalid Signature') >= 0) {
184 rc = '40'; 184 rc = '40';
185 } 185 }
186 186
187 if (rc == '00') { 187 if (rc == '00') {
188 let sn = parseSn(message, config.h2h_out.sn_pattern); 188 let sn = parseSn(message, config.h2h_out.sn_pattern);
189 if (sn) { 189 if (sn) {
190 message = "SN=" + sn + ";" + message; 190 message = "SN=" + sn + ";" + message;
191 } 191 }
192 } 192 }
193 193
194 callbackReport(requestId, rc, message); 194 callbackReport(requestId, rc, message);
195 } 195 }
196 196
197 function generateSign(userid, remoteProduct, destination, requestId, pin, password) { 197 function generateSign(userid, remoteProduct, destination, requestId, pin, password) {
198 let plain = ["OtomaX", userid, remoteProduct, destination, requestId, pin, password].join("|"); 198 let plain = ["OtomaX", userid, remoteProduct, destination, requestId, pin, password].join("|");
199 //let sha1 = crypto.createHash('sha1').update(plain).digest().toString('hex'); 199 //let sha1 = crypto.createHash('sha1').update(plain).digest().toString('hex');
200 //let buffer = new Buffer(sha1); 200 //let buffer = new Buffer(sha1);
201 let buffer = crypto.createHash('sha1').update(plain).digest(); 201 let buffer = crypto.createHash('sha1').update(plain).digest();
202 202
203 return buffer.toString('base64').replace(/\//g, '_').replace(/\+/g, '-'); 203 return buffer.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
204 } 204 }
205 205
206 function createRequestOptions(task) { 206 function createRequestOptions(task) {
207 return { 207 return {
208 url: config.h2h_out.partner, 208 url: config.h2h_out.partner,
209 qs: { 209 qs: {
210 memberID: config.h2h_out.userid, 210 memberID: config.h2h_out.userid,
211 product: task.remoteProduct, 211 product: task.remoteProduct,
212 dest: task.destination, 212 dest: task.destination,
213 refID: task.requestId, 213 refID: task.requestId,
214 sign: generateSign( 214 sign: generateSign(
215 config.h2h_out.userid, 215 config.h2h_out.userid,
216 task.remoteProduct, 216 task.remoteProduct,
217 task.destination, 217 task.destination,
218 task.requestId, 218 task.requestId,
219 config.h2h_out.pin, 219 config.h2h_out.pin,
220 config.h2h_out.password 220 config.h2h_out.password
221 ) 221 )
222 } 222 }
223 }; 223 };
224 } 224 }
225 225
226 function onSameDayDupe(task) { 226 function onSameDayDupe(task) {
227 callbackReport(task.requestId, '55', 'Transaksi duplikat dalam satu hari yang sama'); 227 callbackReport(task.requestId, '55', 'Transaksi duplikat dalam satu hari yang sama');
228 } 228 }
229 229
230 function reverseHttpServerServerHandler(request, response) { 230 function reverseHttpServerServerHandler(request, response) {
231 let qs = url.parse(request.url, true).query; 231 let qs = url.parse(request.url, true).query;
232 logger.verbose('Hit on Reverse HTTP server', {url: request.url, qs: qs}); 232 logger.verbose('Hit on Reverse HTTP server', {url: request.url, qs: qs});
233 233
234 parseMessage(qs.refid, qs.message); 234 parseMessage(qs.refid, qs.message);
235 } 235 }
236 236
237 function createReverseHttpServer() { 237 function createReverseHttpServer() {
238 var httpServer = http.createServer(reverseHttpServerServerHandler); 238 var httpServer = http.createServer(reverseHttpServerServerHandler);
239 239
240 httpServer.listen(config.h2h_out.listen_port, function() { 240 httpServer.listen(config.h2h_out.listen_port, function() {
241 logger.info('Reverse Report HTTP Server listen on %d', config.h2h_out.listen_port); 241 logger.info('Reverse Report HTTP Server listen on %d', config.h2h_out.listen_port);
242 }); 242 });
243 } 243 }
244 244
245 function parseSn(message, pattern) { 245 function parseSn(message, pattern) {
246 246
247 try { 247 try {
248 let sn_regex = new RegExp(pattern); 248 let sn_regex = new RegExp(pattern);
249 let sn_match = message.match(sn_regex); 249 let sn_match = message.match(sn_regex);
250 250
251 return sn_match[1].trim(); 251 return sn_match[1].trim();
252 } 252 }
253 catch(e) { 253 catch(e) {
254 return; 254 return;
255 } 255 }
256 256
257 } 257 }
258 258
259 exports.start = start; 259 exports.start = start;
260 exports.topupRequest = topupRequest; 260 exports.topupRequest = topupRequest;
261 exports.checkStatus = checkStatus; 261 exports.checkStatus = checkStatus;
262 exports.generateSign = generateSign; 262 exports.generateSign = generateSign;
263 exports.parseSn = parseSn; 263 exports.parseSn = parseSn;
264 264