-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
48 lines (41 loc) · 1.23 KB
/
server.js
File metadata and controls
48 lines (41 loc) · 1.23 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
require('dotenv').config();
const express = require('express');
const Paystack = require('@paystack/paystack-sdk');
const crypto = require('crypto');
const app = express();
const paystack = new Paystack(process.env.PAYSTACK_SECRET_KEY);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post('/initialize-transaction', async (req, res) => {
let { email, amount } = req.body;
try {
let initializedTransaction = await paystack.transaction.initialize({
email,
amount,
});
if (initializedTransaction.status === true) {
return res.status(200).json(initializedTransaction);
}
return res.status(400).json(initializedTransaction);
} catch (error) {
console.log(error);
return res.sendStatus(500);
}
});
app.post('/webhook', async (req, res) => {
//validate event
const hash = crypto
.createHmac('sha512', process.env.PAYSTACK_SECRET_KEY)
.update(JSON.stringify(req.body))
.digest('hex');
if (hash !== req.headers['x-paystack-signature']) {
return res.sendStatus(400);
}
res.sendStatus(200);
const event = req.body;
console.log(event);
// process webhook
});
app.listen(5000, () => {
console.log(`App running on http://localhost:5000`);
});