Commit 6c417c09c4ea32761f58d12e81ce6d5c9eb4a34b
1 parent
d305755a4e
Exists in
master
ready to test
Showing 6 changed files with 252 additions and 0 deletions Side-by-side Diff
.gitignore
adaptor-xmpp.js
... | ... | @@ -0,0 +1,50 @@ |
1 | +var xmpp = require('simple-xmpp'); | |
2 | + | |
3 | +var username; | |
4 | +var password; | |
5 | + | |
6 | +var callbacks; | |
7 | + | |
8 | + | |
9 | +function onLoginSuccessful(data) { | |
10 | + logger.info('XMPP login successful', {data: data}); | |
11 | + if (callbacks.onLoginSuccessful) { | |
12 | + callbacks.onLoginSuccessful(); | |
13 | + } | |
14 | +} | |
15 | + | |
16 | +function onPM(sender, msg) { | |
17 | + logger.verbose('Got a message', {from: sender, msg: msg}) | |
18 | + if (callbacks.onPM) { | |
19 | + callbacks.onPM(sender, msg); | |
20 | + } | |
21 | +} | |
22 | + | |
23 | +function onSubscribe(sender) { | |
24 | + xmpp.acceptSubscription(sender); | |
25 | +} | |
26 | + | |
27 | +function init(_username, _password, _logger, _callbacks) { | |
28 | + username = _username; | |
29 | + password = _password; | |
30 | + logger = _logger; | |
31 | + callbacks = _callbacks; | |
32 | + | |
33 | + xmpp.on('online', onLoginSuccessful); | |
34 | + xmpp.on('chat', onPM); | |
35 | + xmpp.on('error', onError); | |
36 | + xmpp.on('subscribe', onSubscribe) | |
37 | + | |
38 | + xmpp.connect({ | |
39 | + jid: username, | |
40 | + password: password | |
41 | + }); | |
42 | +} | |
43 | + | |
44 | +function sendMessage(destination, msg) { | |
45 | + logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); | |
46 | + xmpp.send(destination, msg); | |
47 | +} | |
48 | + | |
49 | +exports.init = init; | |
50 | +exports.sendMessage = sendMessage; |
index.js
... | ... | @@ -0,0 +1,30 @@ |
1 | +var fs = require('fs'); | |
2 | +var ini = require('ini'); | |
3 | +var expresso = require('sate24-expresso'); | |
4 | +var partner = require('./partner-xmpp'); | |
5 | +var config = ini.parse(fs.readFileSync(__dirname + '/config.ini', 'utf-8')); | |
6 | + | |
7 | +process.chdir(__dirname); | |
8 | + | |
9 | +var logger = require('sate24/logger.js').start(); | |
10 | +var HttpServer = require('sate24/httpserver.js'); | |
11 | +var aaa = require('sate24/aaa.js'); | |
12 | +var partner = require('./partner-ym.js'); | |
13 | + | |
14 | +var matrix = aaa.prepareMatrix(); | |
15 | + | |
16 | +var options = { | |
17 | + 'aaa': aaa, | |
18 | + 'logger': logger, | |
19 | + 'config': config, | |
20 | + 'matrix': matrix, | |
21 | + 'partner': partner, | |
22 | +} | |
23 | + | |
24 | +var httpServer = HttpServer.start(config, options); | |
25 | + | |
26 | +aaa.pause(); | |
27 | + | |
28 | +partner.start(options); | |
29 | +aaa.start(config, partner, options); | |
30 | +expresso.start(options); |
logs/empty
package.json
... | ... | @@ -0,0 +1,28 @@ |
1 | +{ | |
2 | + "name": "sate24-to-xmpp", | |
3 | + "version": "1.0.0", | |
4 | + "description": "ST24 Jabber/XMPP H2H-Out", | |
5 | + "main": "index.js", | |
6 | + "scripts": { | |
7 | + "test": "mocha" | |
8 | + }, | |
9 | + "repository": { | |
10 | + "type": "git", | |
11 | + "url": "git@gitlab.kodesumber.com:reload97/sate24-to-xmpp.git" | |
12 | + }, | |
13 | + "keywords": [ | |
14 | + "jabber", | |
15 | + "xmpp", | |
16 | + "st24", | |
17 | + "ppob", | |
18 | + "reload97", | |
19 | + "r97" | |
20 | + ], | |
21 | + "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", | |
22 | + "license": "ISC", | |
23 | + "dependencies": { | |
24 | + "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", | |
25 | + "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git", | |
26 | + "simple-xmpp": "^1.3.0" | |
27 | + } | |
28 | +} |
partner-xmpp.js
... | ... | @@ -0,0 +1,140 @@ |
1 | +var im = require('sate24/im.js') | |
2 | +var imAdaptor = require('./adaptor-xmpp'); | |
3 | + | |
4 | +var config; | |
5 | +var aaa; | |
6 | +var logger; | |
7 | +var callbackReport; | |
8 | + | |
9 | +function onLoginSuccessful() { | |
10 | + logger.info('Login successful, resuming aaa communication'); | |
11 | + aaa.resume(); | |
12 | +} | |
13 | + | |
14 | +function onPM(from, msg) { | |
15 | + | |
16 | + if (!im.isAllowedFrom(from)) { | |
17 | + logger.info('Ignoring message from unknown sender', {from: from, msg: msg}); | |
18 | + return; | |
19 | + } | |
20 | + | |
21 | + var remoteProduct = im.getRemoteProductFromMessage(msg); | |
22 | + var destination = im.getDestinationFromMessage(msg); | |
23 | + | |
24 | + if (!remoteProduct && !destination) { | |
25 | + logger.warn('Missing remote product or destination', {remoteProduct: remoteProduct, destination: destination, msg: msg}); | |
26 | + return; | |
27 | + } | |
28 | + | |
29 | + logger.info('Got report from partner', {remoteProduct: remoteProduct, destination: destination, msg: msg}); | |
30 | + im.getTask(remoteProduct, destination, function(err, task) { | |
31 | + if (err) { | |
32 | + logger.warn('Error getting relevant task'); | |
33 | + return; | |
34 | + } | |
35 | + | |
36 | + if (!task) { | |
37 | + logger.warn('Something wrong, undefined task without error') | |
38 | + return; | |
39 | + } | |
40 | + | |
41 | + logger.verbose('Got relevant task', {task: task, msg: msg}); | |
42 | + var rc = im.getRcFromMessage(msg); | |
43 | + if (rc == '00') { | |
44 | + var sn = im.getSnFromMessage(msg); | |
45 | + if (sn) { | |
46 | + msg = 'SN=' + sn + ';' + msg; | |
47 | + } | |
48 | + } | |
49 | + | |
50 | + if (['00', '55', '68'].indexOf(rc) == -1) { | |
51 | + im.deleteTask(remoteProduct, destination); | |
52 | + } | |
53 | + | |
54 | + if (rc != '68') { | |
55 | + im.cancelResendDelay(task); | |
56 | + } | |
57 | + | |
58 | + callbackReport(task.requestId, rc, msg); | |
59 | + }); | |
60 | +} | |
61 | + | |
62 | +function start(options) { | |
63 | + if (options && options.config) { | |
64 | + config = options.config; | |
65 | + } else { | |
66 | + console.log('Unknown options.config'); | |
67 | + process.exit('1'); | |
68 | + } | |
69 | + | |
70 | + if (options && options.aaa) { | |
71 | + aaa = options.aaa; | |
72 | + } | |
73 | + | |
74 | + if (options && options.aaa && options.aaa.callbackReport) { | |
75 | + callbackReport = options.aaa.callbackReport; | |
76 | + } else { | |
77 | + console.log('Unknown options.aaa.callbackReport') | |
78 | + process.exit(2); | |
79 | + } | |
80 | + | |
81 | + if (options && options.logger) { | |
82 | + logger = options.logger; | |
83 | + } else { | |
84 | + logger = new winston.Logger({ | |
85 | + transports: [ | |
86 | + new (winston.transports.Console)() | |
87 | + ] | |
88 | + }); | |
89 | + } | |
90 | + | |
91 | + var callbacks = { | |
92 | + onLoginSuccessful: onLoginSuccessful, | |
93 | + onPM: onPM, | |
94 | + } | |
95 | + | |
96 | + im.init(options); | |
97 | + imAdaptor.init(config.h2h_out.ym_id, config.h2h_out.ym_password, logger, callbacks); | |
98 | +} | |
99 | + | |
100 | +function onSameDayDupe(task, archivedTask) { | |
101 | + if (task.requestId == archivedTask.requestId) { | |
102 | + logger.info('Mengulang trx untuk advice', {task: task}); | |
103 | + _topupRequest(task); | |
104 | + } else { | |
105 | + logger.info('Terdeteksi trx sama dalam satu hari yang sama', {task: task}); | |
106 | + callbackReport(task.requestId, '55', 'Terdeteksi trx sama dalam satu hari yang sama'); | |
107 | + } | |
108 | +} | |
109 | + | |
110 | +function _topupRequest(task) { | |
111 | + var pattern = config.h2h_out.request_pattern; | |
112 | + | |
113 | + var keywords = { | |
114 | + remoteProduct: task.remoteProduct, | |
115 | + destination: task.destination, | |
116 | + pin: config.h2h_out.pin | |
117 | + } | |
118 | + | |
119 | + im.saveTask(task, function() { | |
120 | + im.registerResendDelay(task); | |
121 | + | |
122 | + var msg = im.createMessage(pattern, keywords); | |
123 | + imAdaptor.sendMessage(config.h2h_out.partner, msg); | |
124 | + }); | |
125 | + | |
126 | +} | |
127 | + | |
128 | +function topupRequest(task) { | |
129 | + if (!aaa.isTodayTrx(task)) { | |
130 | + logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan'); | |
131 | + callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan'); | |
132 | + im.cancelResendDelay(task); | |
133 | + return; | |
134 | + } | |
135 | + | |
136 | + im.checkForSameDayDuplicate(task, _topupRequest, onSameDayDupe, _topupRequest); | |
137 | +} | |
138 | + | |
139 | +exports.start = start; | |
140 | +exports.topupRequest = topupRequest; |