Commit 0ce2484a6ed788d39882a0e390d667150d8dd588
1 parent
c612c9017a
Exists in
master
and in
1 other branch
Add log on including terminal location
Showing 1 changed file with 6 additions and 0 deletions Inline Diff
lib/partner-listener/routers/topup.js
1 | const MODULE_NAME = 'PARTNER-LISTENER.ROUTER.TOPUP'; | 1 | const MODULE_NAME = 'PARTNER-LISTENER.ROUTER.TOPUP'; |
2 | 2 | ||
3 | const express = require('express'); | 3 | const express = require('express'); |
4 | 4 | ||
5 | const config = require('komodo-sdk/config'); | 5 | const config = require('komodo-sdk/config'); |
6 | const logger = require('tektrans-logger'); | 6 | const logger = require('tektrans-logger'); |
7 | const coreapi = require('komodo-sdk/coreapi'); | 7 | const coreapi = require('komodo-sdk/coreapi'); |
8 | // const coreapi = require('../../coreapi'); | 8 | // const coreapi = require('../../coreapi'); |
9 | const matrix = require('../../matrix'); | 9 | const matrix = require('../../matrix'); |
10 | const dumper = require('../dumper'); | 10 | const dumper = require('../dumper'); |
11 | 11 | ||
12 | const router = express.Router(); | 12 | const router = express.Router(); |
13 | module.exports = router; | 13 | module.exports = router; |
14 | 14 | ||
15 | const terminalsWithLocation = Array.isArray(config.terminals_with_location) | 15 | const terminalsWithLocation = Array.isArray(config.terminals_with_location) |
16 | ? config.terminals_with_location | 16 | ? config.terminals_with_location |
17 | .filter((item) => typeof item === 'string') | 17 | .filter((item) => typeof item === 'string') |
18 | .map((item) => (item.trim().toLowerCase())) | 18 | .map((item) => (item.trim().toLowerCase())) |
19 | : []; | 19 | : []; |
20 | 20 | ||
21 | function onInvalidParameter(missingParameter, req, res) { | 21 | function onInvalidParameter(missingParameter, req, res) { |
22 | logger.verbose(`${MODULE_NAME} 1536D577: Undefined ${missingParameter} parameter`, { | 22 | logger.verbose(`${MODULE_NAME} 1536D577: Undefined ${missingParameter} parameter`, { |
23 | xid: res.locals.xid, | 23 | xid: res.locals.xid, |
24 | ip: req.ip, | 24 | ip: req.ip, |
25 | terminal_name: req.body.terminal_name || req.query.terminal_name, | 25 | terminal_name: req.body.terminal_name || req.query.terminal_name, |
26 | request_id: req.body.request_id || req.query.request_id, | 26 | request_id: req.body.request_id || req.query.request_id, |
27 | product_name: req.body.product_name || req.query.product_name, | 27 | product_name: req.body.product_name || req.query.product_name, |
28 | destination: req.body.destination || req.query.destination, | 28 | destination: req.body.destination || req.query.destination, |
29 | }); | 29 | }); |
30 | res.end('INVALID REQUEST'); | 30 | res.end('INVALID REQUEST'); |
31 | } | 31 | } |
32 | 32 | ||
33 | function pagePrerequisite(req, res, next) { | 33 | function pagePrerequisite(req, res, next) { |
34 | if (!req.body) req.body = {}; | 34 | if (!req.body) req.body = {}; |
35 | 35 | ||
36 | const terminalName = req.body.terminal_name || req.query.terminal_name; | 36 | const terminalName = req.body.terminal_name || req.query.terminal_name; |
37 | if (!terminalName || typeof terminalName !== 'string') { | 37 | if (!terminalName || typeof terminalName !== 'string') { |
38 | onInvalidParameter('terminal_name', req, res); | 38 | onInvalidParameter('terminal_name', req, res); |
39 | return; | 39 | return; |
40 | } | 40 | } |
41 | 41 | ||
42 | if (!req.body.password && !req.query.password) { | 42 | if (!req.body.password && !req.query.password) { |
43 | onInvalidParameter('password', req, res); | 43 | onInvalidParameter('password', req, res); |
44 | return; | 44 | return; |
45 | } | 45 | } |
46 | 46 | ||
47 | if (!req.body.request_id && !req.query.request_id) { | 47 | if (!req.body.request_id && !req.query.request_id) { |
48 | onInvalidParameter('request_id', req, res); | 48 | onInvalidParameter('request_id', req, res); |
49 | return; | 49 | return; |
50 | } | 50 | } |
51 | 51 | ||
52 | if (!req.body.product_name && !req.query.product_name) { | 52 | if (!req.body.product_name && !req.query.product_name) { |
53 | onInvalidParameter('product_name', req, res); | 53 | onInvalidParameter('product_name', req, res); |
54 | return; | 54 | return; |
55 | } | 55 | } |
56 | 56 | ||
57 | if (!req.body.destination && !req.query.destination) { | 57 | if (!req.body.destination && !req.query.destination) { |
58 | onInvalidParameter('destination', req, res); | 58 | onInvalidParameter('destination', req, res); |
59 | return; | 59 | return; |
60 | } | 60 | } |
61 | 61 | ||
62 | next(); | 62 | next(); |
63 | } | 63 | } |
64 | 64 | ||
65 | async function pageIndex(req, res) { | 65 | async function pageIndex(req, res) { |
66 | const { xid } = res.locals; | 66 | const { xid } = res.locals; |
67 | 67 | ||
68 | const terminalNameWithoutIp = ((req.body.terminal_name || req.query.terminal_name) || '').trim(); | 68 | const terminalNameWithoutIp = ((req.body.terminal_name || req.query.terminal_name) || '').trim(); |
69 | const terminalName = `${terminalNameWithoutIp}@${req.ip.replace(/^::ffff:/, '')}`; | 69 | const terminalName = `${terminalNameWithoutIp}@${req.ip.replace(/^::ffff:/, '')}`; |
70 | 70 | ||
71 | const qs = { | 71 | const qs = { |
72 | terminal_name: terminalName, | 72 | terminal_name: terminalName, |
73 | password: req.body.password || req.query.password, | 73 | password: req.body.password || req.query.password, |
74 | request_id: req.body.request_id || req.query.request_id, | 74 | request_id: req.body.request_id || req.query.request_id, |
75 | product_name: req.body.product_name || req.query.product_name, | 75 | product_name: req.body.product_name || req.query.product_name, |
76 | destination: req.body.destination || req.query.destination, | 76 | destination: req.body.destination || req.query.destination, |
77 | origin: config.name || 'HTTPGETX', | 77 | origin: config.name || 'HTTPGETX', |
78 | report_ip: config.listener.core.ip || null, | 78 | report_ip: config.listener.core.ip || null, |
79 | report_port: config.listener.core.port, | 79 | report_port: config.listener.core.port, |
80 | reverse_url: req.body.reverse_url || req.query.reverse_url || null, | 80 | reverse_url: req.body.reverse_url || req.query.reverse_url || null, |
81 | }; | 81 | }; |
82 | 82 | ||
83 | if (terminalsWithLocation.indexOf(terminalNameWithoutIp.toLowerCase()) >= 0) { | 83 | if (terminalsWithLocation.indexOf(terminalNameWithoutIp.toLowerCase()) >= 0) { |
84 | const location = req.body.location | 84 | const location = req.body.location |
85 | || req.body.location_id | 85 | || req.body.location_id |
86 | || req.query.location | 86 | || req.query.location |
87 | || req.query.location_id; | 87 | || req.query.location_id; |
88 | 88 | ||
89 | if (location) { | 89 | if (location) { |
90 | logger.verbose(`${MODULE_NAME} 5C41FBFA: Including location from partner request`, { | ||
91 | xid, | ||
92 | terminalName, | ||
93 | location, | ||
94 | }); | ||
95 | |||
90 | qs.location = location; | 96 | qs.location = location; |
91 | } | 97 | } |
92 | } | 98 | } |
93 | 99 | ||
94 | matrix.core.sent += 1; | 100 | matrix.core.sent += 1; |
95 | 101 | ||
96 | const [err, coreResponse] = await coreapi({ | 102 | const [err, coreResponse] = await coreapi({ |
97 | xid, | 103 | xid, |
98 | path: '/prepaid/buy', | 104 | path: '/prepaid/buy', |
99 | qs, | 105 | qs, |
100 | }); | 106 | }); |
101 | 107 | ||
102 | if (err || !coreResponse) { | 108 | if (err || !coreResponse) { |
103 | matrix.core.sent_failed += 1; | 109 | matrix.core.sent_failed += 1; |
104 | matrix.core.last_error = { | 110 | matrix.core.last_error = { |
105 | xid, | 111 | xid, |
106 | ts: new Date(), | 112 | ts: new Date(), |
107 | e: err, | 113 | e: err, |
108 | eCode: err.code, | 114 | eCode: err.code, |
109 | eMessage: err.message, | 115 | eMessage: err.message, |
110 | }; | 116 | }; |
111 | 117 | ||
112 | logger.warn(`${MODULE_NAME} 8DEBE15F: ERROR on /prepaid/buy response`, { | 118 | logger.warn(`${MODULE_NAME} 8DEBE15F: ERROR on /prepaid/buy response`, { |
113 | xid, | 119 | xid, |
114 | err, | 120 | err, |
115 | coreResponseTypeof: typeof coreResponse, | 121 | coreResponseTypeof: typeof coreResponse, |
116 | coreResponse, | 122 | coreResponse, |
117 | }); | 123 | }); |
118 | res.end('INVALID CORE RESPONSE'); | 124 | res.end('INVALID CORE RESPONSE'); |
119 | 125 | ||
120 | dumper(xid, req, 'INVALID CORE RESPONSE'); | 126 | dumper(xid, req, 'INVALID CORE RESPONSE'); |
121 | return; | 127 | return; |
122 | } | 128 | } |
123 | 129 | ||
124 | logger.verbose(`${MODULE_NAME} 2528A9B4: Got CORE response`, { | 130 | logger.verbose(`${MODULE_NAME} 2528A9B4: Got CORE response`, { |
125 | xid, | 131 | xid, |
126 | coreResponse, | 132 | coreResponse, |
127 | }); | 133 | }); |
128 | 134 | ||
129 | const responseToPartner = { | 135 | const responseToPartner = { |
130 | httpgetx_xid: xid, | 136 | httpgetx_xid: xid, |
131 | request_id: coreResponse.request_id, | 137 | request_id: coreResponse.request_id, |
132 | transaction_id: coreResponse.transaction_id, | 138 | transaction_id: coreResponse.transaction_id, |
133 | transaction_date: coreResponse.transaction_date, | 139 | transaction_date: coreResponse.transaction_date, |
134 | store_name: coreResponse.store_name, | 140 | store_name: coreResponse.store_name, |
135 | terminal_name: coreResponse.terminal_name, | 141 | terminal_name: coreResponse.terminal_name, |
136 | product_name: coreResponse.product_name, | 142 | product_name: coreResponse.product_name, |
137 | destination: coreResponse.destination, | 143 | destination: coreResponse.destination, |
138 | rc: coreResponse.rc, | 144 | rc: coreResponse.rc, |
139 | sn: coreResponse.sn || undefined, | 145 | sn: coreResponse.sn || undefined, |
140 | amount: Number(coreResponse.amount) || undefined, | 146 | amount: Number(coreResponse.amount) || undefined, |
141 | ending_balance: Number(coreResponse.ending_balance) || undefined, | 147 | ending_balance: Number(coreResponse.ending_balance) || undefined, |
142 | message: coreResponse.message, | 148 | message: coreResponse.message, |
143 | }; | 149 | }; |
144 | 150 | ||
145 | res.json(responseToPartner); | 151 | res.json(responseToPartner); |
146 | dumper(xid, req, responseToPartner); | 152 | dumper(xid, req, responseToPartner); |
147 | } | 153 | } |
148 | 154 | ||
149 | // router.all('/', (req, res) => { res.status(404).end('404: Not implemented yet'); }); | 155 | // router.all('/', (req, res) => { res.status(404).end('404: Not implemented yet'); }); |
150 | router.get('/', pagePrerequisite, pageIndex); | 156 | router.get('/', pagePrerequisite, pageIndex); |
151 | 157 |