-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the start of a webhook handler (#210)
* Add the start of a webhook handler * fix handler * Apply suggestions from code review Co-authored-by: Jorge Aguirre Gonzalez <[email protected]> Signed-off-by: Chris Streeter <[email protected]> --------- Signed-off-by: Chris Streeter <[email protected]> Co-authored-by: Jorge Aguirre Gonzalez <[email protected]>
- Loading branch information
1 parent
0e96d6d
commit 8fa34a5
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {type NextRequest} from 'next/server'; | ||
import {NextResponse} from 'next/server'; | ||
import {stripe} from '@/lib/stripe'; | ||
|
||
export async function POST(req: NextRequest) { | ||
const body = await req.text(); | ||
|
||
const sig = req.headers.get('stripe-signature'); | ||
if (!sig) { | ||
return NextResponse.json( | ||
{error: 'Cannot find the webhook signature'}, | ||
{status: 400} | ||
); | ||
} | ||
|
||
const secret = process.env.STRIPE_WEBHOOK_SECRET; | ||
if (!secret) { | ||
return NextResponse.json( | ||
{error: 'Cannot find the webhook secret'}, | ||
{status: 400} | ||
); | ||
} | ||
|
||
let event; | ||
try { | ||
event = stripe.webhooks.constructEvent( | ||
body, | ||
sig, | ||
process.env.STRIPE_WEBHOOK_SECRET || '' | ||
); | ||
} catch (err: any) { | ||
return NextResponse.json( | ||
{error: `Webhook Error: ${err.message}`}, | ||
{status: 400} | ||
); | ||
} | ||
|
||
// Handle events - see full event list: https://docs.stripe.com/api/events/types | ||
switch (event.type) { | ||
case 'account.updated': | ||
break; | ||
default: | ||
console.log('Unhandled event type', event.type); | ||
break; | ||
} | ||
|
||
return NextResponse.json({}); | ||
} |