's'调试支付
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const PB_URL = process.env.POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
|
||||
const COLLECTION = "solan";
|
||||
import { getPocketBaseConfig } from "@/config/services.config";
|
||||
import { getThemeConfig } from "@/config";
|
||||
|
||||
function getOrCreateUserId(): string {
|
||||
return `user${Date.now()}`;
|
||||
@@ -62,13 +61,19 @@ export async function POST(request: NextRequest) {
|
||||
(record as Record<string, string>)["media"] = mediaUrl;
|
||||
}
|
||||
|
||||
const theme = getThemeConfig();
|
||||
const pb = getPocketBaseConfig({
|
||||
joinCollection: theme.services?.pocketbaseJoinCollection,
|
||||
});
|
||||
const collection = pb.joinCollection;
|
||||
|
||||
const doCreate = async (authToken?: string) => {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (authToken) headers["Authorization"] = authToken;
|
||||
|
||||
return fetch(`${PB_URL}/api/collections/${COLLECTION}/records`, {
|
||||
return fetch(`${pb.url}/api/collections/${collection}/records`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(record),
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { PAYMENT_API_URL, SITE_URL } from "../../lib/env";
|
||||
|
||||
const ZPAY_SUBMIT = "https://zpayz.cn/submit.php";
|
||||
import { getPaymentConfig, getJoinPaymentConfig } from "@/config/services.config";
|
||||
import { SITE_URL } from "../../lib/env";
|
||||
|
||||
async function createPayOrder(
|
||||
user_id: string,
|
||||
return_url: string,
|
||||
total_fee = 80,
|
||||
type = "meetup",
|
||||
total_fee?: number,
|
||||
type?: string,
|
||||
channel = "alipay"
|
||||
) {
|
||||
if (!PAYMENT_API_URL) throw new Error("未配置 PAYMENT_API_URL");
|
||||
const joinConfig = getJoinPaymentConfig();
|
||||
const fee = total_fee ?? joinConfig.amount;
|
||||
const orderType = type ?? joinConfig.type;
|
||||
const config = getPaymentConfig();
|
||||
if (!config.apiUrl) throw new Error("未配置 PAYMENT_API_URL");
|
||||
|
||||
const payload = {
|
||||
user_id,
|
||||
total_fee: Number(total_fee) || 80,
|
||||
type,
|
||||
total_fee: Number(fee) || joinConfig.amount,
|
||||
type: orderType,
|
||||
channel,
|
||||
return_url,
|
||||
};
|
||||
@@ -23,7 +26,7 @@ async function createPayOrder(
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 10000);
|
||||
|
||||
const res = await fetch(`${PAYMENT_API_URL}/payh5`, {
|
||||
const res = await fetch(`${config.apiUrl}/payh5`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
@@ -36,7 +39,7 @@ async function createPayOrder(
|
||||
}
|
||||
|
||||
let params = data.params || data.xorpay_params || {};
|
||||
const payUrl = data.pay_url || data.xorpay_url || ZPAY_SUBMIT;
|
||||
const payUrl = data.pay_url || data.xorpay_url || config.zpaySubmitUrl;
|
||||
if (!params || typeof params !== "object") {
|
||||
throw new Error("payjsapi 未返回支付参数");
|
||||
}
|
||||
@@ -103,12 +106,14 @@ export async function POST(request: NextRequest) {
|
||||
body = Object.fromEntries(form.entries()) as Record<string, string>;
|
||||
}
|
||||
|
||||
const joinConfig = getJoinPaymentConfig();
|
||||
const payConfig = getPaymentConfig();
|
||||
const user_id = String(body?.user_id || "").trim();
|
||||
const return_url = String(body?.return_url || "").trim();
|
||||
const total_fee = Number(body?.total_fee) || 80;
|
||||
const type = String(body?.type || "meetup");
|
||||
const total_fee = Number(body?.total_fee) || joinConfig.amount;
|
||||
const type = String(body?.type || joinConfig.type);
|
||||
const channel = String(body?.channel || "alipay");
|
||||
const device = String(body?.device || "pc"); // pc | h5,前端根据 UA 传入
|
||||
const device = String(body?.device || "pc"); // pc | h5 | wechat,微信内传 wechat
|
||||
|
||||
if (!user_id) {
|
||||
return NextResponse.json({ ok: false, error: "缺少 user_id" }, { status: 400 });
|
||||
@@ -118,7 +123,8 @@ export async function POST(request: NextRequest) {
|
||||
request.headers.get("x-forwarded-host")
|
||||
? `${request.headers.get("x-forwarded-proto") || "https"}://${request.headers.get("x-forwarded-host")}`
|
||||
: request.headers.get("origin") || SITE_URL;
|
||||
const finalReturnUrl = return_url || `${origin}/join?paid=1`;
|
||||
// Z-Pay return_url 不支持带参数,使用 /join/paid 专用路径
|
||||
const finalReturnUrl = return_url || `${origin}/zh/join/paid`;
|
||||
|
||||
const { params, payUrl } = await createPayOrder(
|
||||
user_id,
|
||||
@@ -151,7 +157,7 @@ export async function POST(request: NextRequest) {
|
||||
};
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 15000);
|
||||
const redirectRes = await fetch(`${PAYMENT_API_URL}/payh5/redirect`, {
|
||||
const redirectRes = await fetch(`${payConfig.apiUrl}/payh5/redirect`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(redirectPayload),
|
||||
@@ -160,9 +166,38 @@ export async function POST(request: NextRequest) {
|
||||
}).finally(() => clearTimeout(timeout));
|
||||
if (redirectRes.status >= 301 && redirectRes.status <= 308) {
|
||||
const location = redirectRes.headers.get("location");
|
||||
if (location) return NextResponse.redirect(location);
|
||||
if (location) {
|
||||
const res = NextResponse.redirect(location);
|
||||
// payh5/redirect 创建的是新订单,必须用其返回的 order_id(非 createPayOrder 的 params)
|
||||
const orderId =
|
||||
redirectRes.headers.get("x-pay-order-id")?.trim() ||
|
||||
String(params?.out_trade_no || "").trim();
|
||||
if (orderId) {
|
||||
// 附带时间戳,前端只对 15 分钟内设置的 cookie 轮询,避免陈旧 cookie 误触发
|
||||
const payload = `${orderId}|${Date.now()}`;
|
||||
res.cookies.set("join_pay_order", payload, { path: "/", maxAge: 900 });
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
const resData = await redirectRes.json().catch(() => ({}));
|
||||
// 微信内:payurl2 会提示「请外微信外打开」,改用收银台表单由 Z-Pay 识别 UA
|
||||
if (redirectRes.status === 200 && resData?.use_form) {
|
||||
const html = buildPayHtml(payConfig.zpaySubmitUrl, params as Record<string, unknown>);
|
||||
const res = new NextResponse(html, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "text/html; charset=utf-8" },
|
||||
});
|
||||
const orderId =
|
||||
resData?.order_id?.trim() || String(params?.out_trade_no || "").trim();
|
||||
if (orderId) {
|
||||
res.cookies.set("join_pay_order", `${orderId}|${Date.now()}`, {
|
||||
path: "/",
|
||||
maxAge: 900,
|
||||
});
|
||||
}
|
||||
return res;
|
||||
}
|
||||
if (redirectRes.status === 200 && resData?.qrcode_mode && resData?.img) {
|
||||
const returnUrl = String(resData.return_url || finalReturnUrl).replace(/[&<>"']/g, (c) =>
|
||||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] || c
|
||||
@@ -247,7 +282,7 @@ export async function POST(request: NextRequest) {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
const zpayRes = await fetch(ZPAY_SUBMIT, {
|
||||
const zpayRes = await fetch(payConfig.zpaySubmitUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: formParams.toString(),
|
||||
@@ -255,7 +290,13 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
const location = zpayRes.headers.get("location");
|
||||
if ((zpayRes.status === 301 || zpayRes.status === 302) && location) {
|
||||
return NextResponse.redirect(location);
|
||||
const res = NextResponse.redirect(location);
|
||||
const outTradeNo = String(params?.out_trade_no || "").trim();
|
||||
if (outTradeNo) {
|
||||
const payload = `${outTradeNo}|${Date.now()}`;
|
||||
res.cookies.set("join_pay_order", payload, { path: "/", maxAge: 900 });
|
||||
}
|
||||
return res;
|
||||
}
|
||||
const text = await zpayRes.text();
|
||||
try {
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { PAYMENT_API_URL } from "../../../lib/env";
|
||||
import { getPaymentConfig } from "@/config/services.config";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const orderId = request.nextUrl.searchParams.get("order_id");
|
||||
if (!orderId) {
|
||||
return NextResponse.json({ ok: false, error: "缺少 order_id" }, { status: 400 });
|
||||
}
|
||||
if (!PAYMENT_API_URL) {
|
||||
const config = getPaymentConfig();
|
||||
if (!config.apiUrl) {
|
||||
return NextResponse.json({ ok: false, error: "未配置 PAYMENT_API_URL" }, { status: 500 });
|
||||
}
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${PAYMENT_API_URL}/zpay_order_status?out_trade_no=${encodeURIComponent(orderId)}`,
|
||||
`${config.apiUrl}/zpay_order_status?out_trade_no=${encodeURIComponent(orderId)}`,
|
||||
{ cache: "no-store" }
|
||||
);
|
||||
const data = await res.json().catch(() => ({}));
|
||||
|
||||
@@ -1,17 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import * as Minio from "minio";
|
||||
import { uploadBuffer, generateObjectKey } from "@/app/lib/minio";
|
||||
|
||||
// mc alias list myminio: http://minioweb.hackrobot.cn:9035
|
||||
const MINIO_ENDPOINT = process.env.MINIO_ENDPOINT || "minioweb.hackrobot.cn";
|
||||
const MINIO_PORT = parseInt(process.env.MINIO_PORT || "9035", 10);
|
||||
const MINIO_USE_SSL = process.env.MINIO_USE_SSL === "true";
|
||||
const MINIO_BUCKET = process.env.MINIO_BUCKET || "hackrobot";
|
||||
const MINIO_ACCESS_KEY = process.env.MINIO_ACCESS_KEY || "6i56HHfg4zPfYItCZtnp";
|
||||
const MINIO_SECRET_KEY = process.env.MINIO_SECRET_KEY || "vDJCqEit3ejH5UmWKAZnvqhziNfbVsoOlBW12G8Q";
|
||||
// 公开访问 URL(https 无端口),API 上传用 9035
|
||||
const MINIO_PUBLIC_URL =
|
||||
process.env.MINIO_PUBLIC_URL ||
|
||||
`https://${MINIO_ENDPOINT}/${MINIO_BUCKET}`;
|
||||
const MAX_SIZE = 20 * 1024 * 1024; // 20MB
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -27,38 +17,23 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (fileObj.size > 20 * 1024 * 1024) {
|
||||
if (fileObj.size > MAX_SIZE) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "文件大小不能超过 20MB" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const client = new Minio.Client({
|
||||
endPoint: MINIO_ENDPOINT,
|
||||
port: MINIO_PORT,
|
||||
useSSL: MINIO_USE_SSL,
|
||||
accessKey: MINIO_ACCESS_KEY,
|
||||
secretKey: MINIO_SECRET_KEY,
|
||||
pathStyle: true,
|
||||
region: process.env.MINIO_REGION || "china",
|
||||
});
|
||||
|
||||
const ext =
|
||||
(fileObj instanceof File ? fileObj.name : "").split(".").pop() ||
|
||||
(fileObj.type?.startsWith("video/") ? "mp4" : "jpg");
|
||||
const objectKey = `joins/${Date.now()}_${Math.random().toString(36).slice(2)}.${ext}`;
|
||||
const objectKey = generateObjectKey(ext);
|
||||
|
||||
const buffer = Buffer.from(await fileObj.arrayBuffer());
|
||||
const contentType =
|
||||
(fileObj as File).type || "application/octet-stream";
|
||||
|
||||
await client.putObject(MINIO_BUCKET, objectKey, buffer, buffer.length, {
|
||||
"Content-Type": contentType,
|
||||
});
|
||||
|
||||
const base = MINIO_PUBLIC_URL.replace(/\/$/, "");
|
||||
const url = `${base}/${objectKey}`;
|
||||
const { url } = await uploadBuffer(buffer, objectKey, contentType);
|
||||
|
||||
return NextResponse.json({ ok: true, url });
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user