total_1ère
This commit is contained in:
63
controllers/webhook_stripe_exemple.js
Normal file
63
controllers/webhook_stripe_exemple.js
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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
|
||||
|
||||
// The library needs to be configured with your account's secret key.
|
||||
// Ensure the key is kept out of any version control system you might be using.
|
||||
const stripe = require('stripe')('sk_test_...');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
|
||||
// This is your Stripe CLI webhook secret for testing your endpoint locally.
|
||||
const endpointSecret = "whsec_fed56735156a81c3ae36f3bb5dbd4dece5412ba299f55702c5cf077c69ce3a11";
|
||||
|
||||
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_failed':
|
||||
const checkoutSessionAsyncPaymentFailed = event.data.object;
|
||||
// Then define and call a function to handle the event checkout.session.async_payment_failed
|
||||
break;
|
||||
case 'checkout.session.async_payment_succeeded':
|
||||
const checkoutSessionAsyncPaymentSucceeded = event.data.object;
|
||||
// Then define and call a function to handle the event checkout.session.async_payment_succeeded
|
||||
break;
|
||||
case 'checkout.session.completed':
|
||||
const checkoutSessionCompleted = event.data.object;
|
||||
// Then define and call a function to handle the event checkout.session.completed
|
||||
break;
|
||||
case 'checkout.session.expired':
|
||||
const checkoutSessionExpired = event.data.object;
|
||||
// Then define and call a function to handle the event checkout.session.expired
|
||||
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'));
|
||||
Reference in New Issue
Block a user