-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
43 lines (35 loc) · 1.21 KB
/
app.ts
File metadata and controls
43 lines (35 loc) · 1.21 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
import express, { Request, Response } from 'express'
import { WebhookUtils, InvalidSignatureError } from '@getopenpay/client'
const app = express()
const port = 8080
// Add your secret key from the webhook page here.
const WH_SECRET_KEY = ''
app.use(express.json())
app.get('/', (req: Request, res: Response) => {
res.send('Hello World!')
})
const verifyWebhookRequest = (request: Request) => {
const signature_digest = request.headers['signature-digest'] as string;
if (!signature_digest) {
throw new Error('Missing signature-digest header');
}
const webhookUtils = new WebhookUtils();
try {
webhookUtils.validatePayload(request.body.data, signature_digest, WH_SECRET_KEY);
console.log('Webhook signature is valid');
} catch (error) {
if (error instanceof InvalidSignatureError) {
console.error('Invalid webhook signature');
} else {
console.error('Error verifying webhook:', error);
}
}
}
app.post('/webhook', (req: Request, res: Response) => {
console.log('Received webhook:', req.body)
verifyWebhookRequest(req);
res.status(200).json({ message: 'Webhook received successfully' })
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})