Blame view

lib/partner-listener/routers/topup.js 3.91 KB
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
1
  const MODULE_NAME = 'PARTNER-LISTENER.ROUTER.TOPUP';
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
2
3
4
5
6
7
  const express = require('express');
  
  const config = require('komodo-sdk/config');
  const logger = require('komodo-sdk/logger');
  const coreapi = require('komodo-sdk/coreapi');
  // const coreapi = require('../../coreapi');
cd4feda87   Adhidarma Hadiwinoto   APISERVER and MATRIX
8
  const matrix = require('../../matrix');
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
9
10
11
12
13
  
  const router = express.Router();
  module.exports = router;
  
  function onInvalidParameter(missingParameter, req, res) {
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
14
      logger.verbose(`${MODULE_NAME} 1536D577: Undefined ${missingParameter} parameter`, {
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
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
          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 = {};
  
      if (!req.body.terminal_name && !req.query.terminal_name) {
          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) {
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
57
      const { xid } = res.locals;
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
58
      const terminalName = `${req.body.terminal_name || req.query.terminal_name}@${req.ip.replace(/^::ffff:/, '')}`;
b403448ad   Adhidarma Hadiwinoto   Refactor some matrix
59
      matrix.core.sent += 1;
cd4feda87   Adhidarma Hadiwinoto   APISERVER and MATRIX
60

f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
61
      const [err, coreResponse] = await coreapi({
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
62
          xid,
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
63
64
65
66
67
68
69
70
          path: '/prepaid/buy',
          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',
404034960   Adhidarma Hadiwinoto   Perbaikan paramet...
71
72
              report_ip: config.listener.core.ip || null,
              report_port: config.listener.core.port,
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
73
74
75
              reverse_url: req.body.reverse_url || req.query.reverse_url || null,
          },
      });
bdbbbf27d   Adhidarma Hadiwinoto   TOPUP: Forward di...
76
      if (err || !coreResponse) {
b403448ad   Adhidarma Hadiwinoto   Refactor some matrix
77
78
79
80
81
82
83
84
          matrix.core.sent_failed += 1;
          matrix.core.last_error = {
              xid,
              ts: new Date(),
              e: err,
              eCode: err.code,
              eMessage: err.message,
          };
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
85
86
87
88
89
          logger.warn(`${MODULE_NAME} 8DEBE15F: ERROR on /prepaid/buy response`, {
              xid,
              err,
              coreResponseTypeof: typeof coreResponse,
              coreResponse,
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
90
91
92
93
          });
          res.end('INVALID CORE RESPONSE');
          return;
      }
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
      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,
050d92ffb   Adhidarma Hadiwinoto   Undefined on miss...
109
          sn: coreResponse.sn || undefined,
70df828c5   Adhidarma Hadiwinoto   TOPUP: undefined ...
110
111
          amount: Number(coreResponse.amount) || undefined,
          ending_balance: Number(coreResponse.ending_balance) || undefined,
9e61d939f   Adhidarma Hadiwinoto   TOPUP: cleansed
112
113
114
115
          message: coreResponse.message,
      };
  
      res.json(responseToPartner);
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
116
  }
65f444c36   Adhidarma Hadiwinoto   Activating /topup
117
  // router.all('/', (req, res) => { res.status(404).end('404: Not implemented yet'); });
f2c18879a   Adhidarma Hadiwinoto   INQUIRY finished
118
  router.get('/', pagePrerequisite, pageIndex);