76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
export const DULUPAY_CONFIG_KEY = "payment_dulupay_config";
|
|
|
|
export type DulupayConfig = {
|
|
enabled: boolean;
|
|
version: "v1" | "v2";
|
|
baseUrl: string;
|
|
notifyUrl: string;
|
|
returnUrl: string;
|
|
// V1
|
|
pid: string;
|
|
key: string;
|
|
defaultType: string;
|
|
// V2
|
|
mchId: string;
|
|
appId: string;
|
|
merchantPrivateKey: string;
|
|
platformPublicKey: string;
|
|
};
|
|
|
|
export const defaultDulupayConfig: DulupayConfig = {
|
|
enabled: false,
|
|
version: "v1",
|
|
baseUrl: "https://www.dulupay.com",
|
|
notifyUrl: "",
|
|
returnUrl: "",
|
|
pid: "",
|
|
key: "",
|
|
defaultType: "alipay",
|
|
mchId: "",
|
|
appId: "",
|
|
merchantPrivateKey: "",
|
|
platformPublicKey: "",
|
|
};
|
|
|
|
function sanitizeText(value: unknown, maxLength: number) {
|
|
return String(value || "").trim().slice(0, maxLength);
|
|
}
|
|
|
|
export function normalizeDulupayConfig(value: unknown): DulupayConfig {
|
|
if (!value || typeof value !== "object") {
|
|
return defaultDulupayConfig;
|
|
}
|
|
|
|
const raw = value as Partial<DulupayConfig>;
|
|
const version = raw.version === "v2" ? "v2" : "v1";
|
|
|
|
return {
|
|
enabled: raw.enabled === true,
|
|
version,
|
|
baseUrl:
|
|
sanitizeText(raw.baseUrl, 255) || defaultDulupayConfig.baseUrl,
|
|
notifyUrl: sanitizeText(raw.notifyUrl, 500),
|
|
returnUrl: sanitizeText(raw.returnUrl, 500),
|
|
pid: sanitizeText(raw.pid, 128),
|
|
key: sanitizeText(raw.key, 500),
|
|
defaultType: sanitizeText(raw.defaultType, 64) || "alipay",
|
|
mchId: sanitizeText(raw.mchId, 128),
|
|
appId: sanitizeText(raw.appId, 128),
|
|
merchantPrivateKey: sanitizeText(raw.merchantPrivateKey, 6000),
|
|
platformPublicKey: sanitizeText(raw.platformPublicKey, 6000),
|
|
};
|
|
}
|
|
|
|
export function normalizeDulupayForPublic(value: unknown): DulupayConfig {
|
|
const config = normalizeDulupayConfig(value);
|
|
return {
|
|
...config,
|
|
pid: "",
|
|
key: "",
|
|
mchId: "",
|
|
appId: "",
|
|
merchantPrivateKey: "",
|
|
platformPublicKey: "",
|
|
};
|
|
}
|