nomadro/packages/pay/src/index.ts
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
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>
2026-11-04 02:25:05 +08:00

106 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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;