topup.js
5.18 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const MODULE_NAME = 'PARTNER-LISTENER.ROUTER.TOPUP';
const express = require('express');
const config = require('komodo-sdk/config');
const logger = require('tektrans-logger');
const coreapi = require('komodo-sdk/coreapi');
// const coreapi = require('../../coreapi');
const matrix = require('../../matrix');
const dumper = require('../dumper');
const webhookSender = require('../../webhook-sender');
const router = express.Router();
module.exports = router;
const terminalsWithLocation = Array.isArray(config.terminals_with_location)
? config.terminals_with_location
.filter((item) => typeof item === 'string')
.map((item) => (item.trim().toLowerCase()))
: [];
function onInvalidParameter(missingParameter, req, res) {
logger.verbose(`${MODULE_NAME} 1536D577: Undefined ${missingParameter} parameter`, {
xid: res.locals.xid,
ip: req.ip,
terminal_name: req.body.terminal_name || req.query.terminal_name,
request_id: req.body.request_id || req.query.request_id,
product_name: req.body.product_name || req.query.product_name,
destination: req.body.destination || req.query.destination,
});
res.end('INVALID REQUEST');
}
function pagePrerequisite(req, res, next) {
if (!req.body) req.body = {};
const terminalName = req.body.terminal_name || req.query.terminal_name;
if (!terminalName || typeof terminalName !== 'string') {
onInvalidParameter('terminal_name', req, res);
return;
}
if (!req.body.password && !req.query.password) {
onInvalidParameter('password', req, res);
return;
}
if (!req.body.request_id && !req.query.request_id) {
onInvalidParameter('request_id', req, res);
return;
}
if (!req.body.product_name && !req.query.product_name) {
onInvalidParameter('product_name', req, res);
return;
}
if (!req.body.destination && !req.query.destination) {
onInvalidParameter('destination', req, res);
return;
}
next();
}
async function pageIndex(req, res) {
const { xid } = res.locals;
const terminalNameWithoutIp = ((req.body.terminal_name || req.query.terminal_name) || '').trim();
const terminalName = `${terminalNameWithoutIp}@${req.ip.replace(/^::ffff:/, '')}`;
const qs = {
terminal_name: terminalName,
password: req.body.password || req.query.password,
request_id: req.body.request_id || req.query.request_id,
product_name: req.body.product_name || req.query.product_name,
destination: req.body.destination || req.query.destination,
origin: config.name || 'HTTPGETX',
report_ip: config.listener.core.ip || null,
report_port: config.listener.core.port,
reverse_url: req.body.reverse_url || req.query.reverse_url || null,
};
if (terminalsWithLocation.indexOf(terminalNameWithoutIp.toLowerCase()) >= 0) {
const location = req.body.location
|| req.body.location_id
|| req.query.location
|| req.query.location_id;
if (location) {
logger.verbose(`${MODULE_NAME} 5C41FBFA: Including location from partner request`, {
xid,
terminalName,
location,
});
qs.location = location;
}
}
matrix.core.sent += 1;
const [err, coreResponse] = await coreapi({
xid,
path: '/prepaid/buy',
qs,
});
if (err || !coreResponse) {
matrix.core.sent_failed += 1;
matrix.core.last_error = {
xid,
ts: new Date(),
e: err,
eCode: err.code,
eMessage: err.message,
};
logger.warn(`${MODULE_NAME} 8DEBE15F: ERROR on /prepaid/buy response`, {
xid,
err,
coreResponseTypeof: typeof coreResponse,
coreResponse,
});
res.end('INVALID CORE RESPONSE');
dumper(xid, req, 'INVALID CORE RESPONSE');
return;
}
logger.verbose(`${MODULE_NAME} 2528A9B4: Got CORE response`, {
xid,
coreResponse,
});
const responseToPartner = {
httpgetx_xid: xid,
request_id: coreResponse.request_id,
transaction_id: coreResponse.transaction_id,
transaction_date: coreResponse.transaction_date,
store_name: coreResponse.store_name,
terminal_name: coreResponse.terminal_name,
product_name: coreResponse.product_name,
destination: coreResponse.destination,
rc: coreResponse.rc,
sn: coreResponse.sn || undefined,
amount: Number(coreResponse.amount) || undefined,
ending_balance: Number(coreResponse.ending_balance) || undefined,
message: coreResponse.message,
};
res.json(responseToPartner);
dumper(xid, req, responseToPartner);
const webhookType = 'KOMODO-CENTER-HTTPGETX.PARTNER-LISTENER.DIRECT-RESPONSE';
webhookSender(xid, webhookType, responseToPartner);
}
// router.all('/', (req, res) => { res.status(404).end('404: Not implemented yet'); });
router.get('/', pagePrerequisite, pageIndex);
router.post('/', express.urlencoded({ extended: true }), express.json({ extended: true }), pagePrerequisite, pageIndex);