nomadro/packages/pay/src/index.ts
eric 56b29205e7 Integrate four verticals into nomadro portal with shared packages and API gateway.
Mount /vip with nomadvip+payjsapi, add @nomadro/pay|pb|sdk, apps/api, /admin shell, and backup/rollback scripts under git.nomadro.cn workflow.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 20:16:44 -05:00

98 lines
2.3 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) */
isChinaUser?: boolean;
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 =
input.region === "auto" || !input.region
? input.isChinaUser === false
? "overseas"
: "cn"
: input.region;
if (region === "overseas") {
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 = {
payjsapiPublic: "https://api.hackrobot.cn",
payjsapiInternal: "http://127.0.0.1:8007",
zpaySubmit: "https://zpayz.cn/submit.php",
} as const;