feat: 初始化雄戈商城服务端代码与Webhook自动部署配置

这个提交包含在:
wikiboss
2026-09-12 16:40:24 +08:00
当前提交 e90ec58e61
共有 6438 个文件被更改,包括 1136083 次插入 和 0 次删除

查看文件

@@ -0,0 +1,91 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\HuifuQueryService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class CompensateQuery extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:compensate')
->addOption('order_sn', null, Option::VALUE_OPTIONAL, '补偿单笔订单(商户订单号/req_seq_id)')
->setDescription('Compensate pending Huifu payment orders via official query API');
}
protected function execute(Input $input, Output $output)
{
if (!class_exists('\\addons\\shopro_huifu\\library\\Bridge') || !\addons\shopro_huifu\library\Bridge::isEnabled()) {
$output->writeln('Huifu plugin disabled.');
return;
}
$service = new HuifuQueryService();
$orderSn = trim((string) $input->getOption('order_sn'));
if ($orderSn !== '') {
try {
$result = $service->compensateByOrderSn($orderSn);
$output->writeln(($result['ok'] ? '[OK] ' : '[FAIL] ') . $result['message']);
if (!empty($result['req_seq_id'])) {
$output->writeln('req_seq_id=' . $result['req_seq_id']);
}
} catch (\Throwable $e) {
$output->writeln('[FAIL] ' . $e->getMessage());
}
return;
}
$results = $service->compensatePending();
$output->writeln('Huifu compensate finished. ok=' . $results['ok'] . ' fail=' . $results['fail']);
}
}

查看文件

@@ -0,0 +1,35 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\Installer;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class InstallFull extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:install')
->setAliases(['shopro_huifu:install-full'])
->setDescription('Full install: copy admin files, run SQL, create menus');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('=== Shopro Huifu 一键安装 ===');
try {
Installer::run($output);
$output->writeln('');
$output->writeln('请执行 php think shopro_huifu:verify 复查');
$output->writeln('后台菜单: 系统管理 -> 商城配置 -> 汇付支付配置');
$output->writeln('直达: /admin/shopro_huifu/config');
} catch (\Throwable $e) {
$output->error($e->getMessage());
return 1;
}
return 0;
}
}

查看文件

@@ -0,0 +1,24 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\onboard\HuifuOnboardService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class OnboardPoll extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:onboard-poll')
->setDescription('Poll pending Huifu personal onboard applications');
}
protected function execute(Input $input, Output $output)
{
$service = new HuifuOnboardService();
$count = $service->pollPending(100);
$output->writeln('Polled onboard records: ' . $count);
}
}

查看文件

@@ -0,0 +1,134 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\Bridge;
use addons\shopro_huifu\library\HuifuClient;
use addons\shopro_huifu\library\HuifuConfig;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
class ProbeWechatOnboarding extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:probe-wechat')
->addOption('openid', null, Option::VALUE_OPTIONAL, '真实小程序 sub_openid,用于探针下单')
->setDescription('Probe Huifu WeChat onboarding/config via official query APIs');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('=== 汇付微信入驻/API 诊断 ===');
$config = HuifuConfig::getInstance();
$client = new HuifuClient();
$shopAppId = Bridge::getMiniProgramAppId();
$channelNo = trim((string) $config->get('channel_no'));
$output->writeln('env: ' . $config->get('env', 'prod'));
$output->writeln('sys_id: ' . $config->get('sys_id'));
$output->writeln('product_id: ' . $config->get('product_id'));
$output->writeln('huifu_id: ' . $config->get('huifu_id'));
$output->writeln('upper_huifu_id: ' . ($config->get('upper_huifu_id') ?: '(空)'));
$output->writeln('商城小程序 AppID: ' . ($shopAppId ?: '(空)'));
$output->writeln('pay_scene: ' . (trim((string) $config->get('pay_scene')) ?: '(空)'));
$output->writeln('channel_no: ' . ($channelNo ?: '(空)'));
$output->writeln('');
foreach (HuifuClient::analyzeCredentials($config) as $line) {
$output->writeln($line);
}
$output->writeln('');
$output->writeln('--- 微信配置查询 (V2MerchantBusiConfigQuery) ---');
$cfg = $client->queryWechatConfig($channelNo);
if (!$cfg['ok']) {
$output->writeln('[FAIL] ' . $cfg['message']);
} else {
$data = $cfg['data'] ?? [];
$respCode = (string) ($data['resp_code'] ?? '');
$respDesc = (string) ($data['resp_desc'] ?? '');
$output->writeln('resp_code=' . $respCode . ' ' . $respDesc);
if ($respCode === '00000002' || strpos($respDesc, 'huifuId不存在') !== false || strpos($respDesc, '不存在') !== false) {
$output->writeln('[FAIL] 插件 huifu_id 在汇付 API 侧不存在,或 sys_id/product_id 无数据权限');
$output->writeln(' 请到合作伙伴控台核对「汇付客户号」,与插件 huifu_id 逐字比对');
$output->writeln(' 若为渠道子商户:sys_id=渠道商号、huifu_id=子商户号、私钥用渠道商密钥');
} elseif ($respCode !== '00000000' && $respCode !== '') {
$output->writeln('[FAIL] 微信配置查询未成功');
} else {
$this->printKeys($output, $data, [
'wx_applet_app_id', 'wx_woa_app_id', 'fee_type', 'bank_channel_no',
]);
$hfAppId = (string) ($data['wx_applet_app_id'] ?? '');
if ($hfAppId === '') {
$output->writeln('[FAIL] 汇付侧未查到 wx_applet_app_id,请在控台「微信配置」保存并提交');
} elseif ($shopAppId !== '' && $hfAppId !== $shopAppId) {
$output->writeln("[FAIL] 汇付 AppID({$hfAppId}) ≠ 商城 AppID({$shopAppId})");
} elseif ($shopAppId !== '') {
$output->writeln('[OK] 汇付 wx_applet_app_id 与商城一致');
}
}
}
$output->writeln('');
$output->writeln('--- 微信入驻查询 (V2MerchantBusiMercodeQuery, payWay=W) ---');
foreach (['01', '02'] as $feeType) {
$output->writeln('fee_type=' . $feeType . ':');
$mer = $client->queryWechatMercode($feeType);
if (!$mer['ok']) {
$output->writeln(' [FAIL] ' . $mer['message']);
continue;
}
$data = $mer['data'] ?? [];
$respCode = (string) ($data['resp_code'] ?? '');
$respDesc = (string) ($data['resp_desc'] ?? '');
$output->writeln(' resp_code=' . $respCode . ' ' . $respDesc);
if ($respCode === '00000003' || strpos($respDesc, '未入驻') !== false) {
$output->writeln(' [FAIL] 该 huifu_id 在汇付 API 侧无微信入驻记录(与控台显示不一致时,多为 huifu_id 填错)');
continue;
}
if (!empty($data['mer_code_list']) && is_array($data['mer_code_list'])) {
foreach ($data['mer_code_list'] as $idx => $row) {
if (!is_array($row)) {
continue;
}
$output->writeln(' [' . $idx . '] sub_mer_id=' . ($row['sub_mer_id'] ?? '')
. ' bank_channel_no=' . ($row['bank_channel_no'] ?? '')
. ' business_stat=' . ($row['business_stat'] ?? ''));
}
} else {
$this->printKeys($output, $data, ['sub_mer_id', 'bank_channel_no', 'bank_mer_code', 'business_stat'], ' ');
}
}
$openid = trim((string) $input->getOption('openid'));
if ($openid !== '') {
$output->writeln('');
$output->writeln('--- T_MINIAPP 探针下单 ---');
$probe = $client->probeMiniAppCreate($openid, $shopAppId, '0.01');
if ($probe['ok']) {
$output->writeln('[OK] ' . $probe['message']);
} else {
$output->writeln('[FAIL] ' . $probe['message']);
if (strpos($probe['message'], 'sub_openid') !== false || strpos($probe['message'], 'appid') !== false) {
$output->writeln(' 若错误变为 openid/appid 相关,说明入驻路由已通,请检查用户授权');
}
}
} else {
$output->writeln('');
$output->writeln('可选:php think shopro_huifu:probe-wechat --openid=用户真实openid');
}
}
protected function printKeys(Output $output, array $data, array $keys, $prefix = '')
{
foreach ($keys as $key) {
if (array_key_exists($key, $data) && (string) $data[$key] !== '') {
$output->writeln($prefix . $key . ': ' . $data[$key]);
}
}
}
}

查看文件

@@ -0,0 +1,36 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\Installer;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class RepairMenu extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:repair-menu')
->setDescription('Repair shopro_huifu install (files + SQL + menus)');
}
protected function execute(Input $input, Output $output)
{
$shopro = \think\Db::name('auth_rule')->where('name', 'shopro')->find();
if (!$shopro) {
$output->error('未找到 Shopro 父菜单 (auth_rule.name=shopro),请先安装 Shopro 商城插件');
return;
}
Installer::installMenu($output);
Installer::installSplitMenu($output);
Installer::upgradePayTypeColumn($output);
\app\common\library\Menu::enable('shopro_huifu/config');
\app\common\library\Menu::enable('shopro/split');
Installer::grantRulesToShoproGroups($output);
Installer::clearMenuCache();
Installer::setAddonState('shopro_huifu', 1, $output);
$output->writeln('请退出后台重新登录,然后执行 php think shopro_huifu:verify 复查');
}
}

查看文件

@@ -0,0 +1,40 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro\library\split\Config;
use addons\shopro\model\split\SplitOrder;
use addons\shopro_huifu\library\HuifuSplitService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class SplitRetry extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:split-retry')->setDescription('Retry failed Huifu split confirm orders');
}
protected function execute(Input $input, Output $output)
{
$config = new Config();
if (!$config->isEnable()) {
$output->writeln('Split disabled.');
return;
}
$rows = SplitOrder::where('channel', 'huifu')
->where('status', 4)
->where('retry_count', '<', $config->getRetryMax())
->where('next_retry_time', '<=', time())
->limit(20)
->select();
$service = new HuifuSplitService();
foreach ($rows as $row) {
$service->retry($row['id']);
}
$output->writeln('Huifu split retry finished.');
}
}

查看文件

@@ -0,0 +1,122 @@
<?php
namespace addons\shopro_huifu\command;
use addons\shopro_huifu\library\Bridge;
use addons\shopro_huifu\library\HuifuConfig;
use addons\shopro_huifu\library\SdkLoader;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class TestPayConnection extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:test-pay')
->setDescription('Test Huifu gateway connectivity and SDK init');
}
protected function execute(Input $input, Output $output)
{
$output->writeln('=== 汇付连通性测试 ===');
if (!Bridge::isEnabled()) {
$output->writeln('[FAIL] Bridge::isEnabled() = false');
return;
}
if (!Bridge::isHuifuMiniProgramChannel('wxMiniProgram')) {
$output->writeln('[FAIL] miniprogram_channel 不是 huifu');
return;
}
$config = HuifuConfig::getInstance();
$gateway = $config->getGatewayUrl();
$output->writeln('网关: ' . $gateway);
$start = microtime(true);
$ch = curl_init($gateway);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$errno = curl_errno($ch);
$err = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$elapsed = round((microtime(true) - $start) * 1000);
if ($errno) {
$output->writeln("[FAIL] 无法连接汇付网关 ({$elapsed}ms): [{$errno}] {$err}");
$output->writeln('请检查服务器出网防火墙是否放行 api.huifu.com');
return;
}
$output->writeln("[OK] 汇付网关可达 HTTP {$httpCode} ({$elapsed}ms)");
try {
HuifuConfig::getInstance()->validateKeys();
$output->writeln('[OK] RSA 密钥格式校验通过');
} catch (\Exception $e) {
$output->writeln('[FAIL] RSA 密钥: ' . $e->getMessage());
$output->writeln('请重新粘贴汇付控台的商户私钥、汇付公钥(支持带或不带 PEM 头)');
return;
}
try {
SdkLoader::boot();
$output->writeln('[OK] SDK 初始化成功');
} catch (\Exception $e) {
$output->writeln('[FAIL] SDK 初始化: ' . $e->getMessage());
return;
}
$output->writeln('');
$output->writeln('=== 汇付签名 API 探针(查单) ===');
try {
$probe = (new \addons\shopro_huifu\library\HuifuClient())->probeSignedApi();
if ($probe['ok']) {
$output->writeln('[OK] ' . $probe['message']);
} else {
$output->writeln('[FAIL] ' . $probe['message']);
}
} catch (\Exception $e) {
$output->writeln('[FAIL] ' . $e->getMessage());
}
$appId = Bridge::getMiniProgramAppId();
$env = $config->get('env', 'prod');
$prodMode = defined('PROD_MODE') ? (PROD_MODE ? 'true' : 'false') : 'undefined';
$gateway = $prodMode === 'true' ? 'https://api.huifu.com (生产)' : 'https://spin-test.cloudpnr.com (测试)';
$output->writeln('后台 env: ' . $env);
$output->writeln('SDK PROD_MODE: ' . $prodMode . ' -> ' . $gateway);
$output->writeln('小程序 AppID: ' . ($appId ?: '(空)'));
$output->writeln('商户号 huifu_id: ' . $config->get('huifu_id'));
$output->writeln('sys_id: ' . $config->get('sys_id'));
$output->writeln('product_id: ' . $config->get('product_id'));
$payScene = trim((string) $config->get('pay_scene'));
$channelNo = trim((string) $config->get('channel_no'));
$output->writeln('pay_scene: ' . ($payScene ?: '(空)') . ' (01=标准费率线上,02=标准费率线下)');
$output->writeln('channel_no: ' . ($channelNo ?: '(空)'));
if ($payScene === '02') {
$output->writeln('[WARN] pay_scene=02 表示标准费率线下;若控台是「标准费率线上」应改为 01 或留空');
}
$output->writeln('延时分账 delay_acct: ' . ($config->shouldDelayAcct() ? 'Y' : 'N'));
if ($env === 'test' && $prodMode === 'true') {
$output->writeln('[WARN] 后台选了测试环境,但 SDK 仍走生产网关,请更新 SdkLoader.php');
}
if ($env === 'prod' && $prodMode === 'false') {
$output->writeln('[WARN] 后台选了生产环境,但 SDK 走测试网关,请更新 SdkLoader.php');
}
$output->writeln('');
$output->writeln('若报「汇付未返回 pay_info」,请核对:');
$output->writeln(' 1) env 与汇付商户凭证是否同一环境(测试/生产不能混用)');
$output->writeln(' 2) 小程序 AppID 与 sub_openid 是否同一应用');
$output->writeln(' 3) 汇付控台是否已开通 T_MINIAPP 微信小程序通道');
$output->writeln(' 4) 查 runtime/log 中 [shopro_huifu] missing-pay_info 日志');
}
}

查看文件

@@ -0,0 +1,396 @@
<?php
namespace addons\shopro_huifu\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
class VerifyInstall extends Command
{
protected function configure()
{
$this->setName('shopro_huifu:verify')
->setDescription('Check shopro_huifu plugin installation (tables, menus, files)');
}
protected function execute(Input $input, Output $output)
{
$prefix = config('database.prefix');
$root = ROOT_PATH;
$pass = 0;
$fail = 0;
$checks = [
'入口文件 Shopro_huifu.php' => is_file($root . 'addons/shopro_huifu/Shopro_huifu.php'),
'info.ini' => is_file($root . 'addons/shopro_huifu/info.ini'),
'config/menu.php' => is_file($root . 'addons/shopro_huifu/config/menu.php'),
'后台控制器(已部署)' => is_file($root . 'application/admin/controller/shopro_huifu/Config.php'),
'后台控制器(插件包内)' => is_file($root . 'addons/shopro_huifu/application/admin/controller/shopro_huifu/Config.php'),
'后台视图(已部署)' => is_file($root . 'application/admin/view/shopro_huifu/config/index.html'),
'后台 JS(已部署)' => is_file($root . 'public/assets/js/backend/shopro_huifu/config.js'),
'汇付 SDK init.php' => $this->hasSdk($root),
];
$output->writeln('=== 文件检查 ===');
foreach ($checks as $label => $ok) {
$this->line($output, $label, $ok, $pass, $fail);
}
$output->writeln('');
$output->writeln('=== 数据库检查 ===');
$tables = ['shopro_huifu_config', 'shopro_huifu_pay_ext', 'shopro_huifu_notify_log'];
foreach ($tables as $table) {
$ok = $this->tableExists($prefix . $table);
$this->line($output, '表 ' . $prefix . $table, $ok, $pass, $fail);
}
try {
$cfgCount = Db::name('shopro_huifu_config')->count();
$this->line($output, '汇付配置项数量 >= 10', $cfgCount >= 10, $pass, $fail, (string) $cfgCount);
} catch (\Exception $e) {
$this->line($output, '汇付配置项', false, $pass, $fail, $e->getMessage());
}
try {
$orderTable = $prefix . 'shopro_order';
$rows = Db::query("SHOW COLUMNS FROM `{$orderTable}` LIKE 'pay_type'");
$type = $rows[0]['Type'] ?? '';
$supportsHuifu = stripos($type, 'huifu_wechat') !== false;
$this->line(
$output,
'shopro_order.pay_type 支持 huifu_wechat',
$supportsHuifu,
$pass,
$fail,
$type ?: '未找到 pay_type 字段'
);
} catch (\Exception $e) {
$this->line($output, 'shopro_order.pay_type 支持 huifu_wechat', false, $pass, $fail, $e->getMessage());
}
$output->writeln('');
$output->writeln('=== 菜单与权限检查 ===');
$menuNames = [
'shopro_huifu/config',
'shopro_huifu/config/index',
'shopro_huifu/config/save',
'shopro/split',
'shopro/split/config',
'shopro/split/config/index',
'shopro/split/receiver',
'shopro/split/rule',
'shopro/split/order',
];
foreach ($menuNames as $name) {
$row = Db::name('auth_rule')->where('name', $name)->find();
$ok = (bool) $row;
$extra = $row ? ('pid=' . $row['pid'] . ' status=' . $row['status'] . ' ismenu=' . $row['ismenu']) : '未找到';
$this->line($output, '菜单 ' . $name, $ok, $pass, $fail, $extra);
}
$legacyMenu = Db::name('auth_rule')->where('name', 'shopro_huifu')->find();
$this->line($output, '旧版菜单 shopro_huifu 已移除', !$legacyMenu, $pass, $fail, $legacyMenu ? '仍存在,需 repair' : '无');
foreach ([
'shopro_huifu/config' => '汇付菜单父节点',
'shopro/split' => '分账菜单父节点',
] as $menuName => $label) {
$row = Db::name('auth_rule')->where('name', $menuName)->find();
if ($row) {
$parent = Db::name('auth_rule')->where('id', $row['pid'])->find();
$parentName = $parent['name'] ?? 'unknown';
$ok = in_array($parentName, ['shopro/config', 'shopro'], true);
$this->line($output, $label . '正确', $ok, $pass, $fail, 'parent=' . $parentName);
} else {
$this->line($output, $label . '正确', false, $pass, $fail, '缺少 ' . $menuName);
}
}
$splitConfig = Db::name('auth_rule')->where('name', 'shopro/split/config')->find();
if ($splitConfig && intval($splitConfig['ismenu']) === 1 && $splitConfig['status'] === 'normal') {
$path = $this->getMenuPathTitles(intval($splitConfig['id']));
$this->line($output, '分账设置侧栏路径', !empty($path), $pass, $fail, implode(' -> ', $path));
} else {
$this->line($output, '分账设置侧栏路径', false, $pass, $fail, '缺少 shopro/split/config');
}
$huifuConfig = Db::name('auth_rule')->where('name', 'shopro_huifu/config')->find();
if ($huifuConfig && intval($huifuConfig['ismenu']) === 1 && $huifuConfig['status'] === 'normal') {
$path = $this->getMenuPathTitles(intval($huifuConfig['id']));
$this->line($output, '汇付配置侧栏路径', !empty($path), $pass, $fail, implode(' -> ', $path));
}
$indexRule = Db::name('auth_rule')->where('name', 'shopro_huifu/config/index')->find();
$superGroup = Db::name('auth_group')->where('rules', '*')->find();
if ($superGroup) {
$this->line($output, '存在超级管理员权限组(*)', true, $pass, $fail, 'id=' . $superGroup['id']);
} else {
$this->line($output, '存在超级管理员权限组(*)', false, $pass, $fail);
}
if ($indexRule) {
$groupsWithHuifu = 0;
foreach (Db::name('auth_group')->where('status', 'normal')->select() as $group) {
if ($group['rules'] === '*') {
$groupsWithHuifu++;
continue;
}
$owned = array_filter(array_map('trim', explode(',', (string) $group['rules'])));
if (in_array((string) $indexRule['id'], $owned, true)) {
$groupsWithHuifu++;
}
}
$this->line($output, '至少1个管理组拥有汇付权限', $groupsWithHuifu > 0, $pass, $fail, (string) $groupsWithHuifu);
}
$iniFile = ADDON_PATH . 'shopro_huifu/info.ini';
$iniState = 0;
if (is_file($iniFile)) {
$ini = parse_ini_file($iniFile);
$iniState = intval($ini['state'] ?? 0);
}
$cachedInfo = get_addon_info('shopro_huifu');
$cachedState = intval($cachedInfo['state'] ?? 0);
$this->line(
$output,
'插件 info.ini state=1',
$iniState === 1,
$pass,
$fail,
'info.ini state=' . $iniState
);
if ($iniState === 1 && $cachedState !== 1) {
$this->line(
$output,
'get_addon_info 缓存与 info.ini 一致',
false,
$pass,
$fail,
'缓存 state=' . $cachedState . ',需刷新插件缓存'
);
} else {
$this->line(
$output,
'get_addon_info 缓存与 info.ini 一致',
true,
$pass,
$fail,
'缓存 state=' . $cachedState
);
}
$addonsFile = ROOT_PATH . 'application/extra/addons.php';
$appInitHooks = [];
if (is_file($addonsFile)) {
$addonsConfig = include $addonsFile;
$appInitHooks = (array) ($addonsConfig['hooks']['app_init'] ?? []);
}
$inAppInit = in_array('shopro_huifu', $appInitHooks, true);
$this->line(
$output,
'addons.php app_init 含 shopro_huifu',
$iniState !== 1 || $inAppInit,
$pass,
$fail,
$inAppInit ? '已注册' : '未注册(线上 Hook 可能不加载,需刷新插件缓存)'
);
$output->writeln('');
$output->writeln('=== 支付通道路由检查(小程序 prepay 是否走汇付) ===');
$this->line(
$output,
'说明:收银台仍显示「微信支付」属正常,由后端路由决定实际通道',
true,
$pass,
$fail
);
try {
$cfg = Db::name('shopro_huifu_config')->column('value', 'name');
$channel = $cfg['miniprogram_channel'] ?? 'yansongda';
$hfStatus = $cfg['status'] ?? '0';
$this->line(
$output,
'miniprogram_channel=huifu',
$channel === 'huifu',
$pass,
$fail,
'当前=' . $channel
);
$this->line(
$output,
'汇付通道开关 status=1',
$hfStatus === '1',
$pass,
$fail,
'当前=' . $hfStatus
);
if ($channel === 'huifu') {
$credOk = !empty($cfg['sys_id']) && !empty($cfg['product_id']) && !empty($cfg['huifu_id']);
$this->line(
$output,
'汇付凭证 sys_id/product_id/huifu_id 已填',
$credOk,
$pass,
$fail,
$credOk ? '已填' : '缺商户号/系统号/产品号'
);
$keyOk = !empty($cfg['merchant_private_key']) && !empty($cfg['huifu_public_key']);
$this->line(
$output,
'RSA 密钥对已填',
$keyOk,
$pass,
$fail,
$keyOk ? '已填' : '缺私钥或汇付公钥'
);
if ($keyOk && class_exists('\\addons\\shopro_huifu\\library\\HuifuConfig')) {
try {
\addons\shopro_huifu\library\HuifuConfig::reload()->validateKeys();
$this->line($output, 'RSA 密钥格式可签名', true, $pass, $fail);
} catch (\Exception $e) {
$this->line($output, 'RSA 密钥格式可签名', false, $pass, $fail, $e->getMessage());
}
}
$payScene = trim((string) ($cfg['pay_scene'] ?? ''));
$channelNo = trim((string) ($cfg['channel_no'] ?? ''));
$scenePairOk = ($channelNo === '') || ($payScene !== '' && $channelNo !== '');
$this->line(
$output,
'pay_scene 与 channel_no 成对配置',
$scenePairOk,
$pass,
$fail,
$scenePairOk ? 'OK' : '填了 channel_no 时必须同时填 pay_scene'
);
}
$mini = Db::name('shopro_config')->where('name', 'wxMiniProgram')->value('value');
$mini = json_decode($mini, true);
$appId = $mini['app_id'] ?? '';
$this->line(
$output,
'商城小程序 AppID 已配置',
$appId !== '',
$pass,
$fail,
$appId !== '' ? $appId : 'shopro_config.wxMiniProgram 为空'
);
} catch (\Exception $e) {
$this->line($output, '支付通道配置可读', false, $pass, $fail, $e->getMessage());
}
if (class_exists('\\addons\\shopro_huifu\\library\\Bridge')) {
$bridgeEnabled = \addons\shopro_huifu\library\Bridge::isEnabled();
$this->line(
$output,
'Bridge::isEnabled()',
$bridgeEnabled,
$pass,
$fail,
$bridgeEnabled ? 'true' : 'false(插件未启用或 status=0)'
);
$huifuChannel = \addons\shopro_huifu\library\Bridge::isHuifuMiniProgramChannel('wxMiniProgram');
$this->line(
$output,
'Bridge::isHuifuMiniProgramChannel(wxMiniProgram)',
$huifuChannel,
$pass,
$fail,
$huifuChannel ? 'true,prepay 应走汇付' : 'false,prepay 仍走 Yansongda'
);
$shouldPay = \addons\shopro_huifu\library\Bridge::shouldHandlePay([
'platform' => 'wxMiniProgram',
'payment' => 'wechat',
'prepay_type' => 'order',
]);
$this->line(
$output,
'Bridge::shouldHandlePay(小程序订单)',
$shouldPay,
$pass,
$fail,
$shouldPay ? 'true' : 'false'
);
}
$hookBehaviors = \think\Hook::get('shopro_pay_create');
$hookOk = !empty($hookBehaviors);
$this->line(
$output,
'Hook shopro_pay_create 已注册',
$hookOk,
$pass,
$fail,
$hookOk ? json_encode($hookBehaviors, JSON_UNESCAPED_UNICODE) : '未注册(插件 state=0 或未加载 appInit)'
);
$output->writeln('');
$output->writeln("结果: 通过 {$pass} / 失败 {$fail}");
$output->writeln('汇付配置直达: /admin/shopro_huifu/config');
$output->writeln('分账设置直达: /admin/shopro/split/config');
if ($fail > 0) {
$output->writeln('修复菜单/权限/缓存: php think shopro_huifu:repair-menu');
$output->writeln('修复后请退出后台重新登录');
}
}
protected function getMenuPathTitles($menuId)
{
$path = [];
$currentId = $menuId;
$guard = 0;
while ($currentId && $guard < 20) {
$row = Db::name('auth_rule')->where('id', $currentId)->find();
if (!$row) {
break;
}
if (intval($row['ismenu']) === 1 && $row['status'] === 'normal') {
array_unshift($path, $row['title']);
}
$currentId = intval($row['pid']);
$guard++;
}
return $path;
}
protected function line(Output $output, $label, $ok, &$pass, &$fail, $extra = '')
{
if ($ok) {
$pass++;
$output->writeln('[OK] ' . $label . ($extra ? ' (' . $extra . ')' : ''));
} else {
$fail++;
$output->writeln('[FAIL] ' . $label . ($extra ? ' (' . $extra . ')' : ''));
}
}
protected function tableExists($table)
{
try {
$rows = Db::query("SHOW TABLES LIKE '{$table}'");
return !empty($rows);
} catch (\Exception $e) {
return false;
}
}
protected function hasSdk($root)
{
$paths = [
$root . 'addons/shopro_huifu/library/sdk/extracted/huifurepo-bspay-php-sdk-45b895b/BsPaySdk/init.php',
$root . 'vendor/huifurepo/dg-php-sdk/BsPaySdk/init.php',
];
foreach ($paths as $p) {
if (is_file($p)) {
return true;
}
}
return false;
}
}