From 46cc225fe85a1f598df8ae341364ac681198e88d Mon Sep 17 00:00:00 2001 From: Guoguo Date: Mon, 18 May 2026 05:35:17 -0700 Subject: [PATCH] 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) --- server/src/lib/wxpay.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/server/src/lib/wxpay.js b/server/src/lib/wxpay.js index 1a9779c..9487352 100644 --- a/server/src/lib/wxpay.js +++ b/server/src/lib/wxpay.js @@ -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,18 +118,25 @@ let _platformCertsExpiry = 0 async function fetchPlatformCertificates() { if (_platformCertsExpiry > Date.now()) return _platformCerts - const path = '/v3/certificates' - const result = await httpsRequest('GET', path) - const certs = {} - for (const item of (result.data || [])) { - const resource = item.encrypt_certificate - if (!resource) continue - const certPem = decryptResource(resource) - certs[item.serial_no] = certPem + try { + const path = '/v3/certificates' + const result = await httpsRequest('GET', path) + const certs = {} + for (const item of (result.data || [])) { + const resource = item.encrypt_certificate + if (!resource) continue + const certPem = decryptResource(resource) + certs[item.serial_no] = certPem + } + if (Object.keys(certs).length > 0) { + _platformCerts = certs + _platformCertsExpiry = Date.now() + 12 * 3600 * 1000 + } + } catch (err) { + console.error('[WXPAY] cert refresh failed, keeping old certs:', err.message) + if (Object.keys(_platformCerts).length === 0) throw err } - _platformCerts = certs - _platformCertsExpiry = Date.now() + 12 * 3600 * 1000 - return certs + return _platformCerts } async function verifyNotifySignature(headers, rawBody) {