-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (42 loc) · 1.48 KB
/
server.js
File metadata and controls
50 lines (42 loc) · 1.48 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
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,
...req.body // pass any other params as key value pairs (see other optional params here: https://paystack.com/docs/api/transaction/#initialize)
});
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( process.env.PORT || 5001, () => {
process.env.PORT ? console.log(`App running on http://localhost:${process.env.PORT}`) : console.log(`App running on http://localhost:5001`);
});