Skip to content

Commit a88992f

Browse files
committed
format
1 parent 75fe7e0 commit a88992f

File tree

1 file changed

+35
-34
lines changed
  • examples/webhook-signing/nextjs/app/api/webhooks

1 file changed

+35
-34
lines changed
Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import Stripe from 'stripe';
2-
import { NextRequest } from "next/server";
2+
import {NextRequest} from 'next/server';
33

44
export const config = {
5-
api: {
6-
bodyParser: false,
7-
},
5+
api: {
6+
bodyParser: false,
7+
},
88
};
99

10-
export async function POST(req : NextRequest){
10+
export async function POST(req: NextRequest) {
1111
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;
1414
let event: Stripe.Event;
15-
if (!sig){
16-
throw new Error("Invalid Signature")
15+
if (!sig) {
16+
throw new Error('Invalid Signature');
1717
}
1818
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});
2524
}
2625
console.log('✅ Success:', event.id);
2726

@@ -38,25 +37,27 @@ export async function POST(req : NextRequest){
3837
}
3938

4039
// 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});
4241
}
4342

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;
6263
};

0 commit comments

Comments
 (0)