-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebhook_nodejs.js
More file actions
223 lines (191 loc) · 6.69 KB
/
Copy pathwebhook_nodejs.js
File metadata and controls
223 lines (191 loc) · 6.69 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Mutasibank Webhook Handler - Node.js/Express
*
* Secure webhook receiver with HMAC-SHA256 signature verification
*/
const express = require('express');
const crypto = require('crypto');
const app = express();
// Configuration
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'your_webhook_secret_here';
const API_TOKEN = process.env.API_TOKEN || 'your_api_token_here';
const TIMESTAMP_TOLERANCE = 300; // 5 minutes
// IMPORTANT: Use raw body for signature verification
app.use(express.json({
verify: (req, res, buf) => {
req.rawBody = buf.toString('utf8');
}
}));
// Webhook endpoint
app.post('/webhook/mutasibank', async (req, res) => {
try {
// Step 1: Extract headers
const signature = req.headers['x-mutasibank-signature'];
const timestamp = parseInt(req.headers['x-mutasibank-timestamp']);
const webhookId = req.headers['x-mutasibank-webhook-id'];
// Step 2: Validate headers
if (!signature || !timestamp) {
console.error('Missing signature headers');
return res.status(401).json({
error: 'Missing signature headers'
});
}
// Step 3: Check timestamp (prevent replay attacks)
const currentTime = Math.floor(Date.now() / 1000);
const timeDiff = Math.abs(currentTime - timestamp);
if (timeDiff > TIMESTAMP_TOLERANCE) {
console.error(`Timestamp expired. Diff: ${timeDiff}s`);
return res.status(401).json({
error: 'Request expired',
timestamp_received: timestamp,
current_time: currentTime,
difference_seconds: timeDiff
});
}
// Step 4: Verify signature
const payload = req.rawBody || JSON.stringify(req.body);
const expectedSignature = crypto
.createHmac('sha256', WEBHOOK_SECRET)
.update(payload)
.digest('hex');
// Use timing-safe comparison
if (!crypto.timingSafeEqual(
Buffer.from(signature),
Buffer.from(expectedSignature)
)) {
console.error('Invalid signature', { webhookId });
return res.status(401).json({
error: 'Invalid signature'
});
}
console.log('✅ Signature verified', { webhookId });
// Step 5: Validate API token (double authentication)
const data = req.body;
if (data.api_key !== API_TOKEN) {
console.error('Invalid API token');
return res.status(401).json({
error: 'Invalid API token'
});
}
// Step 6: Process webhook data
const accountId = data.account_id;
const accountNumber = data.account_number;
const module = data.module; // bca, bri, mandiri, bni
const balance = data.balance;
const transactions = data.data_mutasi;
console.log(`Processing webhook for account: ${accountNumber}`, {
accountId,
module,
transactionCount: transactions.length,
balance
});
// Process each transaction
for (const transaction of transactions) {
const {
id: transactionId,
transaction_date,
description,
type, // CR (credit) or DB (debit)
amount,
balance: txBalance
} = transaction;
console.log(`Processing transaction: ${transactionId}`, {
date: transaction_date,
type,
amount
});
// ================================================================
// YOUR BUSINESS LOGIC HERE
// ================================================================
// Example 1: Auto-confirm order
if (type === 'CR') {
const orderId = extractOrderId(description);
if (orderId) {
await confirmOrder(orderId, amount, transactionId);
}
}
// Example 2: Process top-up
// await processTopUp(description, amount, transactionId);
// Example 3: Save to database
// await saveTransaction(transaction);
}
// Step 7: Return success response
console.log('✅ Webhook processed successfully', {
webhookId,
transactionsProcessed: transactions.length
});
res.json({
success: true,
webhook_id: webhookId,
transactions_processed: transactions.length,
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Webhook processing error:', error);
res.status(500).json({
error: 'Internal server error',
message: error.message
});
}
});
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
/**
* Extract order ID from transaction description
*/
function extractOrderId(description) {
// Match patterns like "ORDER-12345" or "TRF 12345"
const patterns = [
/ORDER[:\-\s]*(\d+)/i,
/TRF\s+(\d+)/i,
/INV[:\-\s]*(\d+)/i
];
for (const pattern of patterns) {
const match = description.match(pattern);
if (match) {
return match[1];
}
}
return null;
}
/**
* Confirm order (example)
*/
async function confirmOrder(orderId, amount, transactionId) {
// Your database logic here
console.log(`Order confirmed: ${orderId}`, { amount, transactionId });
// Example:
// await db.orders.update(
// { id: orderId },
// {
// status: 'paid',
// payment_amount: amount,
// transaction_id: transactionId,
// paid_at: new Date()
// }
// );
}
/**
* Save transaction to database (example)
*/
async function saveTransaction(transaction) {
// Example using Mongoose (MongoDB)
// await Transaction.create({
// transactionId: transaction.id,
// date: new Date(transaction.transaction_date),
// description: transaction.description,
// type: transaction.type,
// amount: transaction.amount,
// balance: transaction.balance,
// processedAt: new Date()
// });
console.log('Transaction saved:', transaction.id);
}
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`🚀 Webhook server running on port ${PORT}`);
console.log(`📝 Endpoint: POST http://localhost:${PORT}/webhook/mutasibank`);
});
module.exports = app;