update pay function

This commit is contained in:
2025-11-22 11:41:56 +08:00
parent a4b634abff
commit d8e4c737c5
397 changed files with 19572 additions and 9326 deletions

View File

@@ -0,0 +1,43 @@
import { ChatBody } from '@/types/types';
import { OpenAIStream } from '@/utils/streams/chatStream';
export const runtime = 'edge';
export async function GET(req: Request): Promise<Response> {
try {
const { inputMessage, model, apiKey } = (await req.json()) as ChatBody;
let apiKeyFinal;
if (apiKey) {
apiKeyFinal = apiKey;
} else {
apiKeyFinal = process.env.NEXT_PUBLIC_OPENAI_API_KEY;
}
const stream = await OpenAIStream(inputMessage, model, apiKeyFinal);
return new Response(stream);
} catch (error) {
console.error(error);
return new Response('Error', { status: 500 });
}
}
export async function POST(req: Request): Promise<Response> {
try {
const { inputMessage, model, apiKey } = (await req.json()) as ChatBody;
let apiKeyFinal;
if (apiKey) {
apiKeyFinal = apiKey;
} else {
apiKeyFinal = process.env.NEXT_PUBLIC_OPENAI_API_KEY;
}
const stream = await OpenAIStream(inputMessage, model, apiKeyFinal);
return new Response(stream);
} catch (error) {
console.error(error);
return new Response('Error', { status: 500 });
}
}

View File

@@ -0,0 +1,29 @@
import { EssayBody } from '@/types/types';
import { OpenAIStream } from '@/utils/streams/essayStream';
export const runtime = 'edge';
const handler = async (req: Request): Promise<Response> => {
try {
const {
topic,
words,
essayType,
model,
apiKey
} = (await req.json()) as EssayBody;
if (!apiKey) {
return new Response('API key not found', { status: 500 });
}
const stream = await OpenAIStream(topic, essayType, words, model, apiKey);
return new Response(stream);
} catch (error) {
console.error(error);
return new Response('Error', { status: 500 });
}
};
export default handler;

View File

@@ -0,0 +1,43 @@
import { PremiumEssayBody } from '@/types/types';
import { OpenAIStream } from '@/utils/streams/premiumEssayStream';
export const runtime = 'edge';
const handler = async (req: Request): Promise<Response> => {
try {
const {
words,
topic,
essayType,
tone,
citation,
level,
citations,
model,
apiKey
} = (await req.json()) as PremiumEssayBody;
if (!apiKey) {
return new Response('API key not found', { status: 500 });
}
const stream = await OpenAIStream(
words,
topic,
essayType,
tone,
citation,
level,
citations,
model,
apiKey
);
return new Response(stream);
} catch (error) {
console.error(error);
return new Response('Error', { status: 500 });
}
};
export default handler;

View File

@@ -0,0 +1,81 @@
import { stripe } from '@/utils/stripe/config';
import {
manageSubscriptionStatusChange,
upsertPriceRecord,
upsertProductRecord
} from '@/utils/supabase/admin';
import { headers } from 'next/headers';
import Stripe from 'stripe';
const relevantEvents = new Set([
'product.created',
'product.updated',
'price.created',
'price.updated',
'checkout.session.completed',
'customer.subscription.created',
'customer.subscription.updated',
'customer.subscription.deleted'
]);
export async function POST(req: Request) {
const body = await req.text();
const sig = headers().get('Stripe-Signature') as string;
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
let event: Stripe.Event;
try {
if (!sig || !webhookSecret) return;
event = stripe.webhooks.constructEvent(body, sig, webhookSecret);
} catch (err) {
console.log(`❌ Error message: ${err.message}`);
return new Response(`Webhook Error: ${err.message}`, { status: 400 });
}
if (relevantEvents.has(event.type)) {
try {
switch (event.type) {
case 'product.created':
case 'product.updated':
await upsertProductRecord(event.data.object as Stripe.Product);
break;
case 'price.created':
case 'price.updated':
await upsertPriceRecord(event.data.object as Stripe.Price);
break;
case 'customer.subscription.created':
case 'customer.subscription.updated':
case 'customer.subscription.deleted':
const subscription = event.data.object as Stripe.Subscription;
await manageSubscriptionStatusChange(
subscription.id,
subscription.customer as string,
event.type === 'customer.subscription.created'
);
break;
case 'checkout.session.completed':
const checkoutSession = event.data.object as Stripe.Checkout.Session;
if (checkoutSession.mode === 'subscription') {
const subscriptionId = checkoutSession.subscription;
await manageSubscriptionStatusChange(
subscriptionId as string,
checkoutSession.customer as string,
true
);
}
break;
default:
throw new Error('Unhandled relevant event!');
}
} catch (error) {
console.log(error);
return new Response(
'Webhook handler failed. View your nextjs function logs.',
{
status: 400
}
);
}
}
return new Response(JSON.stringify({ received: true }));
}