Files
2026-06-16 03:49:49 +08:00

72 lines
2.2 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import {
applyAuthResponseCookies,
buildBackendRequestHeaders,
requireBackendPaymentAuth,
} from "@/lib/backend-auth";
import {
buildProxyExceptionResponse,
buildUpstreamErrorResponse,
} from "@/lib/api-proxy";
import { isPaymentHostAllowed } from "@/lib/payment-host";
import {
requireTurnstileForRequest,
stripTurnstileToken,
} from "@/lib/turnstile";
const API_BASE = process.env.POLYWEATHER_API_BASE_URL;
export async function POST(req: NextRequest) {
if (!API_BASE) {
return NextResponse.json(
{ error: "POLYWEATHER_API_BASE_URL is not configured" },
{ status: 500 },
);
}
const requestHost =
req.headers.get("x-forwarded-host") ||
req.headers.get("host") ||
req.nextUrl.hostname;
if (!isPaymentHostAllowed(requestHost)) {
return NextResponse.json(
{
error:
"Payments are disabled on this host. Please return to the main production site and retry.",
host: requestHost,
},
{ status: 409 },
);
}
try {
const body = await req.json();
const turnstileError = await requireTurnstileForRequest(req, "payment_intent_create", body);
if (turnstileError) return turnstileError;
const auth = await buildBackendRequestHeaders(req);
const authError = requireBackendPaymentAuth(auth);
if (authError) return authError;
const proxiedHeaders = new Headers(auth.headers);
proxiedHeaders.set("Content-Type", "application/json");
const res = await fetch(`${API_BASE}/api/payments/intents`, {
method: "POST",
headers: proxiedHeaders,
body: JSON.stringify(stripTurnstileToken(body ?? {})),
cache: "no-store",
});
if (!res.ok) {
const raw = await res.text();
const response = buildUpstreamErrorResponse(res.status, raw, {
detailLimit: 350,
});
return applyAuthResponseCookies(response, auth.response);
}
const data = await res.json();
const response = NextResponse.json(data);
return applyAuthResponseCookies(response, auth.response);
} catch (error) {
return buildProxyExceptionResponse(error, {
publicMessage: "Failed to create payment intent",
});
}
}