partner-ym.js
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var im = require('./im.js')
var YM = require('yahoomessenger');
var imAdaptor = require('./adaptor-ym');
var config;
var aaa;
var logger;
var callbackReport;
function onLoginSuccessful() {
logger.info('Login successful, resuming aaa communication');
aaa.resume();
}
function onPM(from, msg) {
if (!im.isAllowedFrom(from)) {
logger.info('Ignoring message from unknown sender', {from: from, msg: msg});
return;
}
var remoteProduct = im.getRemoteProductFromMessage(msg);
var destination = im.getDestinationFromMessage(msg);
if (!remoteProduct && !destination) {
logger.warn('Missing remote product or destination', {remoteProduct: remoteProduct, destination: destination, msg: msg});
return;
}
logger.info('Got report from partner', {remoteProduct: remoteProduct, destination: destination, msg: msg});
im.getTask(remoteProduct, destination, function(err, task) {
if (err) {
logger.warn('Error getting relevant task');
return;
}
if (!task) {
logger.warn('Something wrong, undefined task without error')
return;
}
logger.verbose('Got relevant task', {task: task, msg: msg});
var rc = im.getRcFromMessage(msg);
if (rc == '00') {
var sn = im.getSnFromMessage(msg);
if (sn) {
msg = 'SN=' + sn + ';' + msg;
}
}
if (['00', '55', '68'].indexOf(rc) == -1) {
im.deleteTask(remoteProduct, destination);
}
if (rc != '68') {
im.cancelResendDelay(task);
}
callbackReport(task.requestId, rc, msg);
});
}
function start(options) {
if (options && options.config) {
config = options.config;
} else {
console.log('Unknown options.config');
process.exit('1');
}
if (options && options.aaa) {
aaa = options.aaa;
}
if (options && options.aaa && options.aaa.callbackReport) {
callbackReport = options.aaa.callbackReport;
} else {
console.log('Unknown options.aaa.callbackReport')
process.exit(2);
}
if (options && options.logger) {
logger = options.logger;
} else {
logger = new winston.Logger({
transports: [
new (winston.transports.Console)()
]
});
}
var callbacks = {
onLoginSuccessful: onLoginSuccessful,
onPM: onPM,
}
im.init(options);
imAdaptor.init(config.h2h_out.ym_id, config.h2h_out.ym_password, logger, callbacks);
}
function onSameDayDupe(task, archivedTask) {
if (task.requestId == archivedTask.requestId) {
logger.info('Mengulang trx untuk advice', {task: task});
_topupRequest(task);
} else {
logger.info('Terdeteksi trx sama dalam satu hari yang sama', {task: task});
callbackReport(task.requestId, '55', 'Terdeteksi trx sama dalam satu hari yang sama');
}
}
function _topupRequest(task) {
var pattern = config.h2h_out.request_pattern;
var keywords = {
remoteProduct: task.remoteProduct,
destination: task.destination,
pin: config.h2h_out.pin
}
im.saveTask(task, function() {
im.registerResendDelay(task);
var msg = im.createMessage(pattern, keywords);
imAdaptor.sendMessage(config.h2h_out.partner, msg);
});
}
function topupRequest(task) {
if (!aaa.isTodayTrx(task)) {
logger.warn('Maaf, transaksi beda hari tidak dapat dilakukan');
callbackReport(task.requestId, '68', 'Maaf, transaksi beda hari tidak dapat dilakukan');
im.cancelResendDelay(task);
return;
}
im.checkForSameDayDuplicate(task, _topupRequest, onSameDayDupe, _topupRequest);
}
exports.start = start;
exports.topupRequest = topupRequest;