Skip to main content

Payment Webhook Setup

Learn how to handle payment webhooks from popular payment processors like Stripe, PayPal, and others.

Stripe Payment Webhooks

Setup

  1. Go to Stripe Dashboard → Developers → Webhooks
  2. Add your webhook endpoint URL
  3. Select events: payment_intent.succeeded, payment_intent.payment_failed

Handler Implementation

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

app.post('/webhooks/stripe', express.raw({type: 'application/json'}), (req, res) => {
const sig = req.headers['stripe-signature'];
let event;

try {
event = stripe.webhooks.constructEvent(req.body, sig, process.env.STRIPE_ENDPOINT_SECRET);
} catch (err) {
console.log(`Webhook signature verification failed.`, err.message);
return res.status(400).send(`Webhook Error: ${err.message}`);
}

switch (event.type) {
case 'payment_intent.succeeded':
await handlePaymentSuccess(event.data.object);
break;
case 'payment_intent.payment_failed':
await handlePaymentFailure(event.data.object);
break;
}

res.json({received: true});
});

PayPal Webhooks

PayPal IPN Handler

app.post('/webhooks/paypal', async (req, res) => {
const verification = await verifyPayPalIPN(req.body);

if (!verification) {
return res.status(400).send('Invalid IPN');
}

const paymentStatus = req.body.payment_status;

switch (paymentStatus) {
case 'Completed':
await handlePayPalPaymentSuccess(req.body);
break;
case 'Failed':
case 'Denied':
await handlePayPalPaymentFailure(req.body);
break;
}

res.status(200).send('OK');
});

This section is under development. More payment webhook examples coming soon.