;'s'
This commit is contained in:
62
app/page.tsx
62
app/page.tsx
@@ -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, setPayPendingOrder } from "@/app/lib/usePayStatusPoll";
|
||||
import { usePayStatusPoll } from "@/app/lib/usePayStatusPoll";
|
||||
|
||||
const PB_URL =
|
||||
process.env.NEXT_PUBLIC_POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
|
||||
@@ -51,14 +51,14 @@ function redirectToPayZpay(params: {
|
||||
form.submit();
|
||||
}
|
||||
|
||||
/** H5 手机:fetch 获取 order_id + 支付参数,写入 sessionStorage 后提交表单,支持轮询 */
|
||||
/** H5 手机:fetch 获取 order_id,新标签页打开支付,当前页保持可轮询 */
|
||||
async function redirectToPayZpayH5(params: {
|
||||
user_id: string;
|
||||
return_url: string;
|
||||
total_fee: number;
|
||||
type: string;
|
||||
channel: "alipay" | "wxpay";
|
||||
}) {
|
||||
}): Promise<string | null> {
|
||||
const res = await fetch("/api/pay", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -72,18 +72,17 @@ async function redirectToPayZpayH5(params: {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!data?.ok || !data?.params || !data?.payUrl) {
|
||||
alert(data?.error || "支付发起失败");
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
const orderId = data?.order_id?.trim();
|
||||
if (orderId) setPayPendingOrder(orderId);
|
||||
const orderId = data?.order_id?.trim() || null;
|
||||
if (data?.redirect_url) {
|
||||
window.location.href = data.redirect_url;
|
||||
return;
|
||||
window.open(data.redirect_url, "_blank");
|
||||
return orderId;
|
||||
}
|
||||
const form = document.createElement("form");
|
||||
form.method = "POST";
|
||||
form.action = data.payUrl;
|
||||
form.target = "_self";
|
||||
form.target = "_blank";
|
||||
form.style.display = "none";
|
||||
for (const [k, v] of Object.entries(data.params as Record<string, string>)) {
|
||||
if (v == null) continue;
|
||||
@@ -95,9 +94,11 @@ async function redirectToPayZpayH5(params: {
|
||||
}
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
document.body.removeChild(form);
|
||||
return orderId;
|
||||
}
|
||||
|
||||
/** 微信内:fetch 获取 xorpay 参数,写入 order_id 后提交表单到收银台,无中转页 */
|
||||
/** 微信内:fetch 获取 xorpay 参数,直接提交表单到收银台,无中转页 */
|
||||
async function payWechatXorpay(params: {
|
||||
user_id: string;
|
||||
return_url: string;
|
||||
@@ -121,8 +122,6 @@ 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;
|
||||
@@ -159,6 +158,7 @@ export default function Home() {
|
||||
const [paidType, setPaidType] = useState<string | null>(null);
|
||||
const [userName, setUserName] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [payPendingOrderId, setPayPendingOrderId] = useState<string | null>(null);
|
||||
const contentRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const savePaidType = useCallback((type: string | null) => {
|
||||
@@ -250,7 +250,7 @@ export default function Home() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handlePay = useCallback(() => {
|
||||
const handlePay = useCallback(async () => {
|
||||
const userId = getOrCreateUserId();
|
||||
const returnUrl = `${window.location.origin}/paid`;
|
||||
const env = getPayEnv();
|
||||
@@ -260,7 +260,8 @@ export default function Home() {
|
||||
if (device === "wechat" && WECHAT_PAY_PROVIDER === "xorpay") {
|
||||
payWechatXorpay(base);
|
||||
} else if (device === "h5") {
|
||||
redirectToPayZpayH5(base);
|
||||
const orderId = await redirectToPayZpayH5(base);
|
||||
if (orderId) setPayPendingOrderId(orderId);
|
||||
} else {
|
||||
redirectToPayZpay({ ...base, device });
|
||||
}
|
||||
@@ -274,18 +275,27 @@ export default function Home() {
|
||||
}
|
||||
}, [userName]);
|
||||
|
||||
// 微信/手机 H5 支付完成后可能不跳转,轮询检测到已支付则刷新
|
||||
usePayStatusPoll((orderId) => {
|
||||
let uid = getStorage("userId");
|
||||
if (!uid && orderId?.includes("_order_")) {
|
||||
const prefix = orderId.split("_order_")[0];
|
||||
const parts = prefix.split("_");
|
||||
if (parts.length >= 2) uid = parts.slice(0, -1).join("_");
|
||||
}
|
||||
if (uid) saveUserName(uid);
|
||||
savePaidType("vip");
|
||||
window.location.reload();
|
||||
});
|
||||
const env = getPayEnv();
|
||||
const device = getDeviceFromEnv(env);
|
||||
const isH5 = device === "h5";
|
||||
usePayStatusPoll(
|
||||
(orderId) => {
|
||||
let uid = getStorage("userId");
|
||||
if (!uid && orderId?.includes("_order_")) {
|
||||
const prefix = orderId.split("_order_")[0];
|
||||
const parts = prefix.split("_");
|
||||
if (parts.length >= 2) uid = parts.slice(0, -1).join("_");
|
||||
}
|
||||
if (uid) saveUserName(uid);
|
||||
savePaidType("vip");
|
||||
if (isH5) setPayPendingOrderId(null);
|
||||
window.location.reload();
|
||||
},
|
||||
() => {
|
||||
if (isH5) setPayPendingOrderId(null);
|
||||
},
|
||||
isH5 ? payPendingOrderId : undefined
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
|
||||
Reference in New Issue
Block a user