Commit 5909d674dd3136c5ae254d24bc643a4416916bb3
1 parent
bc22efa206
Exists in
master
logging
Showing 1 changed file with 1 additions and 0 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 | 4 | ||
5 | var config; | 5 | var config; |
6 | var aaa; | 6 | var aaa; |
7 | var callbackReport; | 7 | var callbackReport; |
8 | var logger; | 8 | var logger; |
9 | 9 | ||
10 | function start(_config, _callbackReport, options) { | 10 | function start(_config, _callbackReport, options) { |
11 | config = _config; | 11 | config = _config; |
12 | callbackReport = _callbackReport; | 12 | callbackReport = _callbackReport; |
13 | 13 | ||
14 | if (options && options.aaa) { | 14 | if (options && options.aaa) { |
15 | aaa = options.aaa; | 15 | aaa = options.aaa; |
16 | } | 16 | } |
17 | 17 | ||
18 | if (options && options.logger) { | 18 | if (options && options.logger) { |
19 | logger = options.logger; | 19 | logger = options.logger; |
20 | } else { | 20 | } else { |
21 | logger = new winston.Logger({ | 21 | logger = new winston.Logger({ |
22 | transports: [ | 22 | transports: [ |
23 | new (winston.transports.Console)() | 23 | new (winston.transports.Console)() |
24 | ] | 24 | ] |
25 | }); | 25 | }); |
26 | } | 26 | } |
27 | } | 27 | } |
28 | 28 | ||
29 | 29 | ||
30 | function topupRequest(task, retry) { | 30 | function topupRequest(task, retry) { |
31 | aaa.insertTaskToMongoDb(task); | 31 | aaa.insertTaskToMongoDb(task); |
32 | 32 | ||
33 | var partnerUrl = url.parse(config.h2h_out.partner); | 33 | var partnerUrl = url.parse(config.h2h_out.partner); |
34 | var product = prepareRemoteProductCode(task.remoteProduct); | 34 | var product = prepareRemoteProductCode(task.remoteProduct); |
35 | 35 | ||
36 | var options = { | 36 | var options = { |
37 | url: url: config.h2h_out.partner, | 37 | url: url: config.h2h_out.partner, |
38 | qs: { | 38 | qs: { |
39 | request: 'PURCHASE*' | 39 | request: 'PURCHASE*' |
40 | + task.requestId + '*' | 40 | + task.requestId + '*' |
41 | + product.product + '*' | 41 | + product.product + '*' |
42 | + product.productDetail + '*' | 42 | + product.productDetail + '*' |
43 | + task.destination + '*' | 43 | + task.destination + '*' |
44 | + task.nominal + '*' | 44 | + task.nominal + '*' |
45 | + '0*' | 45 | + '0*' |
46 | + config.h2h_out.noid + '*' | 46 | + config.h2h_out.noid + '*' |
47 | + config.h2h_out.userid + '*' | 47 | + config.h2h_out.userid + '*' |
48 | + config.h2h_out.password | 48 | + config.h2h_out.password |
49 | } | 49 | } |
50 | }; | 50 | }; |
51 | 51 | ||
52 | logger.verbose('Requesting to partner', {requestOption: options}); | ||
52 | request(options, function(error, response, body) { | 53 | request(options, function(error, response, body) { |
53 | if (error) { | 54 | if (error) { |
54 | logger.warn('Error requesting to partner', {error: error}); | 55 | logger.warn('Error requesting to partner', {error: error}); |
55 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); | 56 | callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); |
56 | return; | 57 | return; |
57 | } | 58 | } |
58 | 59 | ||
59 | if (response.statusCode != 200) { | 60 | if (response.statusCode != 200) { |
60 | var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; | 61 | var message = 'Partner response with http status code other that 200 (' + response.statusCode +')'; |
61 | 62 | ||
62 | logger.warn(message); | 63 | logger.warn(message); |
63 | callbackReport(task.requestId, '68', message); | 64 | callbackReport(task.requestId, '68', message); |
64 | return; | 65 | return; |
65 | } | 66 | } |
66 | 67 | ||
67 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); | 68 | logger.verbose('Got response', {requestId: task.requestId, responseBody: body}); |
68 | callbackReport(task.requestId, '68', body); | 69 | callbackReport(task.requestId, '68', body); |
69 | }); | 70 | }); |
70 | } | 71 | } |
71 | 72 | ||
72 | function prepareRemoteProductCode(remoteProduct) { | 73 | function prepareRemoteProductCode(remoteProduct) { |
73 | var product = remoteProduct.split(','); | 74 | var product = remoteProduct.split(','); |
74 | 75 | ||
75 | if (product.length != 3) { | 76 | if (product.length != 3) { |
76 | return; | 77 | return; |
77 | } | 78 | } |
78 | 79 | ||
79 | return { | 80 | return { |
80 | product: product[0], | 81 | product: product[0], |
81 | productDetail: product[1], | 82 | productDetail: product[1], |
82 | nominal: product[2] | 83 | nominal: product[2] |
83 | } | 84 | } |
84 | } | 85 | } |
85 | 86 | ||
86 | exports.start = start; | 87 | exports.start = start; |
87 | exports.topupRequest = topupRequest; | 88 | exports.topupRequest = topupRequest; |
88 | exports.prepareRemoteProductCode = prepareRemoteProductCode; | 89 | exports.prepareRemoteProductCode = prepareRemoteProductCode; |
89 | 90 |