51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { getPaymentConfig, getJoinPaymentConfig } from "@/config/services.config";
|
|
|
|
export type PayChannel = "wxpay" | "alipay";
|
|
|
|
export function redirectToPay(params: {
|
|
user_id: string;
|
|
return_url: string;
|
|
total_fee?: number;
|
|
type?: string;
|
|
channel: PayChannel;
|
|
device: "pc" | "h5" | "wechat";
|
|
useJoinConfig?: boolean;
|
|
}): void {
|
|
const baseConfig = getPaymentConfig();
|
|
const joinConfig = params.useJoinConfig ? getJoinPaymentConfig() : null;
|
|
const total_fee = params.total_fee ?? joinConfig?.amount ?? baseConfig.defaultAmount;
|
|
const type = params.type ?? joinConfig?.type ?? baseConfig.defaultType;
|
|
|
|
const payForm = document.createElement("form");
|
|
payForm.method = "POST";
|
|
payForm.action = "/api/pay";
|
|
payForm.target = "_self";
|
|
payForm.style.display = "none";
|
|
|
|
const fields: [string, string][] = [
|
|
["user_id", params.user_id],
|
|
["return_url", params.return_url],
|
|
["total_fee", String(total_fee)],
|
|
["type", type],
|
|
["channel", params.channel],
|
|
["device", params.device],
|
|
["html", "1"],
|
|
];
|
|
|
|
fields.forEach(([k, v]) => {
|
|
const input = document.createElement("input");
|
|
input.type = "hidden";
|
|
input.name = k;
|
|
input.value = v;
|
|
payForm.appendChild(input);
|
|
});
|
|
|
|
document.body.appendChild(payForm);
|
|
payForm.submit();
|
|
}
|
|
|
|
export function getDeviceFromEnv(env: "pc" | "h5" | "wechat"): "pc" | "h5" | "wechat" {
|
|
if (env === "wechat") return "wechat";
|
|
return env === "pc" ? "pc" : "h5";
|
|
}
|