Skip to content

Instantly share code, notes, and snippets.

@Suvink
Created May 22, 2022 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Suvink/62cb552b016f077619c6736b2564c26f to your computer and use it in GitHub Desktop.
Save Suvink/62cb552b016f077619c6736b2564c26f to your computer and use it in GitHub Desktop.
Stripe Success Endpoint Sample
// server.js
//
// Use this sample code to handle webhook events in your integration.
//
// 1) Paste this code into a new file (server.js)
//
// 2) Install dependencies
// npm install stripe
// npm install express
//
// 3) Run the server on http://localhost:4242
// node server.js
const stripe = require('stripe');
const express = require('express');
const app = express();
// This is your Stripe CLI webhook secret for testing your endpoint locally.
const endpointSecret = "whsec_afc1ad875ea9be44dd98a855eb45003f81bede390286c932c4c3717454d525df";
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the event
switch (event.type) {
case 'checkout.session.async_payment_succeeded':
const session = event.data.object;
// Then define and call a function to handle the event checkout.session.async_payment_succeeded
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
});
app.listen(4242, () => console.log('Running on port 4242'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment