From 9ced2cfdf03e19f6c35901c59076ab1322236d6b Mon Sep 17 00:00:00 2001 From: Adhidarma Hadiwinoto <me@adhisimon.org> Date: Fri, 20 Mar 2020 13:48:13 +0700 Subject: [PATCH] PAY finished --- lib/core-callback/sender.js | 5 +- lib/listener-partner/index.js | 2 + lib/listener-partner/routers/pay.js | 105 ++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 lib/listener-partner/routers/pay.js diff --git a/lib/core-callback/sender.js b/lib/core-callback/sender.js index 6d5c423..c552832 100644 --- a/lib/core-callback/sender.js +++ b/lib/core-callback/sender.js @@ -26,9 +26,12 @@ const sender = async (data, xid, retry) => { message: data.message, amount: data.amount, ending_balance: data.ending_balance, - amount_to_charge: data.amount_to_charge, }; + if (data.command === 'INQUIRY' && data.amount_to_charge) { + params.amount_to_charge = data.amount_to_charge; + } + const fullUrl = urlConcatQs(data.reverse_url, params); logger.info(`${MODULE_NAME} 8B6A4CEC: Sending CORE-CALLBACK to PARTNER`, { xid, retry, params, fullUrl, diff --git a/lib/listener-partner/index.js b/lib/listener-partner/index.js index e88271e..dee22b1 100644 --- a/lib/listener-partner/index.js +++ b/lib/listener-partner/index.js @@ -10,6 +10,7 @@ const logger = require('komodo-sdk/logger'); const middlewareCommon = require('../middlewares/common'); const routerInquiry = require('./routers/inquiry'); +const routerPay = require('./routers/pay'); const routerTopup = require('./routers/topup'); const routerTrxStatus = require('./routers/trx-status'); @@ -20,6 +21,7 @@ app.use(express.urlencoded({ extended: true })); app.use(middlewareCommon); app.use('/inquiry', routerInquiry); +app.use('/pay', routerPay); app.use('/topup', routerTopup); app.use('/trx-status', routerTrxStatus); diff --git a/lib/listener-partner/routers/pay.js b/lib/listener-partner/routers/pay.js new file mode 100644 index 0000000..031f023 --- /dev/null +++ b/lib/listener-partner/routers/pay.js @@ -0,0 +1,105 @@ +const axios = require('axios').default; +const express = require('express'); +const coreUrl = require('komodo-sdk/core-url'); + +const config = require('komodo-sdk/config'); +const logger = require('komodo-sdk/logger'); + +const getFromBodyQsParams = require('../../get-from-body-qs-params'); +const ipv6ToIpv4 = require('../../ipv6-to-ipv4'); + +const router = express.Router(); +module.exports = router; + +const CORE_ENDPOINT = `${coreUrl}/postpaid2/pay`; + +const mainHandler = async (req, res) => { + const { xid } = res.locals; + + const requestId = (getFromBodyQsParams(req, 'request_id') || '').toString().trim(); + const terminalNameWithoutIp = (getFromBodyQsParams(req, 'terminal_name') || '').toString().trim(); + const terminalName = `${terminalNameWithoutIp}@${ipv6ToIpv4(req.ip)}`; + const productName = (getFromBodyQsParams(req, 'product_name') || '').trim().toUpperCase(); + const destination = (getFromBodyQsParams(req, 'destination') || '').toString().trim(); + const password = getFromBodyQsParams(req, 'password'); + const reverseUrl = getFromBodyQsParams(req, 'reverse_url'); + + if (!requestId || !terminalNameWithoutIp || !productName || !destination) { + res.end('INVALID REQUEST. Missing request_id or terminal_name or product_name or destination.'); + return; + } + + const params = { + origin: config.name, + report_ip: config.listener.core.from_ip, + report_port: config.listener.core.port || 25614, + request_id: requestId, + terminal_name: terminalName, + product_name: productName, + destination, + terminal_password: password, + reverse_url: reverseUrl, + }; + + logger.info('Forwarding PAY request to CORE', { xid, params }); + try { + const result = await axios.get(CORE_ENDPOINT, { + params, + timeout: 10000, + }); + + if (!result || !result.data) { + const newError = new Error('0D428E4C: Empty CORE PAY direct-response'); + logger.warn(newError.message, { xid }); + throw newError; + } + + logger.verbose('Got PAY direct-response from CORE', { + xid, + coreResponse: result.data, + }); + + const resultForPartner = { + httpgetx_xid: xid, + command: result.data.command, + request_id: result.data.request_id && result.data.request_id.toString(), + transaction_id: result.data.transaction_id && result.data.transaction_id.toString(), + transaction_date: result.data.transaction_date, + store_name: result.data.store_name, + terminal_name: result.data.terminal_name, + product_name: result.data.product_name, + destination: result.data.destination, + rc: result.data.rc, + sn: result.data.sn, + message: result.data.message, + amount: result.data.amount, + ending_balance: result.data.ending_balance, + }; + + logger.verbose('Forwarding CORE PAY direct-response to partner', { + xid, + resultForPartner, + }); + + res.json(resultForPartner); + } catch (e) { + logger.warn('EXCEPTION on forwarding PAY request to CORE', { + xid, + errCode: e.code, + errMessage: e.message, + }); + + res.json({ + httpgetx_xid: xid, + command: 'PAY', + request_id: requestId, + terminal_name: terminalName, + product_name: productName, + destination, + rc: '68', + message: 'CORE tidak merespon dengan benar, tidak dapat mengetahui status request anda', + }); + } +}; + +router.all('/', mainHandler); -- 1.9.0