提交
This commit is contained in:
@@ -19,26 +19,69 @@ class WechatPayService {
|
||||
// 初始化API v3配置
|
||||
async initializeV3() {
|
||||
try {
|
||||
// 检查配置是否存在
|
||||
if (!this.config.keyPath || !this.config.certPath) {
|
||||
console.warn('微信支付证书路径未配置,跳过API v3初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
// 加载私钥
|
||||
const keyPath = path.resolve(__dirname, '..', this.config.keyPath.replace(/^\.\//, ''));
|
||||
if (fs.existsSync(keyPath)) {
|
||||
const keyPath = this.resolveCertPath(this.config.keyPath);
|
||||
console.log('尝试加载私钥文件:', keyPath);
|
||||
|
||||
if (this.isValidFile(keyPath)) {
|
||||
this.privateKey = fs.readFileSync(keyPath, 'utf8');
|
||||
console.log('API v3 私钥加载成功');
|
||||
} else {
|
||||
console.error('私钥文件不存在:', keyPath);
|
||||
console.error('私钥文件不存在或不是有效文件:', keyPath);
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取证书序列号
|
||||
const certPath = path.resolve(__dirname, '..', this.config.certPath.replace(/^\.\//, ''));
|
||||
if (fs.existsSync(certPath)) {
|
||||
const certPath = this.resolveCertPath(this.config.certPath);
|
||||
console.log('尝试加载证书文件:', certPath);
|
||||
|
||||
if (this.isValidFile(certPath)) {
|
||||
const cert = fs.readFileSync(certPath, 'utf8');
|
||||
this.serialNo = this.getCertificateSerialNumber(cert);
|
||||
console.log('证书序列号:', this.serialNo);
|
||||
} else {
|
||||
console.error('证书文件不存在:', certPath);
|
||||
console.error('证书文件不存在或不是有效文件:', certPath);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('初始化API v3配置失败:', error.message);
|
||||
console.error('错误详情:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 解析证书文件路径
|
||||
resolveCertPath(configPath) {
|
||||
// 如果是绝对路径,直接使用
|
||||
if (path.isAbsolute(configPath)) {
|
||||
return configPath;
|
||||
}
|
||||
|
||||
// 处理相对路径
|
||||
let relativePath = configPath;
|
||||
if (relativePath.startsWith('./')) {
|
||||
relativePath = relativePath.substring(2);
|
||||
}
|
||||
|
||||
return path.resolve(__dirname, '..', relativePath);
|
||||
}
|
||||
|
||||
// 检查是否为有效的文件(不是目录)
|
||||
isValidFile(filePath) {
|
||||
try {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const stats = fs.statSync(filePath);
|
||||
return stats.isFile();
|
||||
} catch (error) {
|
||||
console.error('检查文件状态失败:', error.message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user