The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add stripe webhook edge function example
thorwebdev7371c5bf818d
diff --git a/examples/edge-functions/supabase/.env.local.example b/examples/edge-functions/supabase/.env.local.exampleindex b69d3f546a5..2f2c44b56a5 100644--- a/examples/edge-functions/supabase/.env.local.example+++ b/examples/edge-functions/supabase/.env.local.example@@ -7,4 +7,8 @@ SMTP_FROM="[email protected]"# telegram-botTELEGRAM_BOT_TOKEN="get it from https://t.me/BotFather"-FUNCTION_SECRET="random secret"\ No newline at end of file+FUNCTION_SECRET="random secret"++# stripe-webhooks+STRIPE_API_KEY="<YOUR API KEY HERE>"+STRIPE_WEBHOOK_SIGNING_SECRET="<YOUR WEBHOOK SIGNING HERE>"\ No newline at end of filediff --git a/examples/edge-functions/supabase/functions/stripe-webhooks/README.md b/examples/edge-functions/supabase/functions/stripe-webhooks/README.mdnew file mode 100644index 00000000000..e4ddc8d63c0--- /dev/null+++ b/examples/edge-functions/supabase/functions/stripe-webhooks/README.md@@ -0,0 +1,16 @@+# stripe-webhooks++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).++## Test locally++- Terminal 1:+ - `supabase functions serve --no-verify-jwt stripe-webhooks --env-file ./supabase/.env.local`+- Terminal 2:+ - TODO+ - `sripe listen`++## Deploy++- `supabase secrets set --env-file ./supabase/.env.local`+ TODOdiff --git a/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts b/examples/edge-functions/supabase/functions/stripe-webhooks/index.tsnew file mode 100644index 00000000000..03cf4ecaa56--- /dev/null+++ b/examples/edge-functions/supabase/functions/stripe-webhooks/index.ts@@ -0,0 +1,67 @@+// @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.++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'++const stripe = Stripe(Deno.env.get('STRIPE_API_KEY'), {+ // This is needed to use the Fetch API rather than relying on the Node http+ // package.+ httpClient: Stripe.createFetchHttpClient(),+})+// This is needed in order to use the Web Crypto API in Deno.+const cryptoProvider = Stripe.createSubtleCryptoProvider()++console.log(`Function "stripe-webhooks" up and running!`)++serve(async (request) => {+ const signature = request.headers.get('Stripe-Signature')++ // First step is to verify the event. The .text() method must be used as the+ // verification relies on the raw request body rather than the parsed JSON.+ const body = await request.text()+ let receivedEvent+ try {+ receivedEvent = await stripe.webhooks.constructEventAsync(+ body,+ signature,+ Deno.env.get('STRIPE_WEBHOOK_SIGNING_SECRET'),+ undefined,+ cryptoProvider+ )+ } catch (err) {+ return new Response(err.message, { status: 400 })+ }++ // 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.+ const requestOptions =+ receivedEvent.request && receivedEvent.request.idempotency_key+ ? {+ idempotencyKey: receivedEvent.request.idempotency_key,+ }+ : {}++ let retrievedEvent+ try {+ retrievedEvent = await stripe.events.retrieve(receivedEvent.id, requestOptions)+ } catch (err) {+ return new Response(err.message, { status: 400 })+ }++ return new Response(JSON.stringify(retrievedEvent), {+ 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"}'