mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-28 17:56:37 +02:00
✨ Keep stripe customer reference in sync using webhook (#1580)
This commit is contained in:
parent
a1f50dc792
commit
34f5555791
4 changed files with 53 additions and 0 deletions
|
@ -0,0 +1,30 @@
|
|||
import type { Stripe } from "@rallly/billing";
|
||||
import { prisma } from "@rallly/database";
|
||||
import { z } from "zod";
|
||||
|
||||
const customerMetadataSchema = z.object({
|
||||
userId: z.string(),
|
||||
});
|
||||
|
||||
export async function onCustomerCreated(event: Stripe.Event) {
|
||||
const customer = event.data.object as Stripe.Customer;
|
||||
|
||||
const res = customerMetadataSchema.safeParse(customer.metadata);
|
||||
|
||||
if (!res.success) {
|
||||
// If there's no userId in metadata, ignore the event
|
||||
return;
|
||||
}
|
||||
|
||||
const { userId } = res.data;
|
||||
|
||||
// Update the user with the customer id
|
||||
await prisma.user.update({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
data: {
|
||||
customerId: customer.id,
|
||||
},
|
||||
});
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
import type { Stripe } from "@rallly/billing";
|
||||
import { prisma } from "@rallly/database";
|
||||
|
||||
export async function onCustomerDeleted(event: Stripe.Event) {
|
||||
const customer = event.data.object as Stripe.Customer;
|
||||
|
||||
// Find and update the user with this customerId
|
||||
await prisma.user.updateMany({
|
||||
where: {
|
||||
customerId: customer.id,
|
||||
},
|
||||
data: {
|
||||
customerId: null,
|
||||
},
|
||||
});
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
export * from "./created";
|
||||
export * from "./deleted";
|
|
@ -2,6 +2,7 @@ import type { Stripe } from "@rallly/billing";
|
|||
|
||||
import { onCheckoutSessionCompleted } from "./checkout/completed";
|
||||
import { onCheckoutSessionExpired } from "./checkout/expired";
|
||||
import { onCustomerCreated, onCustomerDeleted } from "./customer";
|
||||
import { onCustomerSubscriptionCreated } from "./customer-subscription/created";
|
||||
import { onCustomerSubscriptionDeleted } from "./customer-subscription/deleted";
|
||||
import { onCustomerSubscriptionUpdated } from "./customer-subscription/updated";
|
||||
|
@ -17,6 +18,10 @@ export function getEventHandler(eventType: Stripe.Event["type"]) {
|
|||
return onCheckoutSessionCompleted;
|
||||
case "checkout.session.expired":
|
||||
return onCheckoutSessionExpired;
|
||||
case "customer.created":
|
||||
return onCustomerCreated;
|
||||
case "customer.deleted":
|
||||
return onCustomerDeleted;
|
||||
case "customer.subscription.created":
|
||||
return onCustomerSubscriptionCreated;
|
||||
case "customer.subscription.deleted":
|
||||
|
|
Loading…
Add table
Reference in a new issue