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 stripe from 'stripe';
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 { stripe as stripeClient } from '$lib/utils/stripe';
const toDateTime = (secs: number) => {
const t = new Date(+0); // Unix epoch start.
@@ -10,7 +11,6 @@ const toDateTime = (secs: number) => {
return t;
};
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
type Product = Tables<'products'>;
type Price = Tables<'prices'>;
+1 -4
View File
@@ -1,8 +1,5 @@
import { type RequestHandler, redirect } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
import { stripe as stripeClient } from '$lib/utils/stripe';
// Create a checkout session
export const POST: RequestHandler = async ({ request, cookies }) => {
+1 -3
View File
@@ -1,8 +1,6 @@
import { json } from '@sveltejs/kit';
import { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
import stripe from 'stripe';
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 }) => {
const sessionId = cookies.get('checkout_session_id');
+8 -12
View File
@@ -1,6 +1,5 @@
import type { RequestHandler } from '@sveltejs/kit';
import stripe from 'stripe';
import { STRIPE_SECRET_KEY } from '$env/static/private';
import { STRIPE_ENDPOINT_SECRET } from '$env/static/private';
import {
deletePriceRecord,
@@ -9,8 +8,7 @@ import {
upsertPriceRecord,
upsertProductRecord
} from '$lib/utils/supabase/admin';
const stripeClient = new stripe(STRIPE_SECRET_KEY);
import { stripe as stripeClient } from '$lib/utils/stripe';
export const POST: RequestHandler = async ({ request }) => {
const body = await request.text();
@@ -36,6 +34,7 @@ export const POST: RequestHandler = async ({ request }) => {
}
switch (event.type) {
// Product events
case 'product.created':
case 'product.updated':
await upsertProductRecord(event.data.object as stripe.Product);
@@ -45,8 +44,8 @@ export const POST: RequestHandler = async ({ request }) => {
break;
case 'price.created':
case 'price.updated':
await upsertPriceRecord(event.data.object as stripe.Price);
break;
await upsertPrice(event.data.object)
break
case 'price.deleted':
await deletePriceRecord(event.data.object as stripe.Price);
break;
@@ -76,15 +75,12 @@ export const POST: RequestHandler = async ({ request }) => {
break;
}
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) {
const message = err instanceof Error ? err.message : 'An unknown error has occurred';
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 { PUBLIC_STRIPE_SECRET_KEY } from '$env/static/public';
import stripe from 'stripe';
const stripeClient = new stripe(PUBLIC_STRIPE_SECRET_KEY);
import { stripe as stripeClient } from '$lib/utils/stripe';
export const GET = async () => {
const products = await stripeClient.products.list({
+1 -3
View File
@@ -1,7 +1,5 @@
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);
import { stripe as stripeClient } from '$lib/utils/stripe';
export const GET = async ({ params }) => {
const { productId } = params;
@@ -1,7 +1,5 @@
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);
import { stripe as stripeClient } from '$lib/utils/stripe';
export const GET = async ({ params }) => {
const { priceId } = params;