fix: add HTTP timeout and cert cache error recovery in wxpay
- httpsRequest: 15s timeout, prevents hanging on WeChat API outage - fetchPlatformCertificates: keeps old certs if refresh fails, only throws if no certs at all (first time failure)
这个提交包含在:
+9
-1
@@ -80,6 +80,7 @@ function httpsRequest(method, path, body) {
|
||||
}
|
||||
})
|
||||
})
|
||||
req.setTimeout(15000, () => { req.destroy(new Error('wxpay request timeout (15s)')) })
|
||||
req.on('error', reject)
|
||||
if (bodyStr) req.write(bodyStr)
|
||||
req.end()
|
||||
@@ -117,6 +118,7 @@ let _platformCertsExpiry = 0
|
||||
|
||||
async function fetchPlatformCertificates() {
|
||||
if (_platformCertsExpiry > Date.now()) return _platformCerts
|
||||
try {
|
||||
const path = '/v3/certificates'
|
||||
const result = await httpsRequest('GET', path)
|
||||
const certs = {}
|
||||
@@ -126,9 +128,15 @@ async function fetchPlatformCertificates() {
|
||||
const certPem = decryptResource(resource)
|
||||
certs[item.serial_no] = certPem
|
||||
}
|
||||
if (Object.keys(certs).length > 0) {
|
||||
_platformCerts = certs
|
||||
_platformCertsExpiry = Date.now() + 12 * 3600 * 1000
|
||||
return certs
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[WXPAY] cert refresh failed, keeping old certs:', err.message)
|
||||
if (Object.keys(_platformCerts).length === 0) throw err
|
||||
}
|
||||
return _platformCerts
|
||||
}
|
||||
|
||||
async function verifyNotifySignature(headers, rawBody) {
|
||||
|
||||
在新工单中引用
屏蔽一个用户