Commit 2ab9df305f41c558cf849297dc71c62e23375f84
1 parent
e00a4632fb
Exists in
master
ready to test
Showing 5 changed files with 206 additions and 0 deletions Side-by-side Diff
.gitignore
README
index.js
... | ... | @@ -0,0 +1,31 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +var aaa = require('sate24/aaa.js'); | |
4 | +var expresso = require('sate24-expresso'); | |
5 | +var logger = require('sate24/logger.js').start(); | |
6 | +var HttpServer = require('sate24/httpserver.js'); | |
7 | +var partner = require('./partner-komodo.js'); | |
8 | + | |
9 | +var fs = require('fs'); | |
10 | +var ini = require('ini'); | |
11 | +var config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); | |
12 | + | |
13 | +process.chdir(__dirname); | |
14 | + | |
15 | +var logDirectory = __dirname + '/logs'; | |
16 | +fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory); | |
17 | + | |
18 | +var matrix = aaa.prepareMatrix(); | |
19 | + | |
20 | +var options = { | |
21 | + 'aaa': aaa, | |
22 | + 'logger': logger, | |
23 | + 'config': config, | |
24 | + 'matrix': matrix, | |
25 | +} | |
26 | + | |
27 | +var httpServer = HttpServer.start(config, options); | |
28 | + | |
29 | +partner.start(options); | |
30 | +aaa.start(config, partner, options); | |
31 | +expresso.start(options); |
package.json
... | ... | @@ -0,0 +1,28 @@ |
1 | +{ | |
2 | + "name": "sate24-to-komodo", | |
3 | + "version": "1.0.0", | |
4 | + "description": "ST24 Gateway to KOMODO", | |
5 | + "main": "index.js", | |
6 | + "scripts": { | |
7 | + "test": "mocha" | |
8 | + }, | |
9 | + "repository": { | |
10 | + "type": "git", | |
11 | + "url": "git@gitlab.kodesumber.com:reload97/sate24-to-komodo.git" | |
12 | + }, | |
13 | + "keywords": [ | |
14 | + "ppob", | |
15 | + "st24", | |
16 | + "payment", | |
17 | + "reload97", | |
18 | + "r97", | |
19 | + "komodo" | |
20 | + ], | |
21 | + "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", | |
22 | + "license": "ISC", | |
23 | + "dependencies": { | |
24 | + "request": "^2.81.0", | |
25 | + "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", | |
26 | + "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git" | |
27 | + } | |
28 | +} |
partner-komodo.js
... | ... | @@ -0,0 +1,142 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const request = require('request'); | |
4 | +const resendDelay = require('sate24/resend-delay') | |
5 | + | |
6 | +var config; | |
7 | +var aaa; | |
8 | +var logger; | |
9 | + | |
10 | +let komodoRc = { | |
11 | + '03': '40', | |
12 | + '13': '13', | |
13 | + '14': '14', | |
14 | + '30': '40', | |
15 | + '68': '68', | |
16 | + '55': '55', | |
17 | + '91': '91', | |
18 | + '92': '40', | |
19 | + '96': '68' | |
20 | +} | |
21 | + | |
22 | +function start(options) { | |
23 | + if (!options) { | |
24 | + console.log('Undefined options, terminating....'); | |
25 | + process.exit(1); | |
26 | + } | |
27 | + | |
28 | + if (options.config) { | |
29 | + config = options.config; | |
30 | + } else { | |
31 | + console.log('Undefined options.config, terminating....') | |
32 | + process.exit(1); | |
33 | + } | |
34 | + | |
35 | + if (options.aaa) { | |
36 | + aaa = options.aaa; | |
37 | + } else { | |
38 | + console.log('Undefined options.aaa, terminating....') | |
39 | + process.exit(1); | |
40 | + } | |
41 | + | |
42 | + if (options && options.logger) { | |
43 | + logger = options.logger; | |
44 | + } else { | |
45 | + console.log('Undefined options.logger, terminating....') | |
46 | + process.exit(1); | |
47 | + } | |
48 | + | |
49 | + resendDelay.init({config: config, logger: logger, topupRequest: topupAdvice}); | |
50 | +} | |
51 | + | |
52 | +function callbackReport(requestId, rc, message, options) { | |
53 | + aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | |
54 | + | |
55 | + if (!options.task) { | |
56 | + return; | |
57 | + } | |
58 | + | |
59 | + if (rc == '68') { | |
60 | + resendDelay.register(options.task); | |
61 | + } else { | |
62 | + resendDelay.cancel(options.task) | |
63 | + } | |
64 | + | |
65 | +} | |
66 | + | |
67 | +function topupRequest(task, pendingOnConnectError) { | |
68 | + aaa.insertTaskToMongoDb(task); | |
69 | + | |
70 | + const requestOptions = { | |
71 | + url: config.h2h_out.partner, | |
72 | + qs: { | |
73 | + terminal_name: config.h2h_out.username, | |
74 | + password: config.h2h_out.password, | |
75 | + product_name: task.remoteProduct, | |
76 | + destination: task.destination, | |
77 | + request_id: task.requestId | |
78 | + } | |
79 | + } | |
80 | + | |
81 | + request(requestOptions, function(err, response, body) { | |
82 | + if (err) { | |
83 | + logger.warn('Error requesting to partner', {err: err}); | |
84 | + | |
85 | + let rc = '68'; | |
86 | + | |
87 | + if (err.syscall == 'connect' && !pendingOnConnectError) { | |
88 | + rc = '91'; | |
89 | + } | |
90 | + callbackReport(task.requestId, rc, 'Error requesting to partner: ' + err); | |
91 | + return; | |
92 | + } | |
93 | + | |
94 | + if (response.statusCode != 200) { | |
95 | + let rc = '68'; | |
96 | + | |
97 | + callbackReport(task.requestId, rc, 'Partner returning HTTP status code ' + response.statusCode + ', not 200'); | |
98 | + return; | |
99 | + } | |
100 | + | |
101 | + let result = parsePartnerMessage(body); | |
102 | + if (!result) { | |
103 | + callbackReport(task.requestId, '40', 'Error parsing response from partner. Partner response: ' + body); | |
104 | + return; | |
105 | + } | |
106 | + | |
107 | + let st24Rc = '68'; | |
108 | + | |
109 | + if (komodoRc[result.rc]) { | |
110 | + st24Rc = komodoRc[result.rc]; | |
111 | + } | |
112 | + | |
113 | + let st24Message = result.message; | |
114 | + if (result.sn) { | |
115 | + st24Message = 'SN=' + result.sn + '; ' + st24Message; | |
116 | + } | |
117 | + | |
118 | + callbackReport(task.requestId, st24Rc, st24Message); | |
119 | + }) | |
120 | +} | |
121 | + | |
122 | +function topupAdvice(task) { | |
123 | + topupRequest(task, true); | |
124 | +} | |
125 | + | |
126 | +function parsePartnerMessage(partner_message) { | |
127 | + let result; | |
128 | + try { | |
129 | + result = JSON.parse(partner_message); | |
130 | + | |
131 | + } | |
132 | + catch(e) { | |
133 | + result = null; | |
134 | + } | |
135 | + | |
136 | + return result; | |
137 | +} | |
138 | + | |
139 | + | |
140 | +exports.start = start; | |
141 | +exports.topupRequest = topupRequest; | |
142 | +exports.topupAdvice = topupAdvice; |