Files
soul-yongping/next-project/lib/payment/config.ts
2026-02-09 14:43:35 +08:00

127 lines
4.3 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.

/**
* 支付配置管理 (Payment Configuration)
* 从环境变量读取支付配置
*
* 作者: 卡若
* 版本: v4.0
*/
import { AlipayConfig } from './alipay';
import { WechatPayConfig } from './wechat';
// 应用基础配置
export interface AppConfig {
env: 'development' | 'production';
name: string;
url: string;
currency: 'CNY' | 'USD' | 'EUR';
}
// 完整支付配置
export interface PaymentConfig {
app: AppConfig;
alipay: AlipayConfig;
wechat: WechatPayConfig;
paypal: {
enabled: boolean;
mode: 'sandbox' | 'production';
clientId: string;
clientSecret: string;
};
stripe: {
enabled: boolean;
mode: 'test' | 'production';
publicKey: string;
secretKey: string;
webhookSecret: string;
};
usdt: {
enabled: boolean;
gatewayType: string;
apiKey: string;
ipnSecret: string;
};
order: {
expireMinutes: number;
tradeSnPrefix: string;
};
}
/**
* 获取支付配置
*/
export function getPaymentConfig(): PaymentConfig {
return {
app: {
env: (process.env.NODE_ENV || 'development') as 'development' | 'production',
name: process.env.APP_NAME || 'Soul创业实验',
url: process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000',
currency: (process.env.APP_CURRENCY || 'CNY') as 'CNY',
},
alipay: {
enabled: process.env.ALIPAY_ENABLED === 'true' || true, // 默认启用
mode: (process.env.ALIPAY_MODE || 'production') as 'production',
// 支付宝新版接口需要 app_id如果没有配置则使用 pid旧版兼容
appId: process.env.ALIPAY_APP_ID || process.env.ALIPAY_PID || '2088511801157159',
pid: process.env.ALIPAY_PID || '2088511801157159',
sellerEmail: process.env.ALIPAY_SELLER_EMAIL || 'zhengzhiqun@vip.qq.com',
privateKey: process.env.ALIPAY_PRIVATE_KEY || '',
publicKey: process.env.ALIPAY_PUBLIC_KEY || '',
md5Key: process.env.ALIPAY_MD5_KEY || 'lz6ey1h3kl9zqkgtjz3avb5gk37wzbrp',
},
wechat: {
enabled: process.env.WECHAT_ENABLED === 'true' || true, // 默认启用
mode: (process.env.WECHAT_MODE || 'production') as 'production',
// 微信支付需要使用绑定了支付功能的服务号AppID
appId: process.env.WECHAT_APPID || 'wx7c0dbf34ddba300d', // 服务号AppID已绑定商户号
appSecret: process.env.WECHAT_APP_SECRET || 'f865ef18c43dfea6cbe3b1f1aebdb82e',
serviceAppId: process.env.WECHAT_SERVICE_APPID || 'wx7c0dbf34ddba300d',
serviceSecret: process.env.WECHAT_SERVICE_SECRET || 'f865ef18c43dfea6cbe3b1f1aebdb82e',
mchId: process.env.WECHAT_MCH_ID || '1318592501',
mchKey: process.env.WECHAT_MCH_KEY || 'wx3e31b068be59ddc131b068be59ddc2',
certPath: process.env.WECHAT_CERT_PATH || '',
keyPath: process.env.WECHAT_KEY_PATH || '',
},
paypal: {
enabled: process.env.PAYPAL_ENABLED === 'true',
mode: (process.env.PAYPAL_MODE || 'sandbox') as 'sandbox',
clientId: process.env.PAYPAL_CLIENT_ID || '',
clientSecret: process.env.PAYPAL_CLIENT_SECRET || '',
},
stripe: {
enabled: process.env.STRIPE_ENABLED === 'true',
mode: (process.env.STRIPE_MODE || 'test') as 'test',
publicKey: process.env.STRIPE_PUBLIC_KEY || '',
secretKey: process.env.STRIPE_SECRET_KEY || '',
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET || '',
},
usdt: {
enabled: process.env.USDT_ENABLED === 'true',
gatewayType: process.env.USDT_GATEWAY_TYPE || 'nowpayments',
apiKey: process.env.NOWPAYMENTS_API_KEY || '',
ipnSecret: process.env.NOWPAYMENTS_IPN_SECRET || '',
},
order: {
expireMinutes: parseInt(process.env.ORDER_EXPIRE_MINUTES || '30', 10),
tradeSnPrefix: process.env.TRADE_SN_PREFIX || 'T',
},
};
}
/**
* 获取回调通知URL
*/
export function getNotifyUrl(gateway: 'alipay' | 'wechat' | 'paypal' | 'stripe'): string {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000';
return `${baseUrl}/api/payment/${gateway}/notify`;
}
/**
* 获取支付成功返回URL
*/
export function getReturnUrl(orderId?: string): string {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || 'http://localhost:3000';
const url = `${baseUrl}/payment/success`;
return orderId ? `${url}?orderId=${orderId}` : url;
}