Commit 039a296f2796c143a3059d51522088d4b273bf66

Authored by Adhidarma Hadiwinoto
1 parent f47e9cf023
Exists in master

parsing xml

Showing 2 changed files with 21 additions and 1 deletions Inline Diff

1 { 1 {
2 "name": "sate24-to-bayarkilat", 2 "name": "sate24-to-bayarkilat",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 "description": "ST24 ke BayarKilat.com", 4 "description": "ST24 ke BayarKilat.com",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha" 7 "test": "mocha"
8 }, 8 },
9 "repository": { 9 "repository": {
10 "type": "git", 10 "type": "git",
11 "url": "git@gitlab.kodesumber.com:reload97/sate24-to-bayarkilat.git" 11 "url": "git@gitlab.kodesumber.com:reload97/sate24-to-bayarkilat.git"
12 }, 12 },
13 "keywords": [ 13 "keywords": [
14 "st24", 14 "st24",
15 "ppob", 15 "ppob",
16 "reload97", 16 "reload97",
17 "r97", 17 "r97",
18 "bayarkilat" 18 "bayarkilat"
19 ], 19 ],
20 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", 20 "author": "Adhidarma Hadiwinoto <me@adhisimon.org>",
21 "license": "ISC", 21 "license": "ISC",
22 "dependencies": { 22 "dependencies": {
23 "request": "^2.72.0", 23 "request": "^2.72.0",
24 "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", 24 "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git",
25 "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git" 25 "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git",
26 "xml2js": "^0.4.16"
26 }, 27 },
27 "devDependencies": { 28 "devDependencies": {
28 "should": "^9.0.2" 29 "should": "^9.0.2"
29 } 30 }
30 } 31 }
31 32
partner-bayarkilat.js
1 var request = require('request'); 1 var request = require('request');
2 var url = require('url'); 2 var url = require('url');
3 var winston = require('winston'); 3 var winston = require('winston');
4 var xml2jsParser = require('xml2js').parseString;
4 5
5 var config; 6 var config;
6 var aaa; 7 var aaa;
7 var callbackReport; 8 var callbackReport;
8 var logger; 9 var logger;
9 10
10 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 11 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
11 12
12 function start(_config, _callbackReport, options) { 13 function start(_config, _callbackReport, options) {
13 config = _config; 14 config = _config;
14 callbackReport = _callbackReport; 15 callbackReport = _callbackReport;
15 16
16 if (options && options.aaa) { 17 if (options && options.aaa) {
17 aaa = options.aaa; 18 aaa = options.aaa;
18 } 19 }
19 20
20 if (options && options.logger) { 21 if (options && options.logger) {
21 logger = options.logger; 22 logger = options.logger;
22 } else { 23 } else {
23 logger = new winston.Logger({ 24 logger = new winston.Logger({
24 transports: [ 25 transports: [
25 new (winston.transports.Console)() 26 new (winston.transports.Console)()
26 ] 27 ]
27 }); 28 });
28 } 29 }
29 } 30 }
30 31
31 32
32 function topupRequest(task, retry) { 33 function topupRequest(task, retry) {
33 aaa.insertTaskToMongoDb(task); 34 aaa.insertTaskToMongoDb(task);
34 35
35 var partnerUrl = url.parse(config.h2h_out.partner); 36 var partnerUrl = url.parse(config.h2h_out.partner);
36 var product = prepareRemoteProductCode(task.remoteProduct); 37 var product = prepareRemoteProductCode(task.remoteProduct);
37 38
38 var options = { 39 var options = {
39 url: config.h2h_out.partner, 40 url: config.h2h_out.partner,
40 qs: { 41 qs: {
41 request: 'PURCHASE*' 42 request: 'PURCHASE*'
42 + task.requestId + '*' 43 + task.requestId + '*'
43 + product.product + '*' 44 + product.product + '*'
44 + product.productDetail + '*' 45 + product.productDetail + '*'
45 + task.destination + '*' 46 + task.destination + '*'
46 + product.nominal + '*' 47 + product.nominal + '*'
47 + '0*' 48 + '0*'
48 + config.h2h_out.noid + '*' 49 + config.h2h_out.noid + '*'
49 + config.h2h_out.userid + '*' 50 + config.h2h_out.userid + '*'
50 + config.h2h_out.password 51 + config.h2h_out.password
51 } 52 }
52 }; 53 };
53 54
54 logger.verbose('Requesting to partner', {requestOption: options}); 55 logger.verbose('Requesting to partner', {requestOption: options});
55 request(options, function(error, response, body) { 56 request(options, function(error, response, body) {
56 if (error) { 57 if (error) {
57 logger.warn('Error requesting to partner', {error: error}); 58 logger.warn('Error requesting to partner', {error: error});
58 callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); 59 callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error);
59 return; 60 return;
60 } 61 }
61 62
62 if (response.statusCode != 200) { 63 if (response.statusCode != 200) {
63 var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; 64 var message = 'Partner response with http status code other that 200 (' + response.statusCode +')';
64 65
65 logger.warn(message); 66 logger.warn(message);
66 callbackReport(task.requestId, '68', message); 67 callbackReport(task.requestId, '68', message);
67 return; 68 return;
68 } 69 }
69 70
71 /*
70 logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); 72 logger.verbose('Got response', {requestId: task.requestId, responseBody: body});
71 callbackReport(task.requestId, '68', body); 73 callbackReport(task.requestId, '68', body);
74 */
75
76 parseResponse(body, task);
77 });
78 }
79
80 function parseResponse(body, task) {
81 xml2jsParser(body, function(err, response) {
82 if (err) {
83 logger.warn('Error parsing XML', {error: err, task: task, responseBody: body});
84
85 var message = 'Error parsing XML. ' + err + '. ' + body;
86 callbackReport(task.requestId, '68', message);
87 return;
88 }
89
90 logger.info('Got response', {response: response});
72 }); 91 });
73 } 92 }
74 93
75 function prepareRemoteProductCode(remoteProduct) { 94 function prepareRemoteProductCode(remoteProduct) {
76 var product = remoteProduct.split(','); 95 var product = remoteProduct.split(',');
77 96
78 if (product.length != 3) { 97 if (product.length != 3) {
79 return; 98 return;
80 } 99 }
81 100
82 return { 101 return {
83 product: product[0], 102 product: product[0],
84 productDetail: product[1], 103 productDetail: product[1],
85 nominal: product[2] 104 nominal: product[2]
86 } 105 }
87 } 106 }
88 107
89 exports.start = start; 108 exports.start = start;
90 exports.topupRequest = topupRequest; 109 exports.topupRequest = topupRequest;
91 exports.prepareRemoteProductCode = prepareRemoteProductCode; 110 exports.prepareRemoteProductCode = prepareRemoteProductCode;
92 111