Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: add stripe webhook edge function example

thorwebdev905cd470c44c
diff --git a/examples/edge-functions/supabase/functions/stripe-webhooks/README.md b/examples/edge-functions/supabase/functions/stripe-webhooks/README.md
index e4ddc8d63c0..2d5a7a24d5d 100644
--- a/examples/edge-functions/supabase/functions/stripe-webhooks/README.md
+++ b/examples/edge-functions/supabase/functions/stripe-webhooks/README.md
@@ -2,15 +2,20 @@
Also check out our full Stripe Payments examples for [React Native (Expo)](https://github.com/supabase-community/expo-stripe-payments-with-supabase-functions) and [Flutter](https://github.com/supabase-community/flutter-stripe-payments-with-supabase-functions).
+## Setup env vars
+
+- `cp supabase/.env.local.example supabase/.env.local`
+
## Test locally
- Terminal 1:
- `supabase functions serve --no-verify-jwt stripe-webhooks --env-file ./supabase/.env.local`
- Terminal 2:
- - TODO
- - `sripe listen`
+ - `stripe listen --forward-to localhost:54321/functions/v1/`
+- Terminal 3 (optional):
+ - `stripe trigger payment_intent.succeeded`
## Deploy
+- `supabase functions deploy --no-verify-jwt stripe-webhooks`
- `supabase secrets set --env-file ./supabase/.env.local`
- TODO
diff --git a/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts b/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts
index 03cf4ecaa56..b59755ba424 100644
--- a/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts
+++ b/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts
@@ -1,5 +1,3 @@
-// @ts-nocheck
-
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.
@@ -7,7 +5,7 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
// esm.sh is used to compile stripe-node to be compatible with ES modules.
-import Stripe from 'https://esm.sh/stripe?target=deno&no-check'
+import Stripe from 'https://esm.sh/[email protected]?target=deno&no-check'
const stripe = Stripe(Deno.env.get('STRIPE_API_KEY'), {
// This is needed to use the Fetch API rather than relying on the Node http
@@ -37,6 +35,7 @@ serve(async (request) => {
} catch (err) {
return new Response(err.message, { status: 400 })
}
+ console.log(`🔔 Event received: ${receivedEvent.id}`)
// Secondly, we use this event to query the Stripe API in order to avoid
// handling any forged event. If available, we use the idempotency key.
@@ -54,14 +53,8 @@ serve(async (request) => {
return new Response(err.message, { status: 400 })
}
- return new Response(JSON.stringify(retrievedEvent), {
+ return new Response(JSON.stringify({ id: retrievedEvent.id, status: 'ok' }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
})
-
-// To invoke:
-// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \
-// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \
-// --header 'Content-Type: application/json' \
-// --data '{"name":"Functions"}'