Commit da236d0f7bb117accdaedb471a8fd1281e7d05b9

Authored by Adhidarma Hadiwinoto
1 parent 3ccf2012e6
Exists in master

Fix urljoin di full endpoint

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

lib/hit/prepaid-topup.js
1 const MODULE_NAME = 'HIT.PREPAID-TOPUP'; 1 const MODULE_NAME = 'HIT.PREPAID-TOPUP';
2 2
3 const axios = require('axios'); 3 const axios = require('axios');
4 const urljoin = require('url-join'); 4 const urljoin = require('url-join');
5 const querystring = require('querystring'); 5 const querystring = require('querystring');
6 6
7 const config = require('komodo-sdk/config'); 7 const config = require('komodo-sdk/config');
8 const logger = require('tektrans-logger'); 8 const logger = require('tektrans-logger');
9 9
10 const report = require('../report/prepaid'); 10 const report = require('../report/prepaid');
11 const translateRc = require('../translate-rc'); 11 const translateRc = require('../translate-rc');
12 const composeCallbackUrl = require('./compose-callback-url'); 12 const composeCallbackUrl = require('./compose-callback-url');
13 const dumpReqRes = require('./dump-req-res'); 13 const dumpReqRes = require('./dump-req-res');
14 const axiosErrorIsSafe = require('./axios-error-is-safe'); 14 const axiosErrorIsSafe = require('./axios-error-is-safe');
15 15
16 module.exports = async (xid, task) => { 16 module.exports = async (xid, task) => {
17 logger.verbose(`${MODULE_NAME} 2272F01F: Processing task`, { 17 logger.verbose(`${MODULE_NAME} 2272F01F: Processing task`, {
18 xid, 18 xid,
19 task, 19 task,
20 }); 20 });
21 21
22 const params = { 22 const params = {
23 request_id: task.trx_id, 23 request_id: task.trx_id,
24 terminal_name: config.partner.terminal_name, 24 terminal_name: config.partner.terminal_name,
25 password: config.partner.password, 25 password: config.partner.password,
26 destination: task.destination, 26 destination: task.destination,
27 product_name: task.remote_product, 27 product_name: task.remote_product,
28 reverse_url: composeCallbackUrl(xid, task, false), 28 reverse_url: composeCallbackUrl(xid, task, false),
29 }; 29 };
30 30
31 const endpointUrl = urljoin(config.partner.url, '/topup'); 31 const endpointUrl = urljoin(config.partner.url, '/topup');
32 const fullEndpointUrl = urljoin( 32 const fullEndpointUrl = urljoin(
33 endpointUrl, 33 endpointUrl,
34 [ 34 [
35 '?', 35 '?',
36 querystring.stringify(params), 36 querystring.stringify(params),
37 ], 37 ].join(''),
38 ); 38 );
39 let lastResponse = null; 39 let lastResponse = null;
40 40
41 try { 41 try {
42 logger.verbose(`${MODULE_NAME} 4AAD4F99: Going to HIT prepaid endpoint`, { 42 logger.verbose(`${MODULE_NAME} 4AAD4F99: Going to HIT prepaid endpoint`, {
43 xid, 43 xid,
44 endpointUrl, 44 endpointUrl,
45 params, 45 params,
46 fullEndpointUrl: (!config.partner.method || config.partner.method === 'GET') 46 fullEndpointUrl: (!config.partner.method || config.partner.method === 'GET')
47 ? fullEndpointUrl 47 ? fullEndpointUrl
48 : endpointUrl, 48 : endpointUrl,
49 }); 49 });
50 50
51 report(xid, { 51 report(xid, {
52 trx_id: task.trx_id, 52 trx_id: task.trx_id,
53 rc: '68', 53 rc: '68',
54 message: { 54 message: {
55 xid, 55 xid,
56 msg: 'Sending request to partner', 56 msg: 'Sending request to partner',
57 method: config.partner.method || 'GET', 57 method: config.partner.method || 'GET',
58 endpointUrl, 58 endpointUrl,
59 }, 59 },
60 }); 60 });
61 61
62 const response = config.partner.method === 'POST' 62 const response = config.partner.method === 'POST'
63 ? await axios.post(endpointUrl, new URLSearchParams(params), { 63 ? await axios.post(endpointUrl, new URLSearchParams(params), {
64 headers: { 64 headers: {
65 'User-Agent': 'KOMODO-GW-HTTPGETX', 65 'User-Agent': 'KOMODO-GW-HTTPGETX',
66 }, 66 },
67 timeout: config.partner.hit_timeout_ms || 60 * 1000, 67 timeout: config.partner.hit_timeout_ms || 60 * 1000,
68 }) 68 })
69 : await axios.get(endpointUrl, { 69 : await axios.get(endpointUrl, {
70 headers: { 70 headers: {
71 'User-Agent': 'KOMODO-GW-HTTPGETX', 71 'User-Agent': 'KOMODO-GW-HTTPGETX',
72 }, 72 },
73 timeout: config.partner.hit_timeout_ms || 60 * 1000, 73 timeout: config.partner.hit_timeout_ms || 60 * 1000,
74 params, 74 params,
75 }); 75 });
76 76
77 if (!response) { 77 if (!response) {
78 const e = new Error(`${MODULE_NAME} 8CF4E04D: Empty response`); 78 const e = new Error(`${MODULE_NAME} 8CF4E04D: Empty response`);
79 e.rc = '68'; 79 e.rc = '68';
80 e.response = response; 80 e.response = response;
81 throw e; 81 throw e;
82 } 82 }
83 83
84 if (!response.data) { 84 if (!response.data) {
85 const e = new Error(`${MODULE_NAME} E72B5A53: Empty response data`); 85 const e = new Error(`${MODULE_NAME} E72B5A53: Empty response data`);
86 e.rc = '68'; 86 e.rc = '68';
87 e.response = response; 87 e.response = response;
88 throw e; 88 throw e;
89 } 89 }
90 90
91 if (typeof response.data !== 'object') { 91 if (typeof response.data !== 'object') {
92 const e = new Error(`${MODULE_NAME} 507680AB: Response data is not a JSON`); 92 const e = new Error(`${MODULE_NAME} 507680AB: Response data is not a JSON`);
93 e.rc = '68'; 93 e.rc = '68';
94 e.response = response; 94 e.response = response;
95 throw e; 95 throw e;
96 } 96 }
97 97
98 lastResponse = response; 98 lastResponse = response;
99 99
100 logger.verbose(`${MODULE_NAME} E51AFBBA: Got a direct response`, { 100 logger.verbose(`${MODULE_NAME} E51AFBBA: Got a direct response`, {
101 xid, 101 xid,
102 responseBody: response.data, 102 responseBody: response.data,
103 }); 103 });
104 104
105 report(xid, { 105 report(xid, {
106 trx_id: task.trx_id, 106 trx_id: task.trx_id,
107 rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc 107 rc: response.data.rc ? translateRc[response.data.rc] || response.data.rc
108 : '68', 108 : '68',
109 sn: response.data.sn || null, 109 sn: response.data.sn || null,
110 amount: Number(response.data.amount) || undefined, 110 amount: Number(response.data.amount) || undefined,
111 balance: Number(response.data.ending_balance) || undefined, 111 balance: Number(response.data.ending_balance) || undefined,
112 message: { 112 message: {
113 xid, 113 xid,
114 'DIRECT-RESPONSE': response.data, 114 'DIRECT-RESPONSE': response.data,
115 }, 115 },
116 }); 116 });
117 } catch (e) { 117 } catch (e) {
118 const rc = e.rc 118 const rc = e.rc
119 || (axiosErrorIsSafe(e) && '91') 119 || (axiosErrorIsSafe(e) && '91')
120 || '68'; 120 || '68';
121 121
122 logger.warn(`${MODULE_NAME} 8E8E49F5: Exception`, { 122 logger.warn(`${MODULE_NAME} 8E8E49F5: Exception`, {
123 xid, 123 xid,
124 eCode: e.code, 124 eCode: e.code,
125 eMessage: e.message, 125 eMessage: e.message,
126 eRc: e.rc, 126 eRc: e.rc,
127 rc, 127 rc,
128 responseHttpStatus: e.response && e.response.status, 128 responseHttpStatus: e.response && e.response.status,
129 responseBody: e.response && e.response.data, 129 responseBody: e.response && e.response.data,
130 }); 130 });
131 131
132 lastResponse = e.response; 132 lastResponse = e.response;
133 133
134 report(xid, { 134 report(xid, {
135 trx_id: task.trx_id, 135 trx_id: task.trx_id,
136 rc, 136 rc,
137 message: { 137 message: {
138 xid, 138 xid,
139 'KOMODO-GW-ERROR': { 139 'KOMODO-GW-ERROR': {
140 eCode: e.code, 140 eCode: e.code,
141 eMessage: e.message, 141 eMessage: e.message,
142 responseHttpStatus: e.response && e.response.status, 142 responseHttpStatus: e.response && e.response.status,
143 responseBody: e.response && e.response.data, 143 responseBody: e.response && e.response.data,
144 }, 144 },
145 }, 145 },
146 }); 146 });
147 } finally { 147 } finally {
148 dumpReqRes( 148 dumpReqRes(
149 xid, 149 xid,
150 task, 150 task,
151 config.partner.method || 'GET', 151 config.partner.method || 'GET',
152 endpointUrl, 152 endpointUrl,
153 params, 153 params,
154 lastResponse && lastResponse.data, 154 lastResponse && lastResponse.data,
155 lastResponse && lastResponse.status, 155 lastResponse && lastResponse.status,
156 lastResponse, 156 lastResponse,
157 false, 157 false,
158 ); 158 );
159 } 159 }
160 }; 160 };
161 161