feat(split): 优化汇付分账服务逻辑并增强重试功能 - 修改HuifuSplitService中分账状态查询逻辑,从检查状态[1,2]改为仅检查状态2 - 更新删除条件,从删除状态4改为删除状态[1,4] - 在分账明细为空时添加详细的错误提示信息 - 新增retryByOrderId方法支持订单ID重试 - 优化后台控制器中的重试逻辑,添加分账记录存在性验证 - 改进错误提示信息,提供更准确的失败原因说明 ```
98 行
3.6 KiB
PHP
98 行
3.6 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\shopro\split;
|
||
|
||
use app\admin\controller\shopro\Base;
|
||
use addons\shopro\library\split\SplitExecutor;
|
||
use addons\shopro\library\split\ProfitSharingService;
|
||
|
||
class Order extends Base
|
||
{
|
||
protected $model = null;
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new \addons\shopro\model\split\SplitOrder;
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
$this->request->filter(['strip_tags']);
|
||
if ($this->request->isAjax()) {
|
||
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams();
|
||
$total = $this->model->where($where)->count();
|
||
$list = $this->model->with('receivers')->where($where)->order($sort, $order)->limit($offset, $limit)->select();
|
||
return $this->success('操作成功', null, ['total' => $total, 'rows' => $list]);
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
|
||
public function retry($ids = null)
|
||
{
|
||
$id = $ids ?: $this->request->post('id');
|
||
$row = $this->model->get($id);
|
||
if (!$row) {
|
||
$this->error('分账记录不存在,可能已被上次重试替换。请刷新列表后,对最新一条失败记录重试');
|
||
}
|
||
$orderId = (int) $row['order_id'];
|
||
$channel = $row['channel'] ?? 'wechat';
|
||
$result = (new SplitExecutor())->retry($id);
|
||
if ($result) {
|
||
$this->success('重试成功');
|
||
}
|
||
$hint = $this->formatSplitWxResultHint($orderId, $channel);
|
||
if ($hint === '') {
|
||
$hint = ':请确认已部署最新 HuifuSplitService.php;或执行 php addons/shopro/tests/split_commission_verify.php try-split ' . $row['order_sn'] . ' --execute 查看详情';
|
||
}
|
||
$this->error('重试失败' . $hint);
|
||
}
|
||
|
||
protected function formatSplitWxResultHint($orderId, $channel)
|
||
{
|
||
$latest = $this->model->where('order_id', $orderId)
|
||
->where('channel', $channel)
|
||
->order('id', 'desc')
|
||
->find();
|
||
if (!$latest || empty($latest['wx_result'])) {
|
||
return '';
|
||
}
|
||
$raw = is_string($latest['wx_result']) ? $latest['wx_result'] : json_encode($latest['wx_result'], JSON_UNESCAPED_UNICODE);
|
||
$decoded = json_decode($raw, true);
|
||
if (is_array($decoded)) {
|
||
$parts = array_filter([
|
||
$decoded['resp_desc'] ?? '',
|
||
$decoded['resp_code'] ?? '',
|
||
$decoded['bank_message'] ?? '',
|
||
]);
|
||
return $parts ? (':' . implode(' ', $parts)) : (':' . mb_substr($raw, 0, 300));
|
||
}
|
||
return ':' . mb_substr($raw, 0, 300);
|
||
}
|
||
|
||
public function query($ids = null)
|
||
{
|
||
$row = $this->model->get($ids);
|
||
if (!$row) {
|
||
$this->error('记录不存在');
|
||
}
|
||
try {
|
||
if (($row['channel'] ?? 'wechat') === 'huifu' && class_exists('\\addons\\shopro_huifu\\library\\HuifuSplitService')) {
|
||
$service = new \addons\shopro_huifu\library\HuifuSplitService();
|
||
$result = $service->querySplit($row['transaction_id']);
|
||
} else {
|
||
$service = new ProfitSharingService();
|
||
$result = $service->queryOrder([
|
||
'out_order_no' => $row['out_order_no'],
|
||
'transaction_id' => $row['transaction_id'],
|
||
]);
|
||
}
|
||
$row->wx_result = json_encode($result, JSON_UNESCAPED_UNICODE);
|
||
$row->save();
|
||
$this->success('查询成功', null, $result);
|
||
} catch (\Exception $e) {
|
||
$this->error($e->getMessage());
|
||
}
|
||
}
|
||
}
|