refactor(pay): 将微信支付配置统一收敛至epay插件管理
- epay插件配置:在config.php/config.html中增加微信支付公钥ID、APIv3密钥、回调根域名、转账场景ID等完整字段 - WxPayV3Service:读取微信支付配置时优先从epay插件读取,保留site配置兜底降级能力 - WxLogin与Wxpay:更新配置读取与错误提示说明 - site.php与SQL:系统配置聚焦维护用户端与师傅端小程序AppID与Secret,避免配置分散冲突
这个提交包含在:
@@ -38,7 +38,7 @@ class WxLogin
|
||||
$secret = $cfg['secret'];
|
||||
if ($appid === '' || $secret === '') {
|
||||
$label = $side === 'master' ? '师傅端' : '用户端';
|
||||
return ['error' => "{$label}微信登录参数未配置完整(请在后台「常规管理 -> 系统配置 -> 小程序与支付配置」填写 AppID 与 Secret)", 'errcode' => -1001];
|
||||
return ['error' => "{$label}微信登录参数未配置完整(请在后台「常规管理 -> 系统配置 -> 小程序配置」填写 AppID 与 Secret)", 'errcode' => -1001];
|
||||
}
|
||||
$code = trim((string)$code);
|
||||
if ($code === '') {
|
||||
|
||||
@@ -50,27 +50,79 @@ class WxPayV3Service
|
||||
|
||||
public function __construct(array $overrideConfig = [])
|
||||
{
|
||||
$epay = $this->loadEpayWechatConfig();
|
||||
$side = isset($overrideConfig['side']) && $overrideConfig['side'] === 'master' ? 'master' : 'user';
|
||||
|
||||
if (isset($overrideConfig['appid'])) {
|
||||
$this->appId = trim((string)$overrideConfig['appid']);
|
||||
} else {
|
||||
$miniCfg = WxLogin::getMiniAppConfig($side);
|
||||
$this->appId = $miniCfg['appid'];
|
||||
$epayAppId = $side === 'master'
|
||||
? trim((string)($epay['master_miniapp_id'] ?? ''))
|
||||
: trim((string)($epay['miniapp_id'] ?? ''));
|
||||
if ($epayAppId !== '') {
|
||||
$this->appId = $epayAppId;
|
||||
} else {
|
||||
$miniCfg = WxLogin::getMiniAppConfig($side);
|
||||
$this->appId = $miniCfg['appid'];
|
||||
}
|
||||
}
|
||||
$this->mchId = trim((string)($overrideConfig['mch_id'] ?? Config::get('site.wechat_mch_id')));
|
||||
$this->keyV3 = trim((string)($overrideConfig['key_v3'] ?? Config::get('site.wechat_key_v3')));
|
||||
$this->publicKeyId = trim((string)($overrideConfig['public_key_id'] ?? Config::get('site.wechat_public_key_id')));
|
||||
|
||||
$certRel = trim((string)($overrideConfig['cert_client'] ?? '/addons/epay/certs/apiclient_cert.pem'));
|
||||
$keyRel = trim((string)($overrideConfig['cert_key'] ?? '/addons/epay/certs/apiclient_key.pem'));
|
||||
$pubRel = trim((string)($overrideConfig['public_key'] ?? '/addons/epay/certs/public_key.pem'));
|
||||
$this->mchId = $this->pickConfig($overrideConfig, 'mch_id', $epay, 'mch_id', 'site.wechat_mch_id');
|
||||
$this->keyV3 = $this->pickConfig($overrideConfig, 'key_v3', $epay, 'key_v3', 'site.wechat_key_v3');
|
||||
$this->publicKeyId = $this->pickConfig($overrideConfig, 'public_key_id', $epay, 'public_key_id', 'site.wechat_public_key_id');
|
||||
|
||||
$certRel = trim((string)($overrideConfig['cert_client'] ?? (!empty($epay['cert_client']) ? $epay['cert_client'] : '/addons/epay/certs/apiclient_cert.pem')));
|
||||
$keyRel = trim((string)($overrideConfig['cert_key'] ?? (!empty($epay['cert_key']) ? $epay['cert_key'] : '/addons/epay/certs/apiclient_key.pem')));
|
||||
$pubRel = trim((string)($overrideConfig['public_key'] ?? (!empty($epay['public_key']) ? $epay['public_key'] : '/addons/epay/certs/public_key.pem')));
|
||||
|
||||
$this->certClientPath = $this->resolveCertPath($certRel);
|
||||
$this->certKeyPath = $this->resolveCertPath($keyRel);
|
||||
$this->publicKeyPath = $this->resolveCertPath($pubRel);
|
||||
|
||||
$this->notifyDomain = rtrim(trim((string)($overrideConfig['notify_domain'] ?? Config::get('site.wechat_notify_domain'))), '/');
|
||||
$this->transferSceneId = trim((string)($overrideConfig['transfer_scene_id'] ?? (Config::get('site.wechat_transfer_scene_id') ?: '1005')));
|
||||
$this->notifyDomain = rtrim($this->pickConfig($overrideConfig, 'notify_domain', $epay, 'notify_domain', 'site.wechat_notify_domain'), '/');
|
||||
$sceneId = $this->pickConfig($overrideConfig, 'transfer_scene_id', $epay, 'transfer_scene_id', 'site.wechat_transfer_scene_id');
|
||||
$this->transferSceneId = $sceneId !== '' ? $sceneId : '1005';
|
||||
}
|
||||
|
||||
/**
|
||||
* 优先从 epay 插件配置读取微信支付参数
|
||||
*/
|
||||
protected function loadEpayWechatConfig()
|
||||
{
|
||||
if (function_exists('get_addon_config')) {
|
||||
$addonCfg = get_addon_config('epay');
|
||||
if (is_array($addonCfg) && !empty($addonCfg['wechat']) && is_array($addonCfg['wechat'])) {
|
||||
return $addonCfg['wechat'];
|
||||
}
|
||||
}
|
||||
$root = defined('ROOT_PATH') ? rtrim(ROOT_PATH, '/\\') : dirname(__DIR__, 3);
|
||||
$configFile = $root . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . 'epay' . DIRECTORY_SEPARATOR . 'config.php';
|
||||
if (is_file($configFile)) {
|
||||
$raw = include $configFile;
|
||||
if (is_array($raw)) {
|
||||
foreach ($raw as $item) {
|
||||
if (is_array($item) && ($item['name'] ?? '') === 'wechat' && isset($item['value']) && is_array($item['value'])) {
|
||||
return $item['value'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 按「调用方覆盖 > epay插件配置 > site系统配置兜底」优先级取值
|
||||
*/
|
||||
protected function pickConfig(array $overrideConfig, $overrideKey, array $epay, $epayKey, $siteKey)
|
||||
{
|
||||
if (isset($overrideConfig[$overrideKey])) {
|
||||
return trim((string)$overrideConfig[$overrideKey]);
|
||||
}
|
||||
$epayVal = trim((string)($epay[$epayKey] ?? ''));
|
||||
if ($epayVal !== '') {
|
||||
return $epayVal;
|
||||
}
|
||||
return trim((string)Config::get($siteKey));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,7 +158,7 @@ class WxPayV3Service
|
||||
$domain = trim((string)$domain);
|
||||
if ($domain === '') {
|
||||
throw new WxPayException(
|
||||
'微信支付公网回调域名(wechat.notify_domain)未配置,请在 server/.env 的 [wechat] 段补充正确的 HTTPS 公网域名',
|
||||
'微信支付公网回调域名(notify_domain)未配置,请在后台「插件管理 -> 微信支付宝整合 -> 配置」补充正确的 HTTPS 公网域名',
|
||||
'CONFIG_NOTIFY_DOMAIN_EMPTY'
|
||||
);
|
||||
}
|
||||
@@ -463,7 +515,7 @@ class WxPayV3Service
|
||||
public function decryptResource(array $resource)
|
||||
{
|
||||
if (strlen($this->keyV3) !== 32) {
|
||||
throw new WxPayException('微信 APIv3 密钥长度必须为 32 位,请检查 server/.env 的 [wechat] key_v3', 'CONFIG_KEY_V3_INVALID', 500);
|
||||
throw new WxPayException('微信 APIv3 密钥长度必须为 32 位,请检查后台「插件管理 -> 微信支付宝整合 -> 配置」中的商户APIv3密钥', 'CONFIG_KEY_V3_INVALID', 500);
|
||||
}
|
||||
$ciphertextB64 = (string)($resource['ciphertext'] ?? '');
|
||||
$nonce = (string)($resource['nonce'] ?? '');
|
||||
@@ -788,7 +840,7 @@ class WxPayV3Service
|
||||
}
|
||||
if (!empty($missing)) {
|
||||
throw new WxPayException(
|
||||
'微信支付配置缺失或不完整:' . implode('、', $missing) . ',请检查 server/.env 的 [wechat] 配置段',
|
||||
'微信支付配置缺失或不完整:' . implode('、', $missing) . ',请检查后台「插件管理 -> 微信支付宝整合 -> 配置」',
|
||||
'CONFIG_INCOMPLETE'
|
||||
);
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户