Create webhook to update team with Stripe ids

This commit is contained in:
Joe Au-Yeung 2022-10-21 16:04:13 -04:00
parent 91d3d6b84f
commit 4feb65d130
2 changed files with 49 additions and 19 deletions

View File

@ -158,10 +158,45 @@ async function handlePaymentSuccess(event: Stripe.Event) {
});
}
async function handleTeamSubscriptionSuccess(event: Stripe.Event) {
const checkoutSession = event.data.object as Stripe.Checkout.Session;
if (!checkoutSession?.metadata?.teamId)
throw new HttpCode({
statusCode: 200,
message: `No teamId passed`,
});
// Update team record with Stripe ids
if (checkoutSession.payment_status === "paid") {
await prisma.team.update({
where: {
id: parseInt(checkoutSession.metadata.teamId),
},
data: {
stripeCustomerId: checkoutSession.customer as string,
stripeSubscriptionId: checkoutSession.subscription as string,
subscriptionStatus: "active",
},
});
} else {
throw new HttpCode({
statusCode: 200,
message: `Checkout was not paid for`,
});
}
throw new HttpCode({
statusCode: 200,
message: `Team record updated with Stripe ids`,
});
}
type WebhookHandler = (event: Stripe.Event) => Promise<void>;
const webhookHandlers: Record<string, WebhookHandler | undefined> = {
"payment_intent.succeeded": handlePaymentSuccess,
"checkout.session.completed": handleTeamSubscriptionSuccess,
};
/**
@ -187,9 +222,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const event = stripe.webhooks.constructEvent(payload, sig, process.env.STRIPE_WEBHOOK_SECRET);
if (!event.account) {
throw new HttpCode({ statusCode: 202, message: "Incoming connected account" });
}
// if (!event.account) {
// throw new HttpCode({ statusCode: 202, message: "Incoming connected account" });
// }
const handler = webhookHandlers[event.type];
if (handler) {

View File

@ -170,9 +170,17 @@ export const viewerTeamsRouter = createProtectedRouter()
async resolve({ ctx, input }) {
if (!(await isTeamOwner(ctx.user?.id, input.teamId))) throw new TRPCError({ code: "UNAUTHORIZED" });
// if (process.env.STRIPE_PRIVATE_KEY) {
// await downgradeTeamMembers(input.teamId);
// }
// Delete customer from Stripe
const stripeCustomerId = await ctx.prisma.team.findFirst({
where: {
id: input.teamId,
},
select: { stripeCustomerId: true },
});
if (!stripeCustomerId?.stripeCustomerId)
throw new TRPCError({ code: "UNAUTHORIZED", message: "Could not delete customer from Stripe" });
await stripe.customers.del(stripeCustomerId.stripeCustomerId);
// delete all memberships
await ctx.prisma.membership.deleteMany({
@ -181,19 +189,6 @@ export const viewerTeamsRouter = createProtectedRouter()
},
});
// Delete customer from Stripe
try {
const stripeCustomerId = await ctx.prisma.team.findFirst({
where: {
id: input.teamId,
},
select: { stripeCustomerId: true },
});
await stripe.customers.del(stripeCustomerId.stripeCustomerId);
} catch {
throw new TRPCError({ code: "UNAUTHORIZED", message: "Could not delete customer from Stripe" });
}
const deletedTeam = await ctx.prisma.team.delete({
where: {
id: input.teamId,