Commit 5eccae05552e40605acf215afb1b64c2a00db4dd

Authored by Adhidarma Hadiwinoto
1 parent 92940f844e
Exists in master

Perbaikan pemanggilan composeCallbackUrl

Showing 2 changed files with 2 additions and 6 deletions Inline Diff

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