feat(split): 优化汇付分账服务逻辑并增强重试功能

- 修改HuifuSplitService中分账状态查询逻辑,从检查状态[1,2]改为仅检查状态2
- 更新删除条件,从删除状态4改为删除状态[1,4]
- 在分账明细为空时添加详细的错误提示信息
- 新增retryByOrderId方法支持订单ID重试
- 优化后台控制器中的重试逻辑,添加分账记录存在性验证
- 改进错误提示信息,提供更准确的失败原因说明
```
这个提交包含在:
可乐加冰
2026-10-02 14:26:17 +08:00
父节点 246935728e
当前提交 f238ff212a
共有 2 个文件被更改,包括 45 次插入 和 27 次删除

查看文件

@@ -27,11 +27,11 @@ class HuifuSplitService
return false;
}
$exists = SplitOrder::where('order_id', $orderId)->where('channel', 'huifu')->where('status', 'in', [1, 2])->find();
if ($exists) {
$done = SplitOrder::where('order_id', $orderId)->where('channel', 'huifu')->where('status', 2)->find();
if ($done) {
return true;
}
SplitOrder::where('order_id', $orderId)->where('channel', 'huifu')->where('status', 4)->delete();
SplitOrder::where('order_id', $orderId)->where('channel', 'huifu')->where('status', 'in', [1, 4])->delete();
$calculator = new SplitCalculator();
$items = $calculator->calculate($orderId);
@@ -41,6 +41,8 @@ class HuifuSplitService
if ($rewardCount + $regionalCount > 0) {
\think\Log::warning('[shopro_huifu] split skipped order_id=' . $orderId . ' reason=receiver_missing_or_no_huifu_id');
}
$msg = '分账明细为空:请检查分账金额来源、汇付接收方(huifu_id)与佣金/规则是否大于0';
$this->markFailed($orderId, 'HFPS' . date('YmdHis') . $orderId, $order, [], $msg);
return false;
}
@@ -117,7 +119,16 @@ class HuifuSplitService
if (!$splitOrder || $splitOrder['channel'] !== 'huifu') {
return false;
}
return $this->executeByOrderId($splitOrder['order_id']);
return $this->executeByOrderId((int) $splitOrder['order_id']);
}
public function retryByOrderId($orderId)
{
$orderId = (int) $orderId;
if ($orderId <= 0) {
return false;
}
return $this->executeByOrderId($orderId);
}
protected function confirm($ext, array $acctInfos, $confirmReqSeqId)

查看文件

@@ -32,35 +32,42 @@ class Order extends Base
{
$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 = '';
if ($row) {
$latest = $this->model->where('order_id', $row['order_id'])
->where('channel', $row['channel'] ?? 'wechat')
->order('id', 'desc')
->find();
if ($latest && !empty($latest['wx_result'])) {
$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'] ?? '',
]);
$hint = $parts ? (':' . implode(' ', $parts)) : (':' . mb_substr($raw, 0, 300));
} else {
$hint = ':' . mb_substr($raw, 0, 300);
}
}
}
$hint = $this->formatSplitWxResultHint($orderId, $channel);
if ($hint === '') {
$hint = ':常见为分账明细为空(佣金/规则未匹配到汇付接收方)、汇付未开通分账/延时分账权限、接收方未绑定分账关系';
$hint = ':请确认已部署最新 HuifuSplitService.php;或执行 php addons/shopro/tests/split_commission_verify.php try-split ' . $row['order_sn'] . ' --execute 查看详情';
}
$this->error('重试失败,请查看分账记录 wx_result' . $hint);
$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)