's'
This commit is contained in:
@@ -13,6 +13,7 @@ const ANONYMOUS_USER_COOKIE = "nomadvip_anon_uid";
|
||||
const ANONYMOUS_USER_COOKIE_MAX_AGE = 60 * 60 * 24 * 30;
|
||||
const PAY_ORDER_COOKIE = "nomadvip_pay_order";
|
||||
const PAY_ORDER_MAX_AGE = 60 * 15;
|
||||
const VIP_PAY_TEST_MODE_KEY = "vipPayTestMode";
|
||||
|
||||
function getStorage(key: string): string | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
@@ -114,6 +115,33 @@ async function getOrCreateUserId(): Promise<string> {
|
||||
return syncId;
|
||||
}
|
||||
|
||||
async function recoverAnonymousUserIdByWechatUserId(): Promise<string | null> {
|
||||
const wechatUserId =
|
||||
(getStorage("wechatUserId") || "").trim() ||
|
||||
(getStorage("userid") || "").trim();
|
||||
if (!wechatUserId) return null;
|
||||
try {
|
||||
const res = await fetch("/api/vip/identity-diagnose", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ wechat_userid: wechatUserId }),
|
||||
credentials: "same-origin",
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
const candidate = [
|
||||
data?.resolved?.linked_anonymous_user_id,
|
||||
data?.vip_email_link?.by_anonymous_user_id?.anonymous_user_id,
|
||||
data?.vip_email_link?.by_email?.anonymous_user_id,
|
||||
data?.payments?.by_user_id?.user_id,
|
||||
data?.payments?.by_linked_user_id?.user_id,
|
||||
].find((x) => typeof x === "string" && /^user\d+$/.test(x.trim()));
|
||||
return typeof candidate === "string" ? candidate.trim() : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function redirectToPayZpay(params: {
|
||||
user_id: string;
|
||||
return_url: string;
|
||||
@@ -248,8 +276,10 @@ function payWechatXorpaySync(params: {
|
||||
export async function triggerPay(
|
||||
onPendingOrder?: (orderId: string) => void
|
||||
): Promise<void> {
|
||||
const userId =
|
||||
typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent)
|
||||
const recoveredByWechat = await recoverAnonymousUserIdByWechatUserId();
|
||||
const userId = recoveredByWechat
|
||||
? recoveredByWechat
|
||||
: typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent)
|
||||
? getOrCreateUserIdSync()
|
||||
: await getOrCreateUserId();
|
||||
if (/^user\d+$/.test(userId)) {
|
||||
@@ -260,7 +290,8 @@ export async function triggerPay(
|
||||
const returnUrl = `${window.location.origin}/paid`;
|
||||
const env = getPayEnv();
|
||||
const device = getDeviceFromEnv(env);
|
||||
const totalFee = getPaymentConfig().defaultAmount ?? 8800;
|
||||
const isPayTestMode = getStorage(VIP_PAY_TEST_MODE_KEY) === "1";
|
||||
const totalFee = isPayTestMode ? 100 : getPaymentConfig().defaultAmount ?? 8800;
|
||||
const base = {
|
||||
user_id: userId,
|
||||
return_url: returnUrl,
|
||||
|
||||
Reference in New Issue
Block a user