Commit 4a2867ea375fb5707c3823103002c7a0aed48cb6

Authored by Adhidarma Hadiwinoto
1 parent 2fdc89e3dd
Exists in master

hapus harga dari pesan balikan

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

partner-scrappingkisel.js
1 var fs = require('fs'); 1 var fs = require('fs');
2 var https = require('https'); 2 var https = require('https');
3 var http = require('http'); 3 var http = require('http');
4 var url = require('url'); 4 var url = require('url');
5 var request = require('request'); 5 var request = require('request');
6 var xml2js = require('xml2js').parseString; 6 var xml2js = require('xml2js').parseString;
7 var strftime = require('strftime'); 7 var strftime = require('strftime');
8 var math = require('mathjs'); 8 var math = require('mathjs');
9 var winston = require('winston'); 9 var winston = require('winston');
10 10
11 var config; 11 var config;
12 var logger; 12 var logger;
13 var aaa; 13 var aaa;
14 14
15 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 15 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
16 16
17 var logTag = __filename.split('/').reverse()[0]; 17 var logTag = __filename.split('/').reverse()[0];
18 18
19 function topupRequest(task) { 19 function topupRequest(task) {
20 var options = { 20 var options = {
21 url: config.h2h_out.partner, 21 url: config.h2h_out.partner,
22 qs: { 22 qs: {
23 reqid: task['requestId'], 23 reqid: task['requestId'],
24 msisdn: task['destination'], 24 msisdn: task['destination'],
25 product: task['remoteProduct'] 25 product: task['remoteProduct']
26 } 26 }
27 }; 27 };
28 logger.info('HTTP request to partner', {request_options: options}); 28 logger.info('HTTP request to partner', {request_options: options});
29 29
30 if (aaa) { 30 if (aaa) {
31 aaa.incrementTrxCount(); 31 aaa.incrementTrxCount();
32 aaa.incrementActiveTrxCount(); 32 aaa.incrementActiveTrxCount();
33 } 33 }
34 34
35 request(options, function (error, response, body) { 35 request(options, function (error, response, body) {
36 if (aaa) { 36 if (aaa) {
37 aaa.decrementActiveTrxCount(); 37 aaa.decrementActiveTrxCount();
38 } 38 }
39 39
40 if (config.globals.active_requests_count == undefined) { 40 if (config.globals.active_requests_count == undefined) {
41 config.globals.active_requests_count = 0; 41 config.globals.active_requests_count = 0;
42 } else { 42 } else {
43 config.globals.active_requests_count--; 43 config.globals.active_requests_count--;
44 } 44 }
45 45
46 if (error || response.statusCode != 200) { 46 if (error || response.statusCode != 200) {
47 logger.warn(logTag + ': Gateway Error'); 47 logger.warn(logTag + ': Gateway Error');
48 callbackReport(task['requestId'], '40', 'Gateway Error'); 48 callbackReport(task['requestId'], '40', 'Gateway Error');
49 return; 49 return;
50 } 50 }
51 51
52 logger.info('Got supplier response', {response_body: body}); 52 logger.info('Got supplier response', {response_body: body});
53 53
54 xml2js(body, function (err, result) { 54 xml2js(body, function (err, result) {
55 if (err) { 55 if (err) {
56 callbackReport(task['requestId'], '40', body); 56 callbackReport(task['requestId'], '40', body);
57 return; 57 return;
58 } 58 }
59 59
60 logger.info('Got result from partner', {result: result}); 60 logger.info('Got result from partner', {result: result});
61 61
62 var response_code = '68'; 62 var response_code = '68';
63 var message = result.trx_response.info[0].trim(); 63 var message = result.trx_response.info[0].trim();
64 64
65 if (message == 'Error Parsing') { 65 if (message == 'Error Parsing') {
66 66
67 response_code = '40'; 67 response_code = '40';
68 68
69 } else if (message == 'Stock is not available') { 69 } else if (message == 'Stock is not available') {
70 70
71 response_code = '40'; 71 response_code = '40';
72 72
73 } else if (message == 'Undefined error') { 73 } else if (message == 'Undefined error') {
74 74
75 response_code = '40'; 75 response_code = '40';
76 76
77 } else if (message == "Phone number's not found") { 77 } else if (message == "Phone number's not found") {
78 78
79 response_code = '14'; 79 response_code = '14';
80 80
81 } else if (message == "Phone number is expired") { 81 } else if (message == "Phone number is expired") {
82 82
83 response_code = '14'; 83 response_code = '14';
84 84
85 } else if (message == "Duplicate transaction") { 85 } else if (message == "Duplicate transaction") {
86 86
87 response_code = '55'; 87 response_code = '55';
88 88
89 } else if (message == "Link Provider down") { 89 } else if (message == "Link Provider down") {
90 90
91 response_code = '91'; 91 response_code = '91';
92 92
93 } else if (message == "Unable to route transaction") { 93 } else if (message == "Unable to route transaction") {
94 94
95 response_code = '91'; 95 response_code = '91';
96 96
97 } else if (message == "TRANSAKSI SUKSES !!!") { 97 } else if (message == "TRANSAKSI SUKSES !!!") {
98 var destination = result.trx_response.msisdn.join(' ').trim(); 98 var destination = result.trx_response.msisdn.join(' ').trim();
99 destination = destination.replace(' - ', ''); 99 destination = destination.replace(' - ', '');
100 var product = result.trx_response.product.join(' ').trim(); 100 var product = result.trx_response.product.join(' ').trim();
101 var ref_num = result.trx_response.ref_num.join(' ').trim(); 101 var ref_num = result.trx_response.ref_num.join(' ').trim();
102 var harga = result.trx_response.harga.join(' ').trim(); 102 var harga = result.trx_response.harga.join(' ').trim();
103 var kode_voucher = result.trx_response.kode_voucher.join(' ').trim(); 103 var kode_voucher = result.trx_response.kode_voucher.join(' ').trim();
104 var stocks = result.trx_response.stock.join(' ').trim(); 104 var stocks = result.trx_response.stock.join(' ').trim();
105 var stock = parseStock(stocks, product); 105 var stock = parseStock(stocks, product);
106 106
107 if (stock == 0) { 107 if (stock == 0) {
108 logger.info('OUT OF STOCK: ' + task['product']); 108 logger.info('OUT OF STOCK: ' + task['product']);
109 config.globals.products = productsWithout(task['product']); 109 config.globals.products = productsWithout(task['product']);
110 } 110 }
111 111
112 response_code = '00'; 112 response_code = '00';
113 113
114 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit'; 114 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit';
115 } 115 }
116 116
117 logger.info('Message to AAA: ' + message); 117 logger.info('Message to AAA: ' + message);
118 callbackReport(task['requestId'], response_code, message); 118 callbackReport(task['requestId'], response_code, message);
119 }); 119 });
120 }); 120 });
121 } 121 }
122 122
123 function start(_config, _callbackReport, options) { 123 function start(_config, _callbackReport, options) {
124 config = _config; 124 config = _config;
125 callbackReport = _callbackReport; 125 callbackReport = _callbackReport;
126 126
127 if (options && options.aaa) { 127 if (options && options.aaa) {
128 aaa = options.aaa; 128 aaa = options.aaa;
129 } 129 }
130 130
131 if (options && options.logger) { 131 if (options && options.logger) {
132 logger = options.logger; 132 logger = options.logger;
133 } else { 133 } else {
134 logger = new winston.Logger({ 134 logger = new winston.Logger({
135 transports: [ 135 transports: [
136 new (winston.transports.Console)() 136 new (winston.transports.Console)()
137 ] 137 ]
138 }); 138 });
139 } 139 }
140 } 140 }
141 141
142 function stockKeyword(product_desc) { 142 function stockKeyword(product_desc) {
143 var keywords = product_desc.match(/\s\d+K$/); 143 var keywords = product_desc.match(/\s\d+K$/);
144 if (!keywords) { 144 if (!keywords) {
145 return; 145 return;
146 } 146 }
147 147
148 if (keywords.length < 1) { 148 if (keywords.length < 1) {
149 return; 149 return;
150 } 150 }
151 151
152 return keywords[0].trim(); 152 return keywords[0].trim();
153 } 153 }
154 154
155 function parseStock(stocks, product_desc) { 155 function parseStock(stocks, product_desc) {
156 product_keyword = stockKeyword(product_desc); 156 product_keyword = stockKeyword(product_desc);
157 157
158 var _stocks = stocks.split('; '); 158 var _stocks = stocks.split('; ');
159 count = _stocks.length; 159 count = _stocks.length;
160 for (var i = 0; i < count; i++) { 160 for (var i = 0; i < count; i++) {
161 var stock = _stocks[i].split(' = '); 161 var stock = _stocks[i].split(' = ');
162 if (stock.length < 2) { 162 if (stock.length < 2) {
163 continue; 163 continue;
164 } 164 }
165 165
166 if (stock[0] == product_keyword) { 166 if (stock[0] == product_keyword) {
167 var value = stock[1].replace(/[,\.]/g, ''); 167 var value = stock[1].replace(/[,\.]/g, '');
168 return parseInt(value); 168 return parseInt(value);
169 } 169 }
170 } 170 }
171 171
172 return 0; 172 return 0;
173 } 173 }
174 174
175 function productsWithout(to_be_removed, products) { 175 function productsWithout(to_be_removed, products) {
176 if (products === undefined) { 176 if (products === undefined) {
177 products = config.globals.products; 177 products = config.globals.products;
178 } 178 }
179 179
180 if (products.search(/^,/) == -1) { 180 if (products.search(/^,/) == -1) {
181 products = ',' + products; 181 products = ',' + products;
182 } 182 }
183 183
184 184
185 if (products.search(/,$/) == -1) { 185 if (products.search(/,$/) == -1) {
186 products = products + ','; 186 products = products + ',';
187 } 187 }
188 188
189 products = products.replace(/\s+/, ''); 189 products = products.replace(/\s+/, '');
190 products = products.replace(/,+/, ','); 190 products = products.replace(/,+/, ',');
191 products = products.replace(',' + to_be_removed + ',' , ','); 191 products = products.replace(',' + to_be_removed + ',' , ',');
192 products = products.replace(/^,/, ''); 192 products = products.replace(/^,/, '');
193 products = products.replace(/,$/, ''); 193 products = products.replace(/,$/, '');
194 194
195 return products; 195 return products;
196 } 196 }
197 197
198 exports.start = start; 198 exports.start = start;
199 exports.topupRequest = topupRequest; 199 exports.topupRequest = topupRequest;
200 exports.stockKeyword = stockKeyword; 200 exports.stockKeyword = stockKeyword;
201 exports.parseStock = parseStock; 201 exports.parseStock = parseStock;
202 exports.productsWithout = productsWithout; 202 exports.productsWithout = productsWithout;
203 203