Blame view

lib/listener-partner/routers/pay.js 3.63 KB
9ced2cfdf   Adhidarma Hadiwinoto   PAY finished
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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);