文件
xionggeshop/addons/shopro_huifu/command/ProbeWechatOnboarding.php

135 行
6.4 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?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]);
}
}
}
}