From 98ee3a9c3712358bb6aeaa287ec9ab1cd7f957a6 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 16 Apr 2025 23:04:32 +0200 Subject: [PATCH] clean code --- src/routes/+page.svelte | 2 +- src/routes/discord-bot/+page.server.ts | 25 ------ src/routes/discord-bot/+page.svelte | 35 ++++---- src/routes/payment/discord/+server.ts | 117 +++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 46 deletions(-) create mode 100644 src/routes/payment/discord/+server.ts diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index b2a6ee8f..0fb20b6f 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -116,7 +116,7 @@ class="text-center mb-10 relative w-fit flex justify-center m-auto text-white" >
-
diff --git a/src/routes/discord-bot/+page.server.ts b/src/routes/discord-bot/+page.server.ts index 0967b570..51d7d146 100644 --- a/src/routes/discord-bot/+page.server.ts +++ b/src/routes/discord-bot/+page.server.ts @@ -2,32 +2,7 @@ import { error, fail, redirect } from "@sveltejs/kit"; import { validateData } from "$lib/utils"; import { loginUserSchema, registerUserSchema } from "$lib/schemas"; -/* -export const load = async () => { - const apiKey = import.meta.env.VITE_LEMON_SQUEEZY_API_KEY; - - const getLTDCount = async () => { - // make the POST request to the endpoint - const response = await fetch('https://api.lemonsqueezy.com/v1/order-items?page[size]=100', { - headers: { - 'Accept': 'application/vnd.api+json', - 'Content-Type': 'application/vnd.api+json', - 'Authorization': `Bearer ${apiKey}` - } - }); - const output = await response.json(); - const filteredData = output?.data?.filter(item => item?.attributes?.product_name === 'Pro Subscription (Life Time Access)'); - const count = filteredData?.length || 0; - - return count; - }; - - return { - getLTDCount: await getLTDCount(), - }; - }; -*/ export const actions = { login: async ({ request, locals }) => { diff --git a/src/routes/discord-bot/+page.svelte b/src/routes/discord-bot/+page.svelte index ce7da768..dedc3b45 100644 --- a/src/routes/discord-bot/+page.svelte +++ b/src/routes/discord-bot/+page.svelte @@ -24,22 +24,14 @@ } }); - async function purchasePlan(subscriptionType: string = "") { + async function purchasePlan() { if (data?.user) { let subId = ""; - if (subscriptionType === "lifeTime") { - subId = import.meta.env.VITE_LEMON_SQUEEZY_LIFE_TIME_ACCESS_ID; - } else if (mode && subscriptionType === "plus") { - subId = import.meta.env.VITE_LEMON_SQUEEZY_ANNUAL_ID_PLUS; - } else if (!mode && subscriptionType === "plus") { - subId = import.meta.env.VITE_LEMON_SQUEEZY_MONTHLY_ID_PLUS; - } else if (mode && subscriptionType === "pro") { - subId = import.meta.env.VITE_LEMON_SQUEEZY_ANNUAL_ID_PRO; - } else if (!mode && subscriptionType === "pro") { - subId = import.meta.env.VITE_LEMON_SQUEEZY_MONTHLY_ID_PRO; + if (mode) { + subId = import.meta.env.VITE_LEMON_SQUEEZY_DISCORD_ANNUAL_ID; } else { - subId = import.meta.env.VITE_LEMON_SQUEEZY_ANNUAL_ID_PRO; + subId = import.meta.env.VITE_LEMON_SQUEEZY_DISCORD_MONTHLY_ID; } try { } catch (e) { @@ -66,8 +58,8 @@ @@ -259,12 +251,13 @@
- + -
+
{ + try { + const bodyText = await request.text(); + + // Retrieve the signature header; return early if missing. + const signatureHeader = request.headers.get("x-Signature"); + if (!signatureHeader) { + console.error("Missing x-Signature header."); + return new Response( + JSON.stringify({ error: "Missing signature header" }), + { + status: 403, + headers: { "Content-Type": "application/json" }, + } + ); + } + + if (!isValidSignature(bodyText, signatureHeader)) { + console.error("Signature verification failed."); + return new Response( + JSON.stringify({ error: "Invalid signature" }), + { + status: 403, + headers: { "Content-Type": "application/json" }, + } + ); + } + + // Parse the JSON payload + const payload = JSON.parse(bodyText); + const userId = payload?.meta?.custom_data?.userId; + const { status, refunded } = payload?.data?.attributes || {}; + + + if (!userId || status === undefined) { + console.error("Missing userId or status in payload:", payload); + return new Response( + JSON.stringify({ error: "Invalid payload structure" }), + { + status: 400, + headers: { "Content-Type": "application/json" }, + } + ); + } + + + try { + + + const paymentData = { user: userId, data: payload }; + await locals.pb.collection("discordPayments").create(paymentData); + } catch (dbError) { + console.error("Database error:", dbError); + + return new Response( + JSON.stringify({ error: "Pocketbase error" }), + { + status: 500, + headers: { "Content-Type": "application/json" }, + } + + ); + + + } + + return new Response( + JSON.stringify({ message: "Payment data received successfully" }), + { + status: 200, + headers: { "Content-Type": "application/json" }, + } + ); + } catch (error) { + console.error("Error processing request:", error); + return new Response( + JSON.stringify({ error: "Internal server error" }), + { + status: 500, + headers: { "Content-Type": "application/json" }, + } + ); + } +};