This commit is contained in:
eric
2026-04-01 02:23:17 -05:00
parent 056dadc817
commit de86a65293
8 changed files with 202 additions and 52 deletions

View File

@@ -11,9 +11,18 @@ import {
import { queryXorPayOrderStatus } from "@/app/lib/xorpayStatus";
import { queryZPayOrderStatus } from "@/app/lib/zpayStatus";
const getTotalFee = () =>
const getDefaultTotalFee = () =>
getPaymentConfig().defaultAmount ?? Number(process.env.PAYMENT_DEFAULT_AMOUNT) ?? 8800;
function parsePayPriceToCents(payPrice: unknown): number | null {
if (payPrice == null) return null;
const raw = String(payPrice).trim();
if (!raw) return null;
const value = Number(raw);
if (!Number.isFinite(value) || value <= 0) return null;
return Math.round(value * 100);
}
async function getAdminToken(): Promise<string | null> {
const email = process.env.POCKETBASE_EMAIL;
const password = process.env.POCKETBASE_PASSWORD;
@@ -46,7 +55,7 @@ function resolvePayApiUrl(request: NextRequest): string {
async function verifyOrderPaid(
request: NextRequest,
orderId: string
): Promise<boolean> {
): Promise<{ paid: boolean; totalFeeCents: number | null }> {
const apiUrl = resolvePayApiUrl(request);
const url = `${apiUrl}/nomadvip/order_status?order_id=${encodeURIComponent(orderId)}`;
console.log("[pay/confirm] verify order_status url=%s", url);
@@ -59,11 +68,12 @@ async function verifyOrderPaid(
}).finally(() => clearTimeout(timeout));
const data = await res.json().catch(() => ({}));
const paid = !!data?.paid;
const totalFeeCents = parsePayPriceToCents(data?.pay_price);
if (!paid && !res.ok) {
console.warn("[pay/confirm] order_status non-ok:", res.status, url, data);
}
if (paid) {
return true;
return { paid: true, totalFeeCents };
}
} catch (e) {
console.error("[pay/confirm] verifyOrderPaid fetch error:", e);
@@ -79,7 +89,10 @@ async function verifyOrderPaid(
xorpay?.url || ""
);
if (xorpay?.paid) {
return true;
return {
paid: true,
totalFeeCents: parsePayPriceToCents(xorpay?.pay_price),
};
}
const zpay = await queryZPayOrderStatus(orderId, 5000);
@@ -91,7 +104,10 @@ async function verifyOrderPaid(
zpay?.error || "",
zpay?.url || ""
);
return !!zpay?.paid;
return {
paid: !!zpay?.paid,
totalFeeCents: parsePayPriceToCents(zpay?.pay_price),
};
}
async function createMemosUser(username: string, password: string): Promise<boolean> {
@@ -142,8 +158,8 @@ export async function POST(request: NextRequest) {
return NextResponse.json({ ok: false, error: "缺少 order_id 或 user_id" }, { status: 400 });
}
const paid = await verifyOrderPaid(request, orderId);
if (!paid) {
const verifyResult = await verifyOrderPaid(request, orderId);
if (!verifyResult.paid) {
console.error("[pay/confirm] 订单未支付 order_id=%s user_id=%s (请检查 payjsapi order_status 接口)", orderId, userId);
return NextResponse.json({ ok: false, error: "订单未支付" }, { status: 400 });
}
@@ -154,7 +170,7 @@ export async function POST(request: NextRequest) {
}
const pb = getPocketBaseConfig();
const totalFee = getTotalFee();
const totalFee = verifyResult.totalFeeCents ?? getDefaultTotalFee();
const checkExisting = await fetch(
`${pb.url}/api/collections/payments/records?filter=${encodeURIComponent(`order_id="${orderId}"`)}&perPage=1`,