1
- 'use strict' ;
2
-
3
- const Stripe = require ( 'stripe' ) ;
4
- const Express = require ( 'express' ) ;
1
+ const stripe = require ( 'stripe' ) ( process . env . STRIPE_API_KEY ) ;
2
+ const express = require ( 'express' ) ;
3
+ const bodyParser = require ( 'body-parser' ) ;
5
4
6
5
/**
7
6
* You'll need to make sure this is externally accessible. ngrok (https://ngrok.com/)
@@ -11,66 +10,29 @@ const Express = require('express');
11
10
* STRIPE_API_KEY=sk_test_XXX WEBHOOK_SECRET=whsec_XXX node express.js
12
11
*/
13
12
14
- const apiKey = process . env . STRIPE_API_KEY ;
15
13
const webhookSecret = process . env . WEBHOOK_SECRET ;
14
+ const app = express ( ) ;
16
15
17
- const stripe = Stripe ( apiKey ) ;
18
-
19
- const router = Express . Router ( ) ;
20
-
21
- // Add the raw text body of the request to the `request` object
22
- function addRawBody ( req , res , next ) {
23
- req . setEncoding ( 'utf8' ) ;
24
-
25
- var data = '' ;
26
-
27
- req . on ( 'data' , function ( chunk ) {
28
- data += chunk ;
29
- } ) ;
30
-
31
- req . on ( 'end' , function ( ) {
32
- req . rawBody = data ;
33
-
34
- next ( ) ;
35
- } ) ;
36
- }
16
+ // Stripe requires the raw body to construct the event
17
+ app . post ( '/webhooks' , bodyParser . raw ( { type : 'application/json' } ) , ( req , res ) => {
18
+ const sig = req . headers [ 'stripe-signature' ] ;
37
19
38
- /**
39
- * You can either `use()` addRawBody on the Router...
40
- */
41
- // router.use(addRawBody);
42
-
43
- /**
44
- * ...or add it directly as middleware to the route.
45
- */
46
- router . post ( '/webhooks' , addRawBody , function ( request , response ) {
47
- var event ;
20
+ let event ;
48
21
49
22
try {
50
- // Try adding the Event as `request.event`
51
- event = stripe . webhooks . constructEvent (
52
- request . rawBody ,
53
- request . headers [ 'stripe-signature' ] ,
54
- webhookSecret
55
- ) ;
56
- } catch ( e ) {
57
- // If `constructEvent` throws an error, respond with the message and return.
58
- console . log ( 'Error' , e . message ) ;
59
-
60
- return response . status ( 400 ) . send ( 'Webhook Error:' + e . message ) ;
23
+ event = stripe . webhooks . constructEvent ( req . body , sig , webhookSecret ) ;
24
+ } catch ( err ) {
25
+ // On error, return the error message
26
+ return res . status ( 400 ) . send ( `Webhook Error: ${ err . message } ` ) ;
61
27
}
62
28
63
- console . log ( 'Success' , event . id ) ;
29
+ // Do something with event
30
+ console . log ( 'Success:' , event . id ) ;
64
31
65
- // Event was 'constructed', so we can respond with a 200 OK
66
- response . status ( 200 ) . send ( 'Signed Webhook Received: ' + event . id ) ;
32
+ // Return a response to acknowledge receipt of the event
33
+ res . json ( { received : true } ) ;
67
34
} ) ;
68
35
69
- // You could either create this app, or just return the `Router` for use in an
70
- // existing Express app - up to you!
71
-
72
- const app = Express ( ) ;
73
- app . use ( router ) ;
74
36
app . listen ( 3000 , function ( ) {
75
37
console . log ( 'Example app listening on port 3000!' )
76
38
} ) ;
0 commit comments