From 77ad7a4969b5a1292d4e0180e21970ce78099c23 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Thu, 20 Feb 2025 11:25:32 +0100 Subject: [PATCH] Drop redundant validation --- template/app/src/payment/stripe/webhook.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/template/app/src/payment/stripe/webhook.ts b/template/app/src/payment/stripe/webhook.ts index 12859c1..a0ed9fc 100644 --- a/template/app/src/payment/stripe/webhook.ts +++ b/template/app/src/payment/stripe/webhook.ts @@ -118,7 +118,7 @@ export async function handleCheckoutSessionCompleted( // This is called when a subscription is purchased or renewed and payment succeeds. // Invoices are not created for one-time payments, so we handle them in the payment_intent.succeeded webhook. export async function handleInvoicePaid(invoice: InvoicePaidData, prismaUserDelegate: PrismaClient['user']) { - const userStripeId = validateUserStripeIdOrThrow(invoice.customer); + const userStripeId = invoice.customer; const datePaid = new Date(invoice.period_start * 1000); return updateUserStripePaymentDetails({ userStripeId, datePaid }, prismaUserDelegate); } @@ -133,7 +133,7 @@ export async function handlePaymentIntentSucceeded( return; } - const userStripeId = validateUserStripeIdOrThrow(paymentIntent.customer); + const userStripeId = paymentIntent.customer; const datePaid = new Date(paymentIntent.created * 1000); // We capture the price id from the payment intent metadata @@ -162,7 +162,7 @@ export async function handleCustomerSubscriptionUpdated( subscription: SubscriptionUpdatedData, prismaUserDelegate: PrismaClient['user'] ) { - const userStripeId = validateUserStripeIdOrThrow(subscription.customer); + const userStripeId = subscription.customer; let subscriptionStatus: SubscriptionStatus | undefined; const priceId = extractPriceId(subscription.items); @@ -198,16 +198,10 @@ export async function handleCustomerSubscriptionDeleted( subscription: SubscriptionDeletedData, prismaUserDelegate: PrismaClient['user'] ) { - const userStripeId = validateUserStripeIdOrThrow(subscription.customer); + const userStripeId = subscription.customer; return updateUserStripePaymentDetails({ userStripeId, subscriptionStatus: 'deleted' }, prismaUserDelegate); } -function validateUserStripeIdOrThrow(userStripeId: Stripe.Checkout.Session['customer']): string { - if (!userStripeId) throw new HttpError(400, 'No customer id'); - if (typeof userStripeId !== 'string') throw new HttpError(400, 'Customer id is not a string'); - return userStripeId; -} - type SubscsriptionItems = z.infer; const subscriptionItemsSchema = z.object({