's'
This commit is contained in:
@@ -72,6 +72,7 @@ function setAnonymousUserCookie(
|
|||||||
async function createPayOrder(
|
async function createPayOrder(
|
||||||
apiUrl: string,
|
apiUrl: string,
|
||||||
user_id: string,
|
user_id: string,
|
||||||
|
wechat_userid: string | undefined,
|
||||||
return_url: string,
|
return_url: string,
|
||||||
total_fee: number,
|
total_fee: number,
|
||||||
type: string,
|
type: string,
|
||||||
@@ -81,6 +82,7 @@ async function createPayOrder(
|
|||||||
) {
|
) {
|
||||||
const payload: Record<string, unknown> = {
|
const payload: Record<string, unknown> = {
|
||||||
user_id,
|
user_id,
|
||||||
|
...(wechat_userid ? { wechat_userid } : {}),
|
||||||
site_id: "vip",
|
site_id: "vip",
|
||||||
total_fee,
|
total_fee,
|
||||||
type,
|
type,
|
||||||
@@ -141,6 +143,7 @@ export async function POST(request: NextRequest) {
|
|||||||
).replace(/\/$/, "");
|
).replace(/\/$/, "");
|
||||||
console.log("[pay] 立即加入 apiUrl=%s host=%s", apiUrl, hostHeader);
|
console.log("[pay] 立即加入 apiUrl=%s host=%s", apiUrl, hostHeader);
|
||||||
let user_id = String(body?.user_id || "").trim();
|
let user_id = String(body?.user_id || "").trim();
|
||||||
|
const wechat_userid = String(body?.wechat_userid || body?.userid || "").trim();
|
||||||
if (user_id.startsWith("{") && user_id.includes("data")) {
|
if (user_id.startsWith("{") && user_id.includes("data")) {
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(user_id) as { data?: string };
|
const parsed = JSON.parse(user_id) as { data?: string };
|
||||||
@@ -187,6 +190,7 @@ export async function POST(request: NextRequest) {
|
|||||||
if (effectiveProvider === "zpay") {
|
if (effectiveProvider === "zpay") {
|
||||||
const redirectPayload = {
|
const redirectPayload = {
|
||||||
user_id,
|
user_id,
|
||||||
|
...(wechat_userid ? { wechat_userid } : {}),
|
||||||
site_id: "vip",
|
site_id: "vip",
|
||||||
total_fee,
|
total_fee,
|
||||||
type,
|
type,
|
||||||
@@ -429,6 +433,7 @@ h2{color:#1e293b;margin:0 0 8px;font-size:1.5rem;}
|
|||||||
const { params, payUrl, provider } = await createPayOrder(
|
const { params, payUrl, provider } = await createPayOrder(
|
||||||
apiUrl,
|
apiUrl,
|
||||||
user_id,
|
user_id,
|
||||||
|
wechat_userid || undefined,
|
||||||
finalReturnUrl,
|
finalReturnUrl,
|
||||||
total_fee,
|
total_fee,
|
||||||
type,
|
type,
|
||||||
|
|||||||
@@ -115,35 +115,9 @@ async function getOrCreateUserId(): Promise<string> {
|
|||||||
return syncId;
|
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: {
|
function redirectToPayZpay(params: {
|
||||||
user_id: string;
|
user_id: string;
|
||||||
|
wechat_userid?: string;
|
||||||
return_url: string;
|
return_url: string;
|
||||||
total_fee: number;
|
total_fee: number;
|
||||||
type: string;
|
type: string;
|
||||||
@@ -157,6 +131,7 @@ function redirectToPayZpay(params: {
|
|||||||
form.style.display = "none";
|
form.style.display = "none";
|
||||||
const fields: [string, string][] = [
|
const fields: [string, string][] = [
|
||||||
["user_id", params.user_id],
|
["user_id", params.user_id],
|
||||||
|
...(params.wechat_userid ? [["wechat_userid", params.wechat_userid] as [string, string]] : []),
|
||||||
["return_url", params.return_url],
|
["return_url", params.return_url],
|
||||||
["total_fee", String(params.total_fee)],
|
["total_fee", String(params.total_fee)],
|
||||||
["type", params.type],
|
["type", params.type],
|
||||||
@@ -179,6 +154,7 @@ function redirectToPayZpay(params: {
|
|||||||
async function redirectToPayZpayH5(
|
async function redirectToPayZpayH5(
|
||||||
params: {
|
params: {
|
||||||
user_id: string;
|
user_id: string;
|
||||||
|
wechat_userid?: string;
|
||||||
return_url: string;
|
return_url: string;
|
||||||
total_fee: number;
|
total_fee: number;
|
||||||
type: string;
|
type: string;
|
||||||
@@ -238,6 +214,7 @@ async function redirectToPayZpayH5(
|
|||||||
/** 微信内:同步表单 POST 到 /api/pay,避免异步 fetch 后 form.submit 被微信拦截 */
|
/** 微信内:同步表单 POST 到 /api/pay,避免异步 fetch 后 form.submit 被微信拦截 */
|
||||||
function payWechatXorpaySync(params: {
|
function payWechatXorpaySync(params: {
|
||||||
user_id: string;
|
user_id: string;
|
||||||
|
wechat_userid?: string;
|
||||||
return_url: string;
|
return_url: string;
|
||||||
total_fee: number;
|
total_fee: number;
|
||||||
type: string;
|
type: string;
|
||||||
@@ -250,6 +227,7 @@ function payWechatXorpaySync(params: {
|
|||||||
form.style.display = "none";
|
form.style.display = "none";
|
||||||
const fields: [string, string][] = [
|
const fields: [string, string][] = [
|
||||||
["user_id", params.user_id],
|
["user_id", params.user_id],
|
||||||
|
...(params.wechat_userid ? [["wechat_userid", params.wechat_userid] as [string, string]] : []),
|
||||||
["return_url", params.return_url],
|
["return_url", params.return_url],
|
||||||
["total_fee", String(params.total_fee)],
|
["total_fee", String(params.total_fee)],
|
||||||
["type", params.type],
|
["type", params.type],
|
||||||
@@ -276,10 +254,8 @@ function payWechatXorpaySync(params: {
|
|||||||
export async function triggerPay(
|
export async function triggerPay(
|
||||||
onPendingOrder?: (orderId: string) => void
|
onPendingOrder?: (orderId: string) => void
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const recoveredByWechat = await recoverAnonymousUserIdByWechatUserId();
|
const userId =
|
||||||
const userId = recoveredByWechat
|
typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent)
|
||||||
? recoveredByWechat
|
|
||||||
: typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent)
|
|
||||||
? getOrCreateUserIdSync()
|
? getOrCreateUserIdSync()
|
||||||
: await getOrCreateUserId();
|
: await getOrCreateUserId();
|
||||||
if (/^user\d+$/.test(userId)) {
|
if (/^user\d+$/.test(userId)) {
|
||||||
@@ -290,10 +266,14 @@ export async function triggerPay(
|
|||||||
const returnUrl = `${window.location.origin}/paid`;
|
const returnUrl = `${window.location.origin}/paid`;
|
||||||
const env = getPayEnv();
|
const env = getPayEnv();
|
||||||
const device = getDeviceFromEnv(env);
|
const device = getDeviceFromEnv(env);
|
||||||
|
const wechatUserId =
|
||||||
|
(getStorage("wechatUserId") || "").trim() ||
|
||||||
|
(getStorage("userid") || "").trim();
|
||||||
const isPayTestMode = getStorage(VIP_PAY_TEST_MODE_KEY) === "1";
|
const isPayTestMode = getStorage(VIP_PAY_TEST_MODE_KEY) === "1";
|
||||||
const totalFee = isPayTestMode ? 100 : getPaymentConfig().defaultAmount ?? 8800;
|
const totalFee = isPayTestMode ? 100 : getPaymentConfig().defaultAmount ?? 8800;
|
||||||
const base = {
|
const base = {
|
||||||
user_id: userId,
|
user_id: userId,
|
||||||
|
wechat_userid: wechatUserId || undefined,
|
||||||
return_url: returnUrl,
|
return_url: returnUrl,
|
||||||
total_fee: totalFee,
|
total_fee: totalFee,
|
||||||
type: "vip",
|
type: "vip",
|
||||||
|
|||||||
Reference in New Issue
Block a user