Commit 78e1fc50b9558ea397803b42323cc151e7ad1275
1 parent
a8f9701c39
Exists in
master
ready to pretest
Showing 4 changed files with 167 additions and 0 deletions Side-by-side Diff
.gitignore
package.json
... | ... | @@ -23,5 +23,9 @@ |
23 | 23 | "request": "^2.75.0", |
24 | 24 | "sate24": "git+http://gitlab.kodesumber.com/reload97/node-sate24.git", |
25 | 25 | "sate24-expresso": "git+http://gitlab.kodesumber.com/reload97/sate24-expresso.git" |
26 | + }, | |
27 | + "devDependencies": { | |
28 | + "mocha": "^3.1.0", | |
29 | + "should": "^11.1.0" | |
26 | 30 | } |
27 | 31 | } |
partner-otomax.js
... | ... | @@ -0,0 +1,151 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +var request = require('request'); | |
4 | +var crypto = require('crypto'); | |
5 | + | |
6 | +var taskHistory = require('sate24/task-history'); | |
7 | +var antiSameDayDupe = require('sate24/anti-same-day-dupe'); | |
8 | + | |
9 | +var config; | |
10 | +var aaa; | |
11 | +var logger; | |
12 | + | |
13 | + | |
14 | +function start(options) { | |
15 | + if (!options) { | |
16 | + console.log('Undefined options, terminating....'); | |
17 | + process.exit(1); | |
18 | + } | |
19 | + | |
20 | + if (options.config) { | |
21 | + config = options.config; | |
22 | + } else { | |
23 | + console.log('Undefined options.config, terminating....') | |
24 | + process.exit(1); | |
25 | + } | |
26 | + | |
27 | + if (options.aaa) { | |
28 | + aaa = options.aaa; | |
29 | + } else { | |
30 | + console.log('Undefined options.aaa, terminating....') | |
31 | + process.exit(1); | |
32 | + } | |
33 | + | |
34 | + if (options && options.logger) { | |
35 | + logger = options.logger; | |
36 | + } else { | |
37 | + console.log('Undefined options.logger, terminating....') | |
38 | + process.exit(1); | |
39 | + } | |
40 | + | |
41 | + resendDelay.init({ | |
42 | + config: config, | |
43 | + topupRequest: topupRequest, | |
44 | + logger: logger | |
45 | + }); | |
46 | + | |
47 | + taskHistory.init(options); | |
48 | + antiSameDayDupe.init(options); | |
49 | +} | |
50 | + | |
51 | +function callbackReport(requestId, rc, message) { | |
52 | + if (responseCode != '68' || dontResendDelay) { | |
53 | + resendDelay.cancel(requestId); | |
54 | + } else { | |
55 | + taskHistory.get(requestId, function(err, archivedTask) { | |
56 | + if (archivedTask) { | |
57 | + logger.verbose('DEBUG', {archivedTask: archivedTask}); | |
58 | + resendDelay.register(archivedTask); | |
59 | + } | |
60 | + }); | |
61 | + } | |
62 | + | |
63 | + options.aaa.callbackReportWithPushToMongoDb(requestId, rc, message); | |
64 | +} | |
65 | + | |
66 | +function topupRequest(task) { | |
67 | + if (!aaa.isTodayTrx(task)) { | |
68 | + logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan'); | |
69 | + callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan'); | |
70 | + resendDelay.cancel(task); | |
71 | + return; | |
72 | + } | |
73 | + | |
74 | + aaa.insertTaskToMongoDb(task); | |
75 | + antiSameDayDupe.check(task, _topupRequest, onSameDayDupe, _topupRequest); | |
76 | +} | |
77 | + | |
78 | +function _topupRequest(task) { | |
79 | + taskHistory.put(task, function() { | |
80 | + requestToPartner(task);; | |
81 | + }); | |
82 | +} | |
83 | + | |
84 | +function requestToPartner(task) { | |
85 | + let requestOptions = createRequestOptions(task); | |
86 | + | |
87 | + request(requestOptions, function(error, response, body) { | |
88 | + if (error) { | |
89 | + logger.warn('Error requesting to partner', {task: task, error: error}); | |
90 | + callbackReport(task.requestId, '68', 'Error requesting to partner. ' + error); | |
91 | + return; | |
92 | + } | |
93 | + | |
94 | + if (response.statusCode != 200) { | |
95 | + logger.warn('HTTP status code is not 200', {task: task, http_status_code: response.statusCode}); | |
96 | + callbackReport(task.requestId, '40', 'HTTP status code ' + response.statusCode); | |
97 | + return; | |
98 | + } | |
99 | + | |
100 | + parseMessage(task, message); | |
101 | + }); | |
102 | +} | |
103 | + | |
104 | +function parseMessage(task, message) { | |
105 | + let rc = '68'; | |
106 | + | |
107 | + if (message.indexOf('SUKSES') >= 0) { | |
108 | + rc = '00'; | |
109 | + } | |
110 | + else if (message.indexOf('GAGAL') >= 0) { | |
111 | + rc = '40'; | |
112 | + } | |
113 | + | |
114 | + callbackReport(task.requestId, rc, message); | |
115 | +} | |
116 | + | |
117 | +function generateSign(userid, remoteProduct, destination, requestId, pin, password) { | |
118 | + let plain = ["OtomaX", userid, remoteProduct, destination, requestId, pin, password].join("|"); | |
119 | + let sha1 = crypto.createHash('sha1').update(plain).digest().toString('hex'); | |
120 | + let buffer = new Buffer(sha1); | |
121 | + | |
122 | + return buffer.toString('base64'); | |
123 | +} | |
124 | + | |
125 | +function createRequestOptions(task) { | |
126 | + return requestOptions = { | |
127 | + url: config.h2h_out.partner, | |
128 | + qs: { | |
129 | + memberID: config.h2h_out.userid, | |
130 | + product: task.remoteProduct, | |
131 | + dest: task.destination, | |
132 | + refID: task.requestId, | |
133 | + sign: generateSign( | |
134 | + config.h2h_out.userid, | |
135 | + task.remoteProduct, | |
136 | + task.destination, | |
137 | + task.requestId, | |
138 | + config.h2h_out.pin, | |
139 | + confg.h2h_out.password | |
140 | + ) | |
141 | + } | |
142 | + }; | |
143 | +} | |
144 | + | |
145 | +function onSameDayDupe(task) { | |
146 | + callbackReport(task.requestId, '55', 'Transaksi duplikat dalam satu hari yang sama'); | |
147 | +} | |
148 | + | |
149 | +exports.start = start; | |
150 | +exports.topupRequest = topupRequest; | |
151 | +exports.generateSign = generateSign; |
test.js
... | ... | @@ -0,0 +1,10 @@ |
1 | +var should = require('should'); | |
2 | +var partner = require('./partner-otomax'); | |
3 | + | |
4 | +describe ('#partner', function() { | |
5 | + describe('generateSign', function() { | |
6 | + it('should return correct sign based on example from otomax doc', function() { | |
7 | + partner.generateSign('YUSUF', 'XX10', '08123456789', '2140669', '1144', 'abcd').should.equal('YmU1YWNkZjU4YmFlZTMxMWMwNGZjZmRiNWM4NTA3MmIwZDhkOGM3YQ=='); | |
8 | + }) | |
9 | + }); | |
10 | +}); |