Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README. Co-authored-by: Cursor <cursoragent@cursor.com>
106 lines
2.8 KiB
TypeScript
106 lines
2.8 KiB
TypeScript
/**
|
||
* Shared payment routing — providers are swappable adapters.
|
||
* Overseas: creem | nowpayments
|
||
* CN browser: zpay
|
||
* WeChat in-app H5: xpay (xorpay)
|
||
*/
|
||
|
||
export type PayEnv = "pc" | "h5" | "wechat";
|
||
export type PayChannel = "alipay" | "wxpay";
|
||
export type PayProvider =
|
||
| "creem"
|
||
| "nowpayments"
|
||
| "zpay"
|
||
| "xpay"
|
||
| "xorpay";
|
||
|
||
export type PayRegion = "cn" | "overseas" | "auto";
|
||
|
||
export type ResolvePayInput = {
|
||
region?: PayRegion;
|
||
/** Force China user (e.g. from is-china-user / audience module) */
|
||
isChinaUser?: boolean;
|
||
/** Prefer over isChinaUser when set: cn → 国内网关;intl → Creem */
|
||
audience?: "cn" | "intl";
|
||
userAgent?: string;
|
||
};
|
||
|
||
export function isWeChat(ua?: string): boolean {
|
||
const agent =
|
||
ua ??
|
||
(typeof navigator !== "undefined" ? navigator.userAgent : "");
|
||
return /MicroMessenger/i.test(agent);
|
||
}
|
||
|
||
export function isMobile(ua?: string): boolean {
|
||
const agent =
|
||
ua ??
|
||
(typeof navigator !== "undefined" ? navigator.userAgent : "");
|
||
return /AppleWebKit.*Mobile|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
||
agent,
|
||
);
|
||
}
|
||
|
||
export function getPayEnv(ua?: string): PayEnv {
|
||
if (isWeChat(ua)) return "wechat";
|
||
if (isMobile(ua)) return "h5";
|
||
return "pc";
|
||
}
|
||
|
||
export function getRecommendedChannel(_env: PayEnv): PayChannel {
|
||
return "wxpay";
|
||
}
|
||
|
||
export function mustUseWxPay(env: PayEnv): boolean {
|
||
return env === "wechat";
|
||
}
|
||
|
||
/**
|
||
* Pick gateway by region + device. Does not call the network —
|
||
* verticals / payjsapi execute the chosen provider.
|
||
*/
|
||
export function resolvePayProvider(input: ResolvePayInput = {}): {
|
||
env: PayEnv;
|
||
provider: PayProvider;
|
||
channel: PayChannel;
|
||
} {
|
||
const env = getPayEnv(input.userAgent);
|
||
const region: PayRegion =
|
||
input.region && input.region !== "auto"
|
||
? input.region
|
||
: input.audience === "intl" || input.isChinaUser === false
|
||
? "overseas"
|
||
: "cn";
|
||
|
||
if (region === "overseas") {
|
||
// 默认 Creem;后端无 CREEM_API_KEY 时自动回退 nowpayments(加密货币)
|
||
return {
|
||
env,
|
||
provider: "creem",
|
||
channel: getRecommendedChannel(env),
|
||
};
|
||
}
|
||
|
||
// China
|
||
if (env === "wechat") {
|
||
return { env, provider: "xpay", channel: "wxpay" };
|
||
}
|
||
return {
|
||
env,
|
||
provider: "zpay",
|
||
channel: getRecommendedChannel(env),
|
||
};
|
||
}
|
||
|
||
/** Default public endpoints — override via env in apps */
|
||
export const PAY_DEFAULTS = {
|
||
/** .com 美国公网支付 API(/nomadvip/*) */
|
||
payPublic: "https://api.nomadro.com",
|
||
payInternal: "http://127.0.0.1:8011",
|
||
/** @deprecated 旧名,指向美国公网 */
|
||
payjsapiPublic: "https://api.nomadro.com",
|
||
/** @deprecated 旧国内端口,勿再当 .com 默认 */
|
||
payjsapiInternal: "http://127.0.0.1:8011",
|
||
zpaySubmit: "https://zpayz.cn/submit.php",
|
||
} as const;
|