-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (71 loc) · 2.89 KB
/
server.js
File metadata and controls
97 lines (71 loc) · 2.89 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
const express = require('express');
const bodyParser = require('body-parser');
const crypto = require('crypto');
require('dotenv').config();
const app = express();
// Import the Prisma client
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error'],
errorFormat: 'minimal',
});
// Replace with your Monnify client secret
const DEFAULT_MERCHANT_CLIENT_SECRET = '4SY6TNL8CK3VPRSBTHTRG2N8XXEGC6NL';
// Middleware for parsing JSON
app.use(bodyParser.json());
// Verify the authenticity of the incoming webhook by comparing the computed hash with 'monnify-signature'
function verifyWebhookSignature(requestBody, signature) {
const computedHash = crypto.createHmac('sha512', DEFAULT_MERCHANT_CLIENT_SECRET)
.update(requestBody)
.digest('hex');
return computedHash === signature;
}
// Handle incoming Monnify webhook events
app.post('/webhook', async (req, res) => {
const requestBody = JSON.stringify(req.body);
const signature = req.header('monnify-signature');
if (verifyWebhookSignature(requestBody, signature)) {
// Signature is valid, process the webhook payload.
const eventData = req.body.eventData;
const transactionReference = eventData.transactionReference;
const amount = eventData.amountPaid
console.log('Received webhook event:', eventData);
// Your logic here...
// Check to see if you've processed this webhook before using the reference to prevent double credit
const isDuplicate = await checkDuplicateTransaction(transactionReference);
if (isDuplicate) {
console.log('Duplicate transaction found');
return res.status(200).send('Duplicate transaction found');
}
createTransaction(transactionReference, amount, 'pending');
giveCustomerValue(transactionReference); // Corrected function name
res.status(200).send('Webhook processed successfully');
} else {
// Signature is not valid; do not trust the webhook.
res.status(400).send('Invalid signature');
}
});
function giveCustomerValue(transactionReference) {
// Here you can give value to your customer, e.g., update balance, send SMS, update transaction table, etc.
console.log('Provided Value to Customer for Transaction Reference:', transactionReference);
}
async function createTransaction(transactionReference, amount, status) {
return prisma.transaction.create({
data: {
transactionReference,
amount,
status,
},
});
}
async function checkDuplicateTransaction(transactionReference) {
const existingTransaction = await prisma.transaction.findUnique({
where: { transactionReference },
});
return !!existingTransaction;
}
// Start the server
const port = 3000; // Change this to the desired port
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});