This commit is contained in:
eric
2026-03-10 08:46:58 -05:00
parent 9b4467d4f7
commit 6f6f0fa527
3 changed files with 109 additions and 10 deletions

View File

@@ -8,7 +8,7 @@ import { indexMd } from "./index-content";
import styles from "./index.module.css";
import { getPayEnv, getDeviceFromEnv } from "@/app/lib/payEnv";
import { getPaymentConfig } from "@/config/services.config";
import { usePayStatusPoll } from "@/app/lib/usePayStatusPoll";
import { usePayStatusPoll, setPayPendingOrder } from "@/app/lib/usePayStatusPoll";
const PB_URL =
process.env.NEXT_PUBLIC_POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
@@ -16,7 +16,7 @@ const PB_URL =
/** 微信内支付渠道:zpay | xorpay,改此参数即可切换 */
const WECHAT_PAY_PROVIDER: "zpay" | "xorpay" = "xorpay";
/** PC/H5:form POST 到 /api/pay,有中转页,默认 zpay */
/** PC:form POST 到 /api/pay,有中转页 */
function redirectToPayZpay(params: {
user_id: string;
return_url: string;
@@ -51,7 +51,53 @@ function redirectToPayZpay(params: {
form.submit();
}
/** 微信内:fetch 获取 xorpay 参数,直接提交表单到收银台,无中转页 */
/** H5 手机:fetch 获取 order_id + 支付参数,写入 sessionStorage 后提交表单,支持轮询 */
async function redirectToPayZpayH5(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,
device: "h5",
provider: "zpay",
html: "0",
}),
});
const data = await res.json().catch(() => ({}));
if (!data?.ok || !data?.params || !data?.payUrl) {
alert(data?.error || "支付发起失败");
return;
}
const orderId = data?.order_id?.trim();
if (orderId) setPayPendingOrder(orderId);
if (data?.redirect_url) {
window.location.href = data.redirect_url;
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();
}
/** 微信内:fetch 获取 xorpay 参数,写入 order_id 后提交表单到收银台,无中转页 */
async function payWechatXorpay(params: {
user_id: string;
return_url: string;
@@ -75,6 +121,8 @@ async function payWechatXorpay(params: {
alert(data?.error || "支付发起失败");
return;
}
const orderId = data?.order_id?.trim();
if (orderId) setPayPendingOrder(orderId);
const form = document.createElement("form");
form.method = "POST";
form.action = data.payUrl;
@@ -211,6 +259,8 @@ export default function Home() {
const base = { user_id: userId, return_url: returnUrl, total_fee: totalFee, type: "vip", channel: "wxpay" as const };
if (device === "wechat" && WECHAT_PAY_PROVIDER === "xorpay") {
payWechatXorpay(base);
} else if (device === "h5") {
redirectToPayZpayH5(base);
} else {
redirectToPayZpay({ ...base, device });
}