80 lines
4.2 KiB
TypeScript
80 lines
4.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getStripe } from "@/lib/stripe";
|
|
import { cfg } from "@/lib/cfg";
|
|
import { TIERS, tierForAmount } from "@/lib/donations";
|
|
import { minecraftApi } from "@/lib/minecraft-api";
|
|
import { minecraft } from "@/lib/servertap";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json();
|
|
const { platform, username, tier, customAmount, customRecurring, giftTo, upgrade } = body as {
|
|
platform: "java" | "bedrock"; username: string; tier: string; customAmount?: number; customRecurring?: boolean; giftTo?: string; upgrade?: boolean;
|
|
};
|
|
if (!username || !platform || !tier) return NextResponse.json({ error: "Missing fields" }, { status: 400 });
|
|
|
|
// resolve recipient first — validates the account exists and gives us
|
|
// uuid/xuid so the webhook can grant by UUID instead of name
|
|
const recipientName = (giftTo?.trim() || username).trim();
|
|
const recipientPlatform = platform;
|
|
const resolved = await minecraftApi.resolvePlayerIdentifier({ type: recipientPlatform, username: recipientName }).catch((e: any) => {
|
|
return { error: e?.message || "Could not resolve that player" } as const;
|
|
});
|
|
if ("error" in resolved!) return NextResponse.json({ error: resolved!.error }, { status: 400 });
|
|
const recipient = resolved!;
|
|
|
|
let price = 0; let tierId = tier; let isSub = false; let label = ""; let verifiedFrom: string | null = null;
|
|
if (tier === "custom") {
|
|
if (!customAmount || customAmount < 100) return NextResponse.json({ error: "Custom amount min £1" }, { status: 400 });
|
|
price = customAmount; isSub = !!customRecurring;
|
|
// >=£5/mo recurring custom grants Patron
|
|
const unlocked = isSub && price >= TIERS.patron.price ? "patron" : tierForAmount(price);
|
|
label = unlocked ? `Custom — unlocks ${unlocked}` : "Custom donation";
|
|
tierId = unlocked ?? "custom";
|
|
} else {
|
|
const t = (TIERS as any)[tier];
|
|
if (!t) return NextResponse.json({ error: "Invalid tier" }, { status: 400 });
|
|
if (upgrade) {
|
|
const current = await minecraft.getHighestTierFor({ platform: recipientPlatform, username: recipient.username, uuid: recipient.uuid, xuid: recipient.xuid }).catch(() => null);
|
|
if (!current) return NextResponse.json({ error: "No existing rank found for that username — can't upgrade" }, { status: 400 });
|
|
const from = (TIERS as any)[current];
|
|
const diff = t.price - from.price;
|
|
if (diff <= 0) return NextResponse.json({ error: `Already has ${from.label} or higher — can't upgrade to ${t.label}` }, { status: 400 });
|
|
verifiedFrom = current;
|
|
price = diff;
|
|
label = `${from.label} → ${t.label} Upgrade`;
|
|
} else {
|
|
price = t.price; isSub = !!(t as any).subscription; label = t.label;
|
|
}
|
|
}
|
|
|
|
const stripe = getStripe();
|
|
const returnUrl = `${cfg.stripe.appUrl}/donate/success?session_id={CHECKOUT_SESSION_ID}`;
|
|
|
|
const metadata: Record<string, string> = {
|
|
tier: tierId, platform: recipientPlatform, username: recipient.username,
|
|
uuid: recipient.uuid ?? "", xuid: recipient.xuid ?? "",
|
|
donor: username, gift: giftTo ? "1" : "0",
|
|
custom: tier === "custom" ? "1" : "0", customAmount: String(price), isSub: isSub ? "1" : "0",
|
|
upgradeFrom: verifiedFrom || "", upgrade: verifiedFrom ? "1" : "0",
|
|
};
|
|
|
|
const session = await stripe.checkout.sessions.create({
|
|
ui_mode: "elements",
|
|
integration_identifier: "frg_donate_embedded_qmfzkvbt",
|
|
mode: isSub ? "subscription" : "payment",
|
|
return_url: returnUrl,
|
|
metadata,
|
|
// copy metadata onto the subscription so webhook handlers can revoke later
|
|
...(isSub ? { subscription_data: { metadata } } : {}),
|
|
...(isSub
|
|
? { line_items: [{ price_data: { currency: "gbp", product_data: { name: tier === "custom" ? "Custom Patron" : label }, unit_amount: price, recurring: { interval: "month" } }, quantity: 1 }] }
|
|
: { line_items: [{ price_data: { currency: "gbp", product_data: { name: label }, unit_amount: price }, quantity: 1 }] }),
|
|
});
|
|
|
|
return NextResponse.json({ clientSecret: session.client_secret });
|
|
} catch (e: any) {
|
|
console.error("checkout error", e);
|
|
return NextResponse.json({ error: e?.message || "Checkout failed — check STRIPE_SECRET_KEY" }, { status: 500 });
|
|
}
|
|
}
|