refactor(#29): centralise stripe client

This commit is contained in:
OllyNicholass
2024-09-17 19:29:43 +01:00
parent 49a9b079ea
commit 7e05b3f361
8 changed files with 19 additions and 30 deletions
+4
View File
@@ -0,0 +1,4 @@
import Stripe from 'stripe'
import { STRIPE_SECRET_KEY } from '$env/static/private'
export const stripe = new Stripe(STRIPE_SECRET_KEY)
+2 -2
View File
@@ -1,8 +1,9 @@
import { createClient } from '@supabase/supabase-js'; import { createClient } from '@supabase/supabase-js';
import stripe from 'stripe'; import stripe from 'stripe';
import type { Database, Tables, TablesInsert } from '$lib/types/supabase'; import type { Database, Tables, TablesInsert } from '$lib/types/supabase';
import { PUBLIC_SUPABASE_URL, PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; import { PUBLIC_SUPABASE_URL } from '$env/static/public';
import { SUPABASE_SERVICE_ROLE_KEY } from '$env/static/private'; import { SUPABASE_SERVICE_ROLE_KEY } from '$env/static/private';
import { stripe as stripeClient } from '$lib/utils/stripe';
const toDateTime = (secs: number) => { const toDateTime = (secs: number) => {
const t = new Date(+0); // Unix epoch start. const t = new Date(+0); // Unix epoch start.
@@ -10,7 +11,6 @@ const toDateTime = (secs: number) => {
return t; return t;
}; };
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
type Product = Tables<'products'>; type Product = Tables<'products'>;
type Price = Tables<'prices'>; type Price = Tables<'prices'>;
+1 -4
View File
@@ -1,8 +1,5 @@
import { type RequestHandler, redirect } from '@sveltejs/kit'; import { type RequestHandler, redirect } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; import { stripe as stripeClient } from '$lib/utils/stripe';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
// Create a checkout session // Create a checkout session
export const POST: RequestHandler = async ({ request, cookies }) => { export const POST: RequestHandler = async ({ request, cookies }) => {
+1 -3
View File
@@ -1,8 +1,6 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
import stripe from 'stripe';
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY); import { stripe as stripeClient } from '$lib/utils/stripe';
export const GET: RequestHandler = async ({ cookies }) => { export const GET: RequestHandler = async ({ cookies }) => {
const sessionId = cookies.get('checkout_session_id'); const sessionId = cookies.get('checkout_session_id');
+8 -12
View File
@@ -1,6 +1,5 @@
import type { RequestHandler } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit';
import stripe from 'stripe'; import stripe from 'stripe';
import { STRIPE_SECRET_KEY } from '$env/static/private';
import { STRIPE_ENDPOINT_SECRET } from '$env/static/private'; import { STRIPE_ENDPOINT_SECRET } from '$env/static/private';
import { import {
deletePriceRecord, deletePriceRecord,
@@ -9,8 +8,7 @@ import {
upsertPriceRecord, upsertPriceRecord,
upsertProductRecord upsertProductRecord
} from '$lib/utils/supabase/admin'; } from '$lib/utils/supabase/admin';
import { stripe as stripeClient } from '$lib/utils/stripe';
const stripeClient = new stripe(STRIPE_SECRET_KEY);
export const POST: RequestHandler = async ({ request }) => { export const POST: RequestHandler = async ({ request }) => {
const body = await request.text(); const body = await request.text();
@@ -36,6 +34,7 @@ export const POST: RequestHandler = async ({ request }) => {
} }
switch (event.type) { switch (event.type) {
// Product events
case 'product.created': case 'product.created':
case 'product.updated': case 'product.updated':
await upsertProductRecord(event.data.object as stripe.Product); await upsertProductRecord(event.data.object as stripe.Product);
@@ -45,8 +44,8 @@ export const POST: RequestHandler = async ({ request }) => {
break; break;
case 'price.created': case 'price.created':
case 'price.updated': case 'price.updated':
await upsertPriceRecord(event.data.object as stripe.Price); await upsertPrice(event.data.object)
break; break
case 'price.deleted': case 'price.deleted':
await deletePriceRecord(event.data.object as stripe.Price); await deletePriceRecord(event.data.object as stripe.Price);
break; break;
@@ -76,15 +75,12 @@ export const POST: RequestHandler = async ({ request }) => {
break; break;
} }
default: default:
console.error(`Unhandled event type ${event.type}`); console.log(`Unhandled event type ${event.type}`)
} }
// Return a response to acknowledge receipt of the event
return new Response(JSON.stringify({ received: true }), {
status: 200
});
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : 'An unknown error has occurred'; const message = err instanceof Error ? err.message : 'An unknown error has occurred';
return new Response(`Webhook Error: ${message}`, { status: 500 }); return new Response(`Webhook Error: ${message}`, { status: 500 });
} }
};
return json({received: true})
}
+1 -3
View File
@@ -1,7 +1,5 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; import { stripe as stripeClient } from '$lib/utils/stripe';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
export const GET = async () => { export const GET = async () => {
const products = await stripeClient.products.list({ const products = await stripeClient.products.list({
+1 -3
View File
@@ -1,7 +1,5 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; import { stripe as stripeClient } from '$lib/utils/stripe';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
export const GET = async ({ params }) => { export const GET = async ({ params }) => {
const { productId } = params; const { productId } = params;
@@ -1,7 +1,5 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public'; import { stripe as stripeClient } from '$lib/utils/stripe';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
export const GET = async ({ params }) => { export const GET = async ({ params }) => {
const { priceId } = params; const { priceId } = params;