-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from iuioiua/webhooks
refactor: webhooks-based approach
- Loading branch information
Showing
18 changed files
with
285 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0 | ||
SUPABASE_URL=http://localhost:54321 | ||
SUPABSE_SERVICE_KEY=xxx | ||
|
||
STRIPE_SECRET_KEY=sk_test_xxx | ||
STRIPE_SECRET_KEY=sk_test_xxx | ||
STRIPE_WEBHOOK_SECRET=xxx |
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
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
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,81 @@ | ||
import type { Handlers } from "$fresh/server.ts"; | ||
import { stripe } from "@/utils/stripe.ts"; | ||
import { Stripe } from "stripe"; | ||
import { supabaseAdminClient } from "@/utils/supabase.ts"; | ||
import type { SupabaseClient } from "@supabase/supabase-js"; | ||
import { Database } from "@/utils/supabase_types.ts"; | ||
|
||
const cryptoProvider = Stripe.createSubtleCryptoProvider(); | ||
|
||
interface SetCustomerSubscriptionConfig { | ||
customer: string; | ||
isSubscribed: boolean; | ||
} | ||
|
||
export async function setCustomerSubscription( | ||
supabaseClient: SupabaseClient<Database>, | ||
{ customer, isSubscribed }: SetCustomerSubscriptionConfig, | ||
) { | ||
await supabaseClient | ||
.from("customers") | ||
.update({ is_subscribed: isSubscribed }) | ||
.eq("stripe_customer_id", customer) | ||
.throwOnError(); | ||
} | ||
|
||
export const handler: Handlers = { | ||
/** | ||
* This handler handles Stripe webhooks for the following events: | ||
* 1. customer.subscription.created (when a user subscribes to the premium plan) | ||
* 2. customer.subscription.deleted (when a user cancels the premium plan) | ||
*/ | ||
async POST(request) { | ||
const body = await request.text(); | ||
const signature = request.headers.get("stripe-signature")!; | ||
const signingSecret = Deno.env.get("STRIPE_WEBHOOK_SECRET")!; | ||
|
||
let event!: Stripe.Event; | ||
try { | ||
event = await stripe.webhooks.constructEventAsync( | ||
body, | ||
signature, | ||
signingSecret, | ||
undefined, | ||
cryptoProvider, | ||
); | ||
} catch (error) { | ||
console.error(error.message); | ||
return new Response(error.message, { status: 400 }); | ||
} | ||
|
||
switch (event.type) { | ||
case "customer.subscription.created": { | ||
await setCustomerSubscription( | ||
supabaseAdminClient, | ||
{ | ||
// @ts-ignore: Property 'customer' actually does exist on type 'Object' | ||
customer: event.data.object.customer, | ||
isSubscribed: true, | ||
}, | ||
); | ||
return new Response(null, { status: 201 }); | ||
} | ||
case "customer.subscription.deleted": { | ||
await setCustomerSubscription( | ||
supabaseAdminClient, | ||
{ | ||
// @ts-ignore: Property 'customer' actually does exist on type 'Object' | ||
customer: event.data.object.customer, | ||
isSubscribed: false, | ||
}, | ||
); | ||
return new Response(null, { status: 202 }); | ||
} | ||
default: { | ||
const message = `Event type not supported: ${event.type}`; | ||
console.error(message); | ||
return new Response(message, { status: 400 }); | ||
} | ||
} | ||
}, | ||
}; |
Oops, something went wrong.