Handling Stripe Webhooks
Handling signed Stripe Webhooks with Edge Functions. View on GitHub.
1// Follow this setup guide to integrate the Deno language server with your editor:2// https://deno.land/manual/getting_started/setup_your_environment3// This enables autocomplete, go to definition, etc.45import Stripe from 'npm:stripe@^22'6import { withSupabase } from 'npm:@supabase/server@^1'78const stripe = new Stripe(Deno.env.get('STRIPE_API_KEY') as string)9// This is needed in order to use the Web Crypto API in Deno.10const cryptoProvider = Stripe.createSubtleCryptoProvider()1112console.log('Hello from Stripe Webhook!')1314// Stripe verifies the request via its signature, so deploy with verify_jwt = false.15export default {16 fetch: withSupabase({ auth: 'none' }, async (req) => {17 const signature = req.headers.get('Stripe-Signature')1819 // First step is to verify the event. The .text() method must be used as the20 // verification relies on the raw request body rather than the parsed JSON.21 const body = await req.text()22 let receivedEvent23 try {24 receivedEvent = await stripe.webhooks.constructEventAsync(25 body,26 signature!,27 Deno.env.get('STRIPE_WEBHOOK_SIGNING_SECRET')!,28 undefined,29 cryptoProvider30 )31 } catch (err) {32 return new Response(err.message, { status: 400 })33 }34 console.log(`🔔 Event received: ${receivedEvent.id}`)35 return Response.json({ ok: true })36 }),37}