Commit 9e96d55f801ee7ad67a45ae7af0a83f3be32098e

Authored by Adhidarma Hadiwinoto
1 parent 7b70133158
Exists in master

Ready to test

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

File was created 1 const DEFAULT_CORE_REQUEST_TIMEOUT = 5 * 1000;
2 const MAX_REPORT_RETRY = 30;
3 const REPORT_RETRY_SLEEP_MS = 2000;
4
5 const axios = require('axios').default;
6
7 const config = require('komodo-sdk/config');
8 const coreUrl = require('komodo-sdk/core-url');
9 const logger = require('komodo-sdk/logger');
10
11 let partner;
12 let first = true;
13 let coreIsHealthy = null;
14 let skipNext = 0;
15
16 const DEFAULT_CORE_AXIOS_CONFIG = {
17 headers: { 'Content-Type': 'application/json' },
18 timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT,
19 };
20
21 const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG;
22 const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG;
23
24 function detectCoreStatusChange(isHealthy, url, err) {
25 if (coreIsHealthy !== isHealthy) {
26 coreIsHealthy = isHealthy;
27
28 if (isHealthy) {
29 logger.info('POSTPAID2-SDK: CORE is healthy now', { url, err: err && err.message });
30 } else {
31 logger.warn('POSTPAID2-SDK: CORE is unhealthy now', { url, err: err && err.message });
32 }
33 }
34 }
35
36 async function getTask() {
37 if (!partner) return;
38
39 if (skipNext > 0) {
40 skipNext -= 1;
41 return;
42 }
43
44 if (first) {
45 first = false;
46 logger.verbose('POSTPAID2-SDK: first pull request to CORE');
47 }
48
49 const coreEndpoint = `${coreUrl}/postpaid2/task`;
50
51 const payload = {
52 handler: config.handler || config.handler_name,
53 products: config.products || [],
54 };
55
56 let task;
57 try {
58 const result = await axios.post(
59 coreEndpoint, payload, GET_TASK_AXIOS_CONFIG,
60 );
61
62 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6');
63 if (result && result.data && result.data.task && !result.data.task.postpaid) throw new Error('POSTPAID2-SDK: CORE returning non postpaid task on pulling task. MARK-B338CEF8');
64
65 detectCoreStatusChange(true, coreEndpoint);
66 task = result.data.task;
67 } catch (e) {
68 skipNext = 3;
69 detectCoreStatusChange(false, coreEndpoint, e);
70 }
71
72 if (!task) return;
73
74 task.remote_product = ((config.remote_products || {})[task.product]) || task.product;
75
76 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task });
77
78 if (task.inquiry_only) {
79 if (typeof partner.inquiry === 'function') partner.inquiry(task);
80 return;
81 }
82
83 if (typeof partner.pay === 'function') partner.pay(task);
84 }
85
86 const getTaskLooper = async () => {
87 await getTask();
88
89 setTimeout(() => {
90 getTaskLooper();
91 }, config.pull_interval_ms || 1000);
92 };
93
94 exports.setPartner = (val) => {
95 partner = val;
96 setInterval(getTaskLooper, config.pull_interval_ms || 1000);
97 };
98
99 exports.report = async (data, xid, retry) => {
100 if (!data.trx_id) {
101 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data });
102 return;
103 }
104
105 if (!data.rc || typeof data.rc !== 'string') {
106 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', {
107 xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc,
108 });
109 }
110
111 const dataToReport = {
112 handler: config.handler || config.handler_name,
113 command: data.command,
114 trx_id: data.trx_id,
115 rc: data.rc,
116 sn: data.sn,
117 amount: data.amount,
118 base_bill_amount: data.base_bill_amount,
119 bill_count: data.bill_count,
120 message: data.message,
121 info: data.info,
122 detail: data.detail,
123 data: data.data,
124 };
125
126 logger.verbose('POSTPAID2-SDK: Going to report to CORE', {
127 xid,
128 trxId: data.trx_id,
129 rc: data.rc,
130 sn: data.sn,
131 amount: data.amount,
132 baseBillAmount: data.base_bill_amount,
133 });
134
135 const coreEndpoint = `${coreUrl}/postpaid2/report`;
136
137 try {
138 const reportResult = await axios.post(
139 coreEndpoint, JSON.stringify(dataToReport), REPORT_AXIOS_CONFIG,
140 );
141
142 if (!reportResult) {
143 logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB');
144 }
145 } catch (e) {
146 const newRetry = (retry || 0) + 1;
147 logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', {
148 xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY,
149 });
150
151 if (!this || !this.report) {
152 logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!');
153 return;
154 }
155
156 if (newRetry < MAX_REPORT_RETRY) {
157 setTimeout(() => {
158 logger.info('POSTPAID2-SDK: Retrying to send report to CORE', {
159 xid,
160 trxId: data.trx_id,
161 rc: data.rc,
162 sn: data.sn,
163 amount: data.amount,
164 baseBillAmount: data.base_bill_amount,
165 });
166
167 this.report(data, xid, newRetry);
168 }, REPORT_RETRY_SLEEP_MS);
169 }
170 }
171 };
172