Commit 1182d0fd69d1ba5fde9d0d1f29278132bf2174e6
1 parent
e7bcc28f49
Exists in
master
Perbaikan url webhook
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
lib/webhook-sender.js
1 | const MODULE_NAME = 'WEBHOOK-SENDER'; | 1 | const MODULE_NAME = 'WEBHOOK-SENDER'; |
2 | 2 | ||
3 | const axios = require('axios'); | 3 | const axios = require('axios'); |
4 | const moment = require('moment'); | 4 | const moment = require('moment'); |
5 | const fs = require('fs'); | 5 | const fs = require('fs'); |
6 | const path = require('path'); | 6 | const path = require('path'); |
7 | const stringify = require('json-stringify-pretty-compact'); | 7 | const stringify = require('json-stringify-pretty-compact'); |
8 | const config = require('komodo-sdk/config'); | 8 | const config = require('komodo-sdk/config'); |
9 | const logger = require('tektrans-logger'); | 9 | const logger = require('tektrans-logger'); |
10 | 10 | ||
11 | const DEFAULT_MAX_RETRY = 10; | 11 | const DEFAULT_MAX_RETRY = 10; |
12 | const DEFAULT_SLEEP_BEFORE_RETRY_MS = 10 * 1000; | 12 | const DEFAULT_SLEEP_BEFORE_RETRY_MS = 10 * 1000; |
13 | 13 | ||
14 | const maxRetry = Number(config.webhook && config.webhook.max_retry) | 14 | const maxRetry = Number(config.webhook && config.webhook.max_retry) |
15 | || DEFAULT_MAX_RETRY; | 15 | || DEFAULT_MAX_RETRY; |
16 | const sleepBeforeRetryMs = Number(config.webhook && config.webhook.sleep_before_retry_ms) | 16 | const sleepBeforeRetryMs = Number(config.webhook && config.webhook.sleep_before_retry_ms) |
17 | || DEFAULT_SLEEP_BEFORE_RETRY_MS; | 17 | || DEFAULT_SLEEP_BEFORE_RETRY_MS; |
18 | 18 | ||
19 | const baseDumpDir = path.join('dump', 'webhook-sender'); | 19 | const baseDumpDir = path.join('dump', 'webhook-sender'); |
20 | if (!fs.existsSync(baseDumpDir)) { | 20 | if (!fs.existsSync(baseDumpDir)) { |
21 | fs.mkdirSync(baseDumpDir, { recursive: true }); | 21 | fs.mkdirSync(baseDumpDir, { recursive: true }); |
22 | } | 22 | } |
23 | const lastDumpFileName = path.join(baseDumpDir, 'last.json'); | 23 | const lastDumpFileName = path.join(baseDumpDir, 'last.json'); |
24 | 24 | ||
25 | const sleepMs = (ms) => new Promise((resolve) => { | 25 | const sleepMs = (ms) => new Promise((resolve) => { |
26 | setTimeout(() => { | 26 | setTimeout(() => { |
27 | resolve(); | 27 | resolve(); |
28 | }, ms); | 28 | }, ms); |
29 | }); | 29 | }); |
30 | 30 | ||
31 | const dumper = async (xid, webhookType, body) => { | 31 | const dumper = async (xid, webhookType, body) => { |
32 | if (!config.webhook || !config.webhook.dump) { | 32 | if (!config.webhook || !config.webhook.dump) { |
33 | return; | 33 | return; |
34 | } | 34 | } |
35 | 35 | ||
36 | try { | 36 | try { |
37 | const filename = [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_'); | 37 | const filename = [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_'); |
38 | await fs.promises.writeFile( | 38 | await fs.promises.writeFile( |
39 | path.join(baseDumpDir, `${filename}.json`), | 39 | path.join(baseDumpDir, `${filename}.json`), |
40 | stringify({ webhookType, body }), | 40 | stringify({ webhookType, body }), |
41 | ); | 41 | ); |
42 | 42 | ||
43 | await fs.promises.writeFile( | 43 | await fs.promises.writeFile( |
44 | lastDumpFileName, | 44 | lastDumpFileName, |
45 | stringify({ webhookType, body }), | 45 | stringify({ webhookType, body }), |
46 | ); | 46 | ); |
47 | } catch (e) { | 47 | } catch (e) { |
48 | logger.warn(`${MODULE_NAME} D3EF00D9: Exception on dumper`, { | 48 | logger.warn(`${MODULE_NAME} D3EF00D9: Exception on dumper`, { |
49 | xid, | 49 | xid, |
50 | eCode: e.code, | 50 | eCode: e.code, |
51 | eMessage: e.message || e.toString(), | 51 | eMessage: e.message || e.toString(), |
52 | }); | 52 | }); |
53 | } | 53 | } |
54 | }; | 54 | }; |
55 | 55 | ||
56 | const sender = async (xid, webhookType, body, retry) => { | 56 | const sender = async (xid, webhookType, body, retry) => { |
57 | if (!config.webhook || !config.webhook.url) { | 57 | if (!config.webhook || !config.webhook.url) { |
58 | return; | 58 | return; |
59 | } | 59 | } |
60 | 60 | ||
61 | try { | 61 | try { |
62 | logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, { | 62 | logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, { |
63 | xid, | 63 | xid, |
64 | webhookType, | 64 | webhookType, |
65 | partner: config.webhook.url, | 65 | partner: config.webhook.url, |
66 | trxId: body.transaction_id, | 66 | trxId: body.transaction_id, |
67 | request_id: body.request_id, | 67 | request_id: body.request_id, |
68 | }); | 68 | }); |
69 | 69 | ||
70 | await axios.post(config.listener.partner.webhook, { | 70 | await axios.post(config.webhook.url, { |
71 | webhookType, | 71 | webhookType, |
72 | body, | 72 | body, |
73 | }); | 73 | }); |
74 | 74 | ||
75 | await dumper(xid, webhookType, body); | 75 | await dumper(xid, webhookType, body); |
76 | 76 | ||
77 | logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, { | 77 | logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, { |
78 | xid, | 78 | xid, |
79 | webhookType, | 79 | webhookType, |
80 | partner: config.listener.partner.webhook, | 80 | partner: config.webhook.url, |
81 | }); | 81 | }); |
82 | } catch (e) { | 82 | } catch (e) { |
83 | logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, { | 83 | logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, { |
84 | xid, | 84 | xid, |
85 | httpStatusCode: e.response && e.response.status, | 85 | httpStatusCode: e.response && e.response.status, |
86 | eCode: e.code, | 86 | eCode: e.code, |
87 | eMessage: e.message || e.toString(), | 87 | eMessage: e.message || e.toString(), |
88 | retried: retry || 0, | 88 | retried: retry || 0, |
89 | maxRetry, | 89 | maxRetry, |
90 | }); | 90 | }); |
91 | 91 | ||
92 | if ((retry || 0) >= maxRetry) { | 92 | if ((retry || 0) >= maxRetry) { |
93 | logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, { | 93 | logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, { |
94 | xid, | 94 | xid, |
95 | }); | 95 | }); |
96 | 96 | ||
97 | return; | 97 | return; |
98 | } | 98 | } |
99 | 99 | ||
100 | await sleepMs(sleepBeforeRetryMs); | 100 | await sleepMs(sleepBeforeRetryMs); |
101 | await sender(xid, webhookType, body, (retry || 0) + 1); | 101 | await sender(xid, webhookType, body, (retry || 0) + 1); |
102 | } | 102 | } |
103 | }; | 103 | }; |
104 | module.exports = sender; | 104 | module.exports = sender; |
105 | 105 |