diff --git a/addons/shopro_huifu/library/HuifuSplitService.php b/addons/shopro_huifu/library/HuifuSplitService.php index 8d29c37..50d6f9a 100644 --- a/addons/shopro_huifu/library/HuifuSplitService.php +++ b/addons/shopro_huifu/library/HuifuSplitService.php @@ -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) diff --git a/application/admin/controller/shopro/split/Order.php b/application/admin/controller/shopro/split/Order.php index 27b1dfc..46fee11 100644 --- a/application/admin/controller/shopro/split/Order.php +++ b/application/admin/controller/shopro/split/Order.php @@ -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)