From af28f75e6e987346fdad775b3b0ebec54cdb0c72 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Sun, 5 May 2024 12:33:54 +0100 Subject: [PATCH] feat(*): Add products page --- .../products/ProductsListItem.svelte | 38 +++++++++++++++++ src/routes/products/+page.svelte | 41 +++++++++++++++++++ src/routes/products/+server.ts | 23 +++++++++++ .../products/prices/[priceId]/+server.ts | 10 +++++ 4 files changed, 112 insertions(+) create mode 100644 src/lib/components/products/ProductsListItem.svelte create mode 100644 src/routes/products/+page.svelte create mode 100644 src/routes/products/+server.ts create mode 100644 src/routes/products/prices/[priceId]/+server.ts diff --git a/src/lib/components/products/ProductsListItem.svelte b/src/lib/components/products/ProductsListItem.svelte new file mode 100644 index 0000000..40574a5 --- /dev/null +++ b/src/lib/components/products/ProductsListItem.svelte @@ -0,0 +1,38 @@ + + + +
+ {product.description} + +
+
+
+
{product.name}
+
£{product.actualPrice}
+
+ +
+
diff --git a/src/routes/products/+page.svelte b/src/routes/products/+page.svelte new file mode 100644 index 0000000..ad1edd7 --- /dev/null +++ b/src/routes/products/+page.svelte @@ -0,0 +1,41 @@ + + +
+
+

+ Available Products +

+
+ {#each products as product (product.id)} + + {/each} +
+ {#if enablePagination} +
+
+ + + + +
+
+ {/if} +
+
diff --git a/src/routes/products/+server.ts b/src/routes/products/+server.ts new file mode 100644 index 0000000..70e129d --- /dev/null +++ b/src/routes/products/+server.ts @@ -0,0 +1,23 @@ +import { json } from '@sveltejs/kit'; +import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; +import stripe from 'stripe'; +const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY); + +export const GET = async () => { + const products = await stripeClient.products.list({ + limit: 100 + }); + + try { + // Get the price for each product + for (const product of products.data) { + const price = await stripeClient.prices.retrieve(product.default_price as string); + if (price) { + product.actualPrice = price.unit_amount / 100; + } + } + } catch (error) { + console.error(error); + } + return json(products); +}; diff --git a/src/routes/products/prices/[priceId]/+server.ts b/src/routes/products/prices/[priceId]/+server.ts new file mode 100644 index 0000000..b1fca62 --- /dev/null +++ b/src/routes/products/prices/[priceId]/+server.ts @@ -0,0 +1,10 @@ +import { json } from '@sveltejs/kit'; +import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; +import stripe from 'stripe'; +const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY); + +export const GET = async ({ params }) => { + const { priceId } = params; + const price = await stripeClient.prices.retrieve(priceId); + return json(price); +};