1
1
import Stripe from 'stripe' ;
2
- import { NextRequest } from " next/server" ;
2
+ import { NextRequest } from ' next/server' ;
3
3
4
4
export const config = {
5
- api : {
6
- bodyParser : false ,
7
- } ,
5
+ api : {
6
+ bodyParser : false ,
7
+ } ,
8
8
} ;
9
9
10
- export async function POST ( req : NextRequest ) {
10
+ export async function POST ( req : NextRequest ) {
11
11
const stripe = new Stripe ( process . env . STRIPE_SECRET_KEY ) ;
12
- const sig = req . headers . get ( " stripe-signature" )
13
- const wh_sec = process . env . STRIPE_WEBHOOK_SECRET
12
+ const sig = req . headers . get ( ' stripe-signature' ) ;
13
+ const wh_sec = process . env . STRIPE_WEBHOOK_SECRET ;
14
14
let event : Stripe . Event ;
15
- if ( ! sig ) {
16
- throw new Error ( " Invalid Signature" )
15
+ if ( ! sig ) {
16
+ throw new Error ( ' Invalid Signature' ) ;
17
17
}
18
18
try {
19
- const body = Uint8Array . from ( await buffer ( req ) ) as Buffer < ArrayBufferLike >
20
- event = stripe . webhooks . constructEvent ( body , sig ! , wh_sec )
21
- }
22
- catch ( err : any ) {
23
- console . log ( `❌ Error message: ${ err . message } ` ) ;
24
- return new Response ( `Webhook Error: ${ err . message } ` , { status : 400 } )
19
+ const body = Uint8Array . from ( await buffer ( req ) ) as Buffer < ArrayBufferLike > ;
20
+ event = stripe . webhooks . constructEvent ( body , sig ! , wh_sec ) ;
21
+ } catch ( err ) {
22
+ console . log ( `❌ Error message: ${ err . message } ` ) ;
23
+ return new Response ( `Webhook Error: ${ err . message } ` , { status : 400 } ) ;
25
24
}
26
25
console . log ( '✅ Success:' , event . id ) ;
27
26
@@ -38,25 +37,27 @@ export async function POST(req : NextRequest){
38
37
}
39
38
40
39
// Return a response to acknowledge receipt of the event
41
- return Response . json ( { received : true } , { status : 200 } ) ;
40
+ return Response . json ( { received : true } , { status : 200 } ) ;
42
41
}
43
42
44
- const buffer = async ( req : NextRequest ) => {
45
- const body = req . body ;
46
- if ( ! body ) {
47
- throw new Error ( 'Request body is null' ) ;
48
- }
49
- const reader = body . getReader ( ) ;
50
- const chunks = [ ] ;
51
- let done , value ;
52
- while ( { done, value } = await reader . read ( ) , ! done ) {
53
- chunks . push ( value ) ;
54
- }
55
- const buf = new Uint8Array ( chunks . reduce ( ( acc , chunk ) => acc + chunk . length , 0 ) ) ;
56
- let offset = 0 ;
57
- for ( const chunk of chunks ) {
58
- buf . set ( chunk , offset ) ;
59
- offset += chunk . length ;
60
- }
61
- return buf ;
43
+ const buffer = async ( req : NextRequest ) => {
44
+ const body = req . body ;
45
+ if ( ! body ) {
46
+ throw new Error ( 'Request body is null' ) ;
47
+ }
48
+ const reader = body . getReader ( ) ;
49
+ const chunks = [ ] ;
50
+ let done , value ;
51
+ while ( ( ( { done, value} = await reader . read ( ) ) , ! done ) ) {
52
+ chunks . push ( value ) ;
53
+ }
54
+ const buf = new Uint8Array (
55
+ chunks . reduce ( ( acc , chunk ) => acc + chunk . length , 0 )
56
+ ) ;
57
+ let offset = 0 ;
58
+ for ( const chunk of chunks ) {
59
+ buf . set ( chunk , offset ) ;
60
+ offset += chunk . length ;
61
+ }
62
+ return buf ;
62
63
} ;
0 commit comments