diff --git a/src/lib/types/Products/ProductInfo.ts b/src/lib/types/Products/ProductInfo.ts new file mode 100644 index 0000000..900eab4 --- /dev/null +++ b/src/lib/types/Products/ProductInfo.ts @@ -0,0 +1,7 @@ +import Stripe from 'stripe'; + +export type ProductInfo = { + product: Stripe.Product; + price: Stripe.Price; + isSubscription: boolean; +}; \ No newline at end of file diff --git a/src/routes/products/[productId]/+page.server.ts b/src/routes/products/[productId]/+page.server.ts index 6a3fba3..2a43b4e 100644 --- a/src/routes/products/[productId]/+page.server.ts +++ b/src/routes/products/[productId]/+page.server.ts @@ -1,5 +1,6 @@ import { redirect } from '@sveltejs/kit'; import { hasProductAccess } from '$lib/utils/supabase/admin'; +import type { ProductInfo } from '$lib/types/Products/ProductInfo'; export const load = async ({ params, locals: { safeGetSession }, fetch }) => { const { session } = await safeGetSession(); @@ -8,7 +9,7 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => { } const productResponse = await fetch(`/products/${params.productId}`); - const product = await productResponse.json(); + const { product, isSubscription, price }: ProductInfo = await productResponse.json(); // Create the item object const item = { @@ -17,17 +18,13 @@ export const load = async ({ params, locals: { safeGetSession }, fetch }) => { categoryDescription: product.description, imgAlt: product.description, imgSrc: product.images[0], - price: 0, priceId: product.default_price, quantity: 1, - isSubscription: product.isSubscription + isSubscription: isSubscription, + price: (price.unit_amount ? price.unit_amount / 100 : NaN), + interval: price.recurring ? price.recurring.interval : null, } - - // Fetch the price of the product - const priceResponse = await fetch(`/products/prices/${product.default_price}`); - const price = await priceResponse.json(); - // Set the price of the product - item.price = price.unit_amount / 100; + // Check if the user has access to the 'EXAMPLE PRODUCT' product const hasPurchasedProduct = await hasProductAccess(session.user.id, params.productId); diff --git a/src/routes/products/[productId]/+server.ts b/src/routes/products/[productId]/+server.ts index c1b83c8..528dcce 100644 --- a/src/routes/products/[productId]/+server.ts +++ b/src/routes/products/[productId]/+server.ts @@ -1,14 +1,21 @@ import { json } from '@sveltejs/kit'; import { stripe as stripeClient } from '$lib/utils/stripe'; import Stripe from 'stripe'; +import { type ProductInfo } from '$lib/types/Products/ProductInfo'; export const GET = async ({ params }) => { const { productId } = params; const product = await stripeClient.products.retrieve(productId); - const price = await stripeClient.prices.retrieve(product.default_price); + const price = await stripeClient.prices.retrieve(product.default_price as string); - const isSubscription = price.type === ('recurring' as Stripe.Price.type); - product.isSubscription = isSubscription; - return json(product); + const isSubscription = price.type === ('recurring' as Stripe.Price.Type); + + const productInfo: ProductInfo = { + product, + price, + isSubscription + } + + return json(productInfo); };