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