diff --git a/app/webhooks/stripe/route.ts b/app/webhooks/stripe/route.ts index c4ec6154..fd79aebf 100644 --- a/app/webhooks/stripe/route.ts +++ b/app/webhooks/stripe/route.ts @@ -1,4 +1,5 @@ import { stripe } from "@/services/external/stripe"; +import { deleteSubscription } from "@/services/external/stripe/actions/delete-subscription"; import { upsertSubscription } from "@/services/external/stripe/actions/upsert-subscription"; import type Stripe from "stripe"; import { handleSubscriptionCycleInvoice } from "./handle-subscription-cycle-invoice"; @@ -60,7 +61,6 @@ export async function POST(req: Request) { break; case "customer.subscription.updated": - case "customer.subscription.deleted": if ( event.data.object.customer == null || typeof event.data.object.customer !== "string" @@ -72,6 +72,18 @@ export async function POST(req: Request) { await upsertSubscription(event.data.object.id); break; + case "customer.subscription.deleted": + if ( + event.data.object.customer == null || + typeof event.data.object.customer !== "string" + ) { + throw new Error( + "The checkout session is missing a valid customer ID. Please check the session data.", + ); + } + await deleteSubscription(event.data.object.id); + break; + case "invoice.created": console.log(`🔔 Invoice created: ${event.data.object.id}`); if (event.data.object.billing_reason === "subscription_cycle") { diff --git a/services/external/stripe/actions/delete-subscription.ts b/services/external/stripe/actions/delete-subscription.ts new file mode 100644 index 00000000..0358e365 --- /dev/null +++ b/services/external/stripe/actions/delete-subscription.ts @@ -0,0 +1,10 @@ +import { stripe } from "../config"; + +export async function deleteSubscription(subscriptionId: string) { + const subscription = await stripe.subscriptions.retrieve(subscriptionId); + + console.log("deleteSubscription"); + console.log("subscription.id", subscription.id); + console.log("subscription.object", subscription.object); + console.log("subscription.status", subscription.status); +} diff --git a/services/external/stripe/actions/upsert-subscription.ts b/services/external/stripe/actions/upsert-subscription.ts index 33f23729..18822372 100644 --- a/services/external/stripe/actions/upsert-subscription.ts +++ b/services/external/stripe/actions/upsert-subscription.ts @@ -12,6 +12,12 @@ const timestampToDateTime = (timestamp: number) => new Date(timestamp * 1000); export const upsertSubscription = async (subscriptionId: string) => { const subscription = await stripe.subscriptions.retrieve(subscriptionId); + + console.log("upsertSubscription"); + console.log("subscription.id", subscription.id); + console.log("subscription.object", subscription.object); + console.log("subscription.status", subscription.status); + const existingSubscriptionRecord = await db .select() .from(subscriptions)