-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhook_handler-Script1.js
More file actions
38 lines (29 loc) · 1.08 KB
/
Webhook_handler-Script1.js
File metadata and controls
38 lines (29 loc) · 1.08 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
const express = require('express');
const bodyParser = require('body-parser');
const stripe = require('stripe')('your_stripe_secret_key');
const app = express();
const endpointSecret = 'your_webhook_secret';
app.use(bodyParser.raw({ type: 'application/json' }));
app.post('/webhook', (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
stripe listen --forward-to localhost:4242/webhook
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
// Handle successful payment here
// Send signal to PLC
sendSignalToPLC(session.amount_total / 100); // Assuming amount is in cents
}
response.json({ received: true });
});
function sendSignalToPLC(tokens) {
// Logic to send signal to PLC
}
app.listen(4242, () => console.log('Running on port 4242'));
// JavaScript source code