's'
This commit is contained in:
59
app/page.tsx
59
app/page.tsx
@@ -13,7 +13,8 @@ import { usePayStatusPoll } from "@/app/lib/usePayStatusPoll";
|
||||
const PB_URL =
|
||||
process.env.NEXT_PUBLIC_POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
|
||||
|
||||
function redirectToPay(params: {
|
||||
/** PC/H5:form POST 到 /api/pay,有中转页,默认 zpay */
|
||||
function redirectToPayZpay(params: {
|
||||
user_id: string;
|
||||
return_url: string;
|
||||
total_fee: number;
|
||||
@@ -33,6 +34,7 @@ function redirectToPay(params: {
|
||||
["type", params.type],
|
||||
["channel", params.channel],
|
||||
["device", params.device],
|
||||
["provider", "zpay"],
|
||||
["html", "1"],
|
||||
];
|
||||
fields.forEach(([k, v]) => {
|
||||
@@ -46,6 +48,47 @@ function redirectToPay(params: {
|
||||
form.submit();
|
||||
}
|
||||
|
||||
/** 微信内:fetch 获取 xorpay 参数,直接提交表单到收银台,无中转页 */
|
||||
async function payWechatXorpay(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,
|
||||
total_fee: params.total_fee,
|
||||
device: "wechat",
|
||||
provider: "xorpay",
|
||||
html: "0",
|
||||
}),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!data?.ok || !data?.params || !data?.payUrl) {
|
||||
alert(data?.error || "支付发起失败");
|
||||
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();
|
||||
}
|
||||
|
||||
function getStorage(key: string): string | null {
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem(key);
|
||||
@@ -162,14 +205,12 @@ export default function Home() {
|
||||
const env = getPayEnv();
|
||||
const device = getDeviceFromEnv(env);
|
||||
const totalFee = getPaymentConfig().defaultAmount ?? 880;
|
||||
redirectToPay({
|
||||
user_id: userId,
|
||||
return_url: returnUrl,
|
||||
total_fee: totalFee,
|
||||
type: "vip",
|
||||
channel: "wxpay",
|
||||
device,
|
||||
});
|
||||
const base = { user_id: userId, return_url: returnUrl, total_fee: totalFee, type: "vip", channel: "wxpay" as const };
|
||||
if (device === "wechat") {
|
||||
payWechatXorpay(base);
|
||||
} else {
|
||||
redirectToPayZpay({ ...base, device });
|
||||
}
|
||||
}, [getOrCreateUserId]);
|
||||
|
||||
const handleCopyUsername = useCallback(() => {
|
||||
|
||||
Reference in New Issue
Block a user