-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (76 loc) · 3.42 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 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_JLJwb4X9TD05k5TJ0t6a4LDxxtRkG6xb";
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 'payment_intent.amount_capturable_updated':
const paymentIntent = event.data.object;
console.log('payment_intent.amount_capturable_updated', paymentIntent)
// Then define and call a function to handle the event payment_intent.amount_capturable_updated
break;
case 'payment_intent.canceled':
const paymentIntent1 = event.data.object;
console.log('payment_intent.canceled', paymentIntent1)
// Then define and call a function to handle the event payment_intent.canceled
break;
case 'payment_intent.created':
const paymentIntent2 = event.data.object;
console.log('payment_intent.created', paymentIntent2)
// Then define and call a function to handle the event payment_intent.created
break;
case 'payment_intent.partially_funded':
const paymentIntent3 = event.data.object;
console.log('payment_intent.partially_funded', paymentIntent3)
// Then define and call a function to handle the event payment_intent.partially_funded
break;
case 'payment_intent.payment_failed':
const paymentIntent4 = event.data.object;
console.log('payment_intent.payment_failed', paymentIntent4)
// Then define and call a function to handle the event payment_intent.payment_failed
break;
case 'payment_intent.processing':
const paymentIntent5 = event.data.object;
console.log('payment_intent.processing', paymentIntent5)
// Then define and call a function to handle the event payment_intent.processing
break;
case 'payment_intent.requires_action':
const paymentIntent6 = event.data.object;
console.log('payment_intent.requires_action', paymentIntent6)
// Then define and call a function to handle the event payment_intent.requires_action
break;
case 'payment_intent.succeeded':
const paymentIntent7 = event.data.object;
console.log('payment_intent.succeeded', paymentIntent7)
// Then define and call a function to handle the event payment_intent.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'));