This commit is contained in:
eric
2026-03-10 08:11:06 -05:00
parent 78b215b314
commit ceb951f50e
2 changed files with 73 additions and 17 deletions

View File

@@ -13,7 +13,8 @@ import { usePayStatusPoll } from "@/app/lib/usePayStatusPoll";
const PB_URL =
process.env.NEXT_PUBLIC_POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
function redirectToPay(params: {
/** PC/H5:form POST 到 /api/pay,有中转页,默认 zpay */
function redirectToPayZpay(params: {
user_id: string;
return_url: string;
total_fee: number;
@@ -33,6 +34,7 @@ function redirectToPay(params: {
["type", params.type],
["channel", params.channel],
["device", params.device],
["provider", "zpay"],
["html", "1"],
];
fields.forEach(([k, v]) => {
@@ -46,6 +48,47 @@ function redirectToPay(params: {
form.submit();
}
/** 微信内:fetch 获取 xorpay 参数,直接提交表单到收银台,无中转页 */
async function payWechatXorpay(params: {
user_id: string;
return_url: string;
total_fee: number;
type: string;
channel: "alipay" | "wxpay";
}) {
const res = await fetch("/api/pay", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
...params,
total_fee: params.total_fee,
device: "wechat",
provider: "xorpay",
html: "0",
}),
});
const data = await res.json().catch(() => ({}));
if (!data?.ok || !data?.params || !data?.payUrl) {
alert(data?.error || "支付发起失败");
return;
}
const form = document.createElement("form");
form.method = "POST";
form.action = data.payUrl;
form.target = "_self";
form.style.display = "none";
for (const [k, v] of Object.entries(data.params as Record<string, string>)) {
if (v == null) continue;
const input = document.createElement("input");
input.type = "hidden";
input.name = k;
input.value = String(v);
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
}
function getStorage(key: string): string | null {
if (typeof window === "undefined") return null;
return localStorage.getItem(key);
@@ -162,14 +205,12 @@ export default function Home() {
const env = getPayEnv();
const device = getDeviceFromEnv(env);
const totalFee = getPaymentConfig().defaultAmount ?? 880;
redirectToPay({
user_id: userId,
return_url: returnUrl,
total_fee: totalFee,
type: "vip",
channel: "wxpay",
device,
});
const base = { user_id: userId, return_url: returnUrl, total_fee: totalFee, type: "vip", channel: "wxpay" as const };
if (device === "wechat") {
payWechatXorpay(base);
} else {
redirectToPayZpay({ ...base, device });
}
}, [getOrCreateUserId]);
const handleCopyUsername = useCallback(() => {