This commit is contained in:
eric
2026-03-13 05:51:05 -05:00
parent 2365bb9e17
commit c1eb0bbcf3
5 changed files with 17 additions and 8 deletions

View File

@@ -54,18 +54,27 @@ function parsePendingOrder(raw: string | null): [string | null, boolean] {
return [orderId, true];
}
export function usePayStatusPoll(onPaid: () => void): boolean {
export function usePayStatusPoll(onPaid: (orderId: string) => void): boolean {
const [polling, setPolling] = useState(false);
const onPaidRef = useRef(onPaid);
onPaidRef.current = onPaid;
useEffect(() => {
const urlOrderId =
typeof window !== "undefined"
? new URLSearchParams(window.location.search).get("order_id") || ""
: "";
const cookieRaw = getCookie(COOKIE_NAME);
const [cookieOrderId, cookieValid] = parsePendingOrder(cookieRaw);
const storageRaw = getStorage(STORAGE_NAME);
const [storageOrderId, storageValid] = parsePendingOrder(storageRaw);
const orderId = cookieValid ? cookieOrderId : storageValid ? storageOrderId : null;
const activeRaw = cookieValid ? cookieRaw : storageValid ? storageRaw : null;
let orderId = cookieValid ? cookieOrderId : storageValid ? storageOrderId : null;
let activeRaw = cookieValid ? cookieRaw : storageValid ? storageRaw : null;
if (!orderId && urlOrderId.trim()) {
orderId = urlOrderId.trim();
activeRaw = `${orderId}|${Date.now()}`;
persistPendingOrder(activeRaw);
}
if (!orderId) {
if (cookieRaw || storageRaw) {
@@ -134,7 +143,7 @@ export function usePayStatusPoll(onPaid: () => void): boolean {
} catch {
/* 忽略,不影响跳转 */
}
onPaidRef.current();
onPaidRef.current(orderId);
return;
}
} catch {