Commit 9bc606946ef40cac590a3234488cfcf96b7b799d
1 parent
da1b3450cc
Exists in
master
perbaikan rc
Showing 1 changed file with 3 additions and 2 deletions Inline Diff
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 | var xml2jsParser = require('xml2js').parseString; |
5 | 5 | ||
6 | var config; | 6 | var config; |
7 | var aaa; | 7 | var aaa; |
8 | var callbackReport; | 8 | var callbackReport; |
9 | var logger; | 9 | var logger; |
10 | 10 | ||
11 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | 11 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; |
12 | 12 | ||
13 | function start(_config, _callbackReport, options) { | 13 | function start(_config, _callbackReport, options) { |
14 | config = _config; | 14 | config = _config; |
15 | callbackReport = _callbackReport; | 15 | callbackReport = _callbackReport; |
16 | 16 | ||
17 | if (options && options.aaa) { | 17 | if (options && options.aaa) { |
18 | aaa = options.aaa; | 18 | aaa = options.aaa; |
19 | } | 19 | } |
20 | 20 | ||
21 | if (options && options.logger) { | 21 | if (options && options.logger) { |
22 | logger = options.logger; | 22 | logger = options.logger; |
23 | } else { | 23 | } else { |
24 | logger = new winston.Logger({ | 24 | logger = new winston.Logger({ |
25 | transports: [ | 25 | transports: [ |
26 | new (winston.transports.Console)() | 26 | new (winston.transports.Console)() |
27 | ] | 27 | ] |
28 | }); | 28 | }); |
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | 32 | ||
33 | function topupRequest(task, retry) { | 33 | function topupRequest(task, retry) { |
34 | aaa.insertTaskToMongoDb(task); | 34 | aaa.insertTaskToMongoDb(task); |
35 | 35 | ||
36 | var partnerUrl = url.parse(config.h2h_out.partner); | 36 | var partnerUrl = url.parse(config.h2h_out.partner); |
37 | var product = prepareRemoteProductCode(task.remoteProduct); | 37 | var product = prepareRemoteProductCode(task.remoteProduct); |
38 | 38 | ||
39 | var options = { | 39 | var options = { |
40 | url: config.h2h_out.partner, | 40 | url: config.h2h_out.partner, |
41 | qs: { | 41 | qs: { |
42 | request: 'PURCHASE*' | 42 | request: 'PURCHASE*' |
43 | + task.requestId + '*' | 43 | + task.requestId + '*' |
44 | + product.product + '*' | 44 | + product.product + '*' |
45 | + product.productDetail + '*' | 45 | + product.productDetail + '*' |
46 | + task.destination + '*' | 46 | + task.destination + '*' |
47 | + product.nominal + '*' | 47 | + product.nominal + '*' |
48 | + '0*' | 48 | + '0*' |
49 | + config.h2h_out.noid + '*' | 49 | + config.h2h_out.noid + '*' |
50 | + config.h2h_out.userid + '*' | 50 | + config.h2h_out.userid + '*' |
51 | + config.h2h_out.password | 51 | + config.h2h_out.password |
52 | } | 52 | } |
53 | }; | 53 | }; |
54 | 54 | ||
55 | logger.verbose('Requesting to partner', {requestOption: options}); | 55 | logger.verbose('Requesting to partner', {requestOption: options}); |
56 | request(options, function(error, response, body) { | 56 | request(options, function(error, response, body) { |
57 | if (error) { | 57 | if (error) { |
58 | logger.warn('Error requesting to partner', {error: error}); | 58 | logger.warn('Error requesting to partner', {error: error}); |
59 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); | 59 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); |
60 | return; | 60 | return; |
61 | } | 61 | } |
62 | 62 | ||
63 | if (response.statusCode != 200) { | 63 | if (response.statusCode != 200) { |
64 | 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 +')'; |
65 | 65 | ||
66 | logger.warn(message); | 66 | logger.warn(message); |
67 | callbackReport(task.requestId, '68', message); | 67 | callbackReport(task.requestId, '68', message); |
68 | return; | 68 | return; |
69 | } | 69 | } |
70 | 70 | ||
71 | /* | 71 | /* |
72 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); | 72 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); |
73 | callbackReport(task.requestId, '68', body); | 73 | callbackReport(task.requestId, '68', body); |
74 | */ | 74 | */ |
75 | 75 | ||
76 | logger.verbose('Got respose', {rawBody: body}); | ||
76 | parseResponse(body, task); | 77 | parseResponse(body, task); |
77 | }); | 78 | }); |
78 | } | 79 | } |
79 | 80 | ||
80 | function parseResponse(body, task) { | 81 | function parseResponse(body, task) { |
81 | xml2jsParser(body, function(err, response) { | 82 | xml2jsParser(body, function(err, response) { |
82 | if (err) { | 83 | if (err) { |
83 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); | 84 | logger.warn('Error parsing XML', {error: err, task: task, responseBody: body}); |
84 | 85 | ||
85 | var message = 'Error parsing XML. ' + err + '. ' + body; | 86 | var message = 'Error parsing XML. ' + err + '. ' + body; |
86 | callbackReport(task.requestId, '68', message); | 87 | callbackReport(task.requestId, '68', message); |
87 | return; | 88 | return; |
88 | } | 89 | } |
89 | 90 | ||
90 | logger.info('Got response', {response: response}); | 91 | logger.info('Got response', {response: response}); |
91 | 92 | ||
92 | var responseCode = '68'; | 93 | var responseCode = '68'; |
93 | var message = ''; | 94 | var message = ''; |
94 | 95 | ||
95 | try { | 96 | try { |
96 | responseCode = response.xml.response_code[0]; | 97 | responseCode = response.xml.response_code[0]; |
97 | saldo = response.xml.saldo[0]; | 98 | saldo = response.xml.saldo[0]; |
98 | message = response.xml.response_message[0]; | 99 | message = response.xml.response_message[0]; |
99 | 100 | ||
100 | var st24rc = '40'; | 101 | var st24rc = '40'; |
101 | if (responseCode == '00') { | 102 | if (parseInt(responseCode) == 0) { |
102 | st24rc = '00'; | 103 | st24rc = '00'; |
103 | } | 104 | } |
104 | else if (responseCode == '99') { | 105 | else if (parseInt(responseCode) == '99') { |
105 | st24rc = '68' | 106 | st24rc = '68' |
106 | } | 107 | } |
107 | else { | 108 | else { |
108 | st24rc = '40'; | 109 | st24rc = '40'; |
109 | } | 110 | } |
110 | 111 | ||
111 | var st24message = responseCode + ' ' + message + '. Saldo ' + saldo; | 112 | var st24message = responseCode + ' ' + message + '. Saldo ' + saldo; |
112 | 113 | ||
113 | callbackReport(task.requestId, st24rc, st24message); | 114 | callbackReport(task.requestId, st24rc, st24message); |
114 | } | 115 | } |
115 | catch(errGetParam) { | 116 | catch(errGetParam) { |
116 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); | 117 | logger.warn('Exception saat parsing hasil', {error: errGetParam, task: task, responseBody: body}); |
117 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); | 118 | callbackReport(task.requestId, '68', 'Exception saat parsing hasil. ' + errGetParam); |
118 | } | 119 | } |
119 | 120 | ||
120 | }); | 121 | }); |
121 | } | 122 | } |
122 | 123 | ||
123 | function prepareRemoteProductCode(remoteProduct) { | 124 | function prepareRemoteProductCode(remoteProduct) { |
124 | var product = remoteProduct.split(','); | 125 | var product = remoteProduct.split(','); |
125 | 126 | ||
126 | if (product.length != 3) { | 127 | if (product.length != 3) { |
127 | return; | 128 | return; |
128 | } | 129 | } |
129 | 130 | ||
130 | return { | 131 | return { |
131 | product: product[0], | 132 | product: product[0], |
132 | productDetail: product[1], | 133 | productDetail: product[1], |
133 | nominal: product[2] | 134 | nominal: product[2] |
134 | } | 135 | } |
135 | } | 136 | } |
136 | 137 | ||
137 | exports.start = start; | 138 | exports.start = start; |
138 | exports.topupRequest = topupRequest; | 139 | exports.topupRequest = topupRequest; |
139 | exports.prepareRemoteProductCode = prepareRemoteProductCode; | 140 | exports.prepareRemoteProductCode = prepareRemoteProductCode; |
140 | 141 |