50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { NextResponse, type NextRequest } from 'next/server';
|
|
import { createSupabaseServerClient } from '@/lib/supabase/server';
|
|
|
|
export const runtime = 'nodejs';
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const supabase = createSupabaseServerClient();
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser();
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'unauthorized' }, { status: 401 });
|
|
}
|
|
|
|
let session: string | null = null;
|
|
try {
|
|
const body = (await req.json()) as { session?: string };
|
|
session = body.session ?? null;
|
|
} catch {
|
|
// ignore — session id is optional in stub
|
|
}
|
|
|
|
const stub = process.env.STRIPE_STUB_URL;
|
|
if (!stub) {
|
|
return NextResponse.json(
|
|
{ error: 'STRIPE_STUB_URL not configured' },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
|
|
const res = await fetch(`${stub}/v1/webhooks/test`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
session,
|
|
customer_email: user.email,
|
|
user_id: user.id,
|
|
}),
|
|
});
|
|
if (!res.ok) {
|
|
return NextResponse.json(
|
|
{ error: `stripe-stub returned ${res.status}` },
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
const body = await res.json().catch(() => ({}));
|
|
return NextResponse.json({ ok: true, stub: body });
|
|
}
|