Compare View
Commits (3)
Changes
Showing 3 changed files Inline Diff
lib/actions/buy-to-sds.js
| 1 | const MODULE_NAME = 'ACTIONS.BUY-TO-SDS'; | 1 | const MODULE_NAME = 'ACTIONS.BUY-TO-SDS'; |
| 2 | 2 | ||
| 3 | const logger = require('tektrans-logger'); | 3 | const logger = require('tektrans-logger'); |
| 4 | const axios = require('axios').default; | 4 | const axios = require('axios').default; |
| 5 | 5 | ||
| 6 | const config = require('../config'); | 6 | const config = require('../config'); |
| 7 | 7 | ||
| 8 | const client = axios.create({ | 8 | const client = axios.create({ |
| 9 | baseURL: config.sds.url, | 9 | baseURL: config.sds.url, |
| 10 | timeout: config.sds.request_timeout_ms, | 10 | timeout: config.sds.request_timeout_ms, |
| 11 | }); | 11 | }); |
| 12 | 12 | ||
| 13 | /** | 13 | /** |
| 14 | * request buy to sds | 14 | * request buy to sds |
| 15 | * | 15 | * |
| 16 | * @param {string} xid | 16 | * @param {string} xid |
| 17 | * @param {string} requestId | 17 | * @param {string} requestId |
| 18 | * @param {string} destination | 18 | * @param {string} destination |
| 19 | * @param {string} productName | 19 | * @param {string} productName |
| 20 | * @param {number} quantity | 20 | * @param {number} quantity |
| 21 | */ | 21 | */ |
| 22 | module.exports = async ( | 22 | module.exports = async ( |
| 23 | xid, | 23 | xid, |
| 24 | requestId, | 24 | requestId, |
| 25 | destination, | 25 | destination, |
| 26 | productName, | 26 | productName, |
| 27 | quantity, | 27 | quantity, |
| 28 | ) => { | 28 | ) => { |
| 29 | logger.verbose(`${MODULE_NAME} 4AD4DF41: buy to sds`, { | 29 | logger.verbose(`${MODULE_NAME} 4AD4DF41: buy to sds`, { |
| 30 | requestId, destination, productName, quantity, xid, | 30 | requestId, destination, productName, quantity, xid, |
| 31 | }); | 31 | }); |
| 32 | 32 | ||
| 33 | const params = { | 33 | const params = { |
| 34 | trxid: requestId, | 34 | trxid: requestId, |
| 35 | username: config.sds.username, | 35 | username: config.sds.username, |
| 36 | password: config.sds.password, | 36 | password: config.sds.password, |
| 37 | number: destination, | 37 | number: destination, |
| 38 | product: productName, | 38 | product: productName, |
| 39 | amount: quantity, | 39 | amount: quantity, |
| 40 | payment: config.sds.payment, | 40 | payment: config.sds.payment, |
| 41 | }; | 41 | }; |
| 42 | 42 | ||
| 43 | try { | 43 | try { |
| 44 | const response = await client.get('/request', { | 44 | const response = await client.get('/request', { |
| 45 | params, | 45 | params, |
| 46 | }); | 46 | }); |
| 47 | if (!response) { | 47 | if (!response) { |
| 48 | throw new Error(`${MODULE_NAME} 6CE3E06E: Empty response sds`); | 48 | throw new Error(`${MODULE_NAME} 6CE3E06E: Empty response sds`); |
| 49 | } | 49 | } |
| 50 | if (!response.data) { | 50 | if (!response.data) { |
| 51 | throw new Error(`${MODULE_NAME} F236476F: Empty response sds`); | 51 | throw new Error(`${MODULE_NAME} F236476F: Empty response sds`); |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | return response.data; | 54 | return response.data; |
| 55 | } catch (err) { | 55 | } catch (err) { |
| 56 | logger.warn(`${MODULE_NAME} 48BAA6B7: Exception`, { | 56 | logger.warn(`${MODULE_NAME} 48BAA6B7: Exception`, { |
| 57 | xid, | 57 | xid, |
| 58 | requestId, | 58 | requestId, |
| 59 | destination, | 59 | destination, |
| 60 | productName, | 60 | productName, |
| 61 | quantity, | 61 | quantity, |
| 62 | message: err.message, | 62 | message: err.message, |
| 63 | }); | 63 | }); |
| 64 | throw err; | 64 | throw err; |
| 65 | } | 65 | } |
| 66 | }; | 66 | }; |
| 67 | 67 |
lib/actions/buy.js
| 1 | const MODULE_NAME = 'ACTIONS.BUY'; | 1 | const MODULE_NAME = 'ACTIONS.BUY'; |
| 2 | 2 | ||
| 3 | const logger = require('tektrans-logger'); | 3 | const logger = require('tektrans-logger'); |
| 4 | const axios = require('axios').default; | 4 | const axios = require('axios').default; |
| 5 | 5 | ||
| 6 | const config = require('../config'); | 6 | const config = require('../config'); |
| 7 | const configData = require('../config/data'); | 7 | const configData = require('../config/data'); |
| 8 | 8 | ||
| 9 | const buyToSDS = require('./buy-to-sds'); | 9 | const buyToSDS = require('./buy-to-sds'); |
| 10 | 10 | ||
| 11 | const client = axios.create({ | 11 | const client = axios.create({ |
| 12 | baseURL: config.core.url, | 12 | baseURL: config.core.url, |
| 13 | timeout: config.core.request_timeout_ms, | 13 | timeout: config.core.request_timeout_ms, |
| 14 | headers: { | 14 | headers: { |
| 15 | 'x-access-token': config.core.access_token, | 15 | 'x-access-token': config.core.access_token, |
| 16 | }, | 16 | }, |
| 17 | }); | 17 | }); |
| 18 | 18 | ||
| 19 | /** | 19 | /** |
| 20 | * Buy a product from supplier komodo | 20 | * Buy a product from supplier komodo |
| 21 | * | 21 | * |
| 22 | * @param {string} xid | 22 | * @param {string} xid |
| 23 | * @param {object} transaction | 23 | * @param {object} transaction |
| 24 | * | 24 | * |
| 25 | */ | 25 | */ |
| 26 | module.exports = async (xid, transaction) => { | 26 | module.exports = async (xid, transaction) => { |
| 27 | try { | 27 | try { |
| 28 | logger.verbose(`${MODULE_NAME} 4B139379: Buy product to komodo`, { | 28 | logger.verbose(`${MODULE_NAME} 4B139379: Buy product to komodo`, { |
| 29 | xid, | 29 | xid, |
| 30 | transaction, | 30 | transaction, |
| 31 | }); | 31 | }); |
| 32 | const iConfig = await configData.all(); | 32 | const iConfig = await configData.all(); |
| 33 | 33 | ||
| 34 | let productName = transaction.product_name; | 34 | let productName = transaction.product_name; |
| 35 | if ( | 35 | if ( |
| 36 | iConfig.products[transaction.product_name] | 36 | iConfig.products[transaction.product_name] |
| 37 | && iConfig.products[transaction.product_name].remote | 37 | && iConfig.products[transaction.product_name].remote |
| 38 | ) { | 38 | ) { |
| 39 | productName = iConfig.products[transaction.product_name].remote; | 39 | productName = iConfig.products[transaction.product_name].remote; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | // const callbackUrl = `${iConfig.url}:${iConfig.port}/apikey/${iConfig.apikey}/updates`; | 42 | // const callbackUrl = `${iConfig.url}:${iConfig.port}/apikey/${iConfig.apikey}/updates`; |
| 43 | const result = await buyToSDS( | 43 | const result = await buyToSDS( |
| 44 | xid, | 44 | xid, |
| 45 | `${transaction.request_id}-${transaction.id}`, | 45 | `${transaction.request_id}-${transaction.id}`, |
| 46 | transaction.destination, | 46 | transaction.destination, |
| 47 | productName, | 47 | productName, |
| 48 | transaction.quantity, | 48 | transaction.quantity, |
| 49 | ); | 49 | ); |
| 50 | logger.verbose(`${MODULE_NAME} 5BDFAF41: result from sds`, { | 50 | logger.verbose(`${MODULE_NAME} 5BDFAF41: result from sds`, { |
| 51 | xid, | 51 | xid, |
| 52 | trxId: transaction.id, | 52 | trxId: transaction.id, |
| 53 | result, | 53 | result, |
| 54 | }); | 54 | }); |
| 55 | let rc = 40; | 55 | let rc = 40; |
| 56 | if (result.code === '200') { | 56 | if (result.code === '200') { |
| 57 | rc = 68; | 57 | rc = 68; |
| 58 | } | 58 | } |
| 59 | const params = { | 59 | const params = { |
| 60 | id: transaction.id, | 60 | id: transaction.id, |
| 61 | rc, | 61 | rc, |
| 62 | amount: result.amount || null, | 62 | amount: result.amount || null, |
| 63 | message: result.text || '', | 63 | message: result.text || '', |
| 64 | sn: result.sn || null, | 64 | sn: result.sn || null, |
| 65 | }; | 65 | }; |
| 66 | 66 | ||
| 67 | await client.post('/transactions/gateway-update', params); | 67 | await client.post('/transactions/gateway-update', params); |
| 68 | } catch (e) { | 68 | } catch (e) { |
| 69 | logger.warn(`${MODULE_NAME} E9887C98: Exception`, { | 69 | logger.warn(`${MODULE_NAME} E9887C98: Exception`, { |
| 70 | xid, | 70 | xid, |
| 71 | message: e.message, | 71 | message: e.message, |
| 72 | code: e.code, | 72 | code: e.code, |
| 73 | }); | 73 | }); |
| 74 | } | 74 | } |
| 75 | }; | 75 | }; |
| 76 | 76 |
lib/http-server/routers/updates/index.js
| 1 | const MODULE_NAME = 'HTTP-SERVER.ROUTER.UPDATES'; | 1 | const MODULE_NAME = 'HTTP-SERVER.ROUTER.UPDATES'; |
| 2 | 2 | ||
| 3 | const express = require('express'); | 3 | const express = require('express'); |
| 4 | const axios = require('axios').default; | 4 | const axios = require('axios').default; |
| 5 | const logger = require('tektrans-logger'); | 5 | const logger = require('tektrans-logger'); |
| 6 | 6 | ||
| 7 | const config = require('../../../config'); | 7 | const config = require('../../../config'); |
| 8 | 8 | ||
| 9 | const router = express.Router(); | 9 | const router = express.Router(); |
| 10 | 10 | ||
| 11 | module.exports = router; | 11 | module.exports = router; |
| 12 | 12 | ||
| 13 | const client = axios.create({ | 13 | const client = axios.create({ |
| 14 | baseURL: config.core.url, | 14 | baseURL: config.core.url, |
| 15 | timeout: config.core.request_timeout_ms, | 15 | timeout: config.core.request_timeout_ms, |
| 16 | headers: { | 16 | headers: { |
| 17 | 'x-access-token': config.core.access_token, | 17 | 'x-access-token': config.core.access_token, |
| 18 | }, | 18 | }, |
| 19 | }); | 19 | }); |
| 20 | 20 | ||
| 21 | const pageUpdate = async (req, res) => { | 21 | const pageUpdate = async (req, res) => { |
| 22 | const { xid } = res.locals; | 22 | const { xid } = res.locals; |
| 23 | let data = req.query; | 23 | let data = req.query; |
| 24 | if (req.method.toUpperCase() !== 'GET') { | 24 | if (req.method.toUpperCase() !== 'GET') { |
| 25 | data = req.body; | 25 | data = req.body; |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | try { | 28 | try { |
| 29 | logger.verbose(`${MODULE_NAME} 9E5C70C8: update from sds`, { xid, data }); | 29 | logger.verbose(`${MODULE_NAME} 9E5C70C8: update from sds`, { xid, data }); |
| 30 | const requestIds = data.trxid.split('-'); | 30 | const requestIds = data.trxid.split('-'); |
| 31 | let trxId = ''; | 31 | let trxId = ''; |
| 32 | if (Array.isArray(requestIds) && requestIds.length > 0) { | 32 | if (Array.isArray(requestIds) && requestIds.length > 0) { |
| 33 | trxId = requestIds[requestIds.length - 1]; | 33 | trxId = requestIds[requestIds.length - 1]; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | let rc = '68'; | 36 | let rc = '68'; |
| 37 | if (data.status === 'S') { | 37 | if (data.status === 'S') { |
| 38 | rc = '00'; | 38 | rc = '00'; |
| 39 | } else if (data.status === 'F' || data.status === 'R') { | 39 | } else if (data.status === 'F') { |
| 40 | rc = '40'; | 40 | rc = '40'; |
| 41 | } else if (data.status === 'R') { | ||
| 42 | rc = '47'; | ||
| 41 | } | 43 | } |
| 42 | const params = { | 44 | const params = { |
| 43 | id: trxId, | 45 | id: trxId, |
| 44 | rc, | 46 | rc, |
| 45 | message: data.message, | 47 | message: data.message, |
| 46 | sn: data.sn || null, | 48 | sn: data.sn || null, |
| 47 | related_data: JSON.stringify(data), | 49 | related_data: JSON.stringify(data), |
| 48 | }; | 50 | }; |
| 49 | 51 | ||
| 50 | client.post('/transactions/gateway-update', params).then((result) => { | 52 | client.post('/transactions/gateway-update', params).then((result) => { |
| 51 | logger.verbose(`${MODULE_NAME} A8DA0D04: response from core2`, { | 53 | logger.verbose(`${MODULE_NAME} A8DA0D04: response from core2`, { |
| 52 | xid, | 54 | xid, |
| 53 | response: result.data, | 55 | response: result.data, |
| 54 | }); | 56 | }); |
| 55 | }).catch((err) => { | 57 | }).catch((err) => { |
| 56 | logger.warn(`${MODULE_NAME} 32EB485C: Exception on request to core2`, { | 58 | logger.warn(`${MODULE_NAME} 32EB485C: Exception on request to core2`, { |
| 57 | xid, | 59 | xid, |
| 58 | eMessage: err.message, | 60 | eMessage: err.message, |
| 59 | eCode: err.code, | 61 | eCode: err.code, |
| 60 | }); | 62 | }); |
| 61 | }); | 63 | }); |
| 62 | 64 | ||
| 63 | res.json({ error: false, message: 'OK' }); | 65 | res.json({ error: false, message: 'OK' }); |
| 64 | } catch (e) { | 66 | } catch (e) { |
| 65 | logger.warn(`${MODULE_NAME} B12B4A2C: Exception.`, { | 67 | logger.warn(`${MODULE_NAME} B12B4A2C: Exception.`, { |
| 66 | xid, eMessage: e.message, eCode: e.code, | 68 | xid, eMessage: e.message, eCode: e.code, |
| 67 | }); | 69 | }); |
| 68 | res.status(500).json({ | 70 | res.status(500).json({ |
| 69 | error: true, | 71 | error: true, |
| 70 | error_code: e.code, | 72 | error_code: e.code, |
| 71 | message: e.message, | 73 | message: e.message, |
| 72 | }); | 74 | }); |
| 73 | } | 75 | } |
| 74 | }; | 76 | }; |
| 75 | 77 | ||
| 76 | router.get('/', pageUpdate); | 78 | router.get('/', pageUpdate); |
| 77 | router.post('/', [express.urlencoded({ extended: true }), express.json()], pageUpdate); | 79 | router.post('/', [express.urlencoded({ extended: true }), express.json()], pageUpdate); |
| 78 | 80 |