fix: 优化订单及售后退款控制器异常捕获,避免第三方支付错误触发500
这个提交包含在:
@@ -297,6 +297,7 @@ class Aftersale extends Backend
|
||||
$this->error('订单商品已退款,不能重复退款');
|
||||
}
|
||||
|
||||
try {
|
||||
Db::transaction(function () use ($aftersale, $order, $orderItem, $refund_money, $refund_surplus_money) {
|
||||
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
|
||||
\think\Hook::listen('aftersale_finish_before', $data);
|
||||
@@ -323,6 +324,9 @@ class Aftersale extends Backend
|
||||
$data = ['aftersale' => $aftersale, 'order' => $order, 'item' => $orderItem];
|
||||
\think\Hook::listen('aftersale_finish_after', $data);
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->success('操作成功');
|
||||
}
|
||||
|
||||
@@ -1034,6 +1034,7 @@ class Order extends Base
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Db::transaction(function () use ($order, $items, $item_id, $refund_money, $refund_surplus_money) {
|
||||
if ($item_id) {
|
||||
// 单个商品退款
|
||||
@@ -1068,6 +1069,9 @@ class Order extends Base
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
|
||||
$item_list = \app\admin\model\shopro\order\OrderItem::where(['order_id' => $id])->select();
|
||||
return $this->success('操作成功', null, $item_list);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* 订单与售后退款异常捕获单元测试 (TDD)
|
||||
*/
|
||||
|
||||
class OrderRefundExceptionCatchTest
|
||||
{
|
||||
private $passed = 0;
|
||||
private $failed = 0;
|
||||
|
||||
public function run()
|
||||
{
|
||||
echo "=== 开始执行 退款控制器异常捕获单元测试 (TDD) ===\n";
|
||||
$this->testAftersaleRefundCatchesThrowable();
|
||||
$this->testOrderRefundCatchesThrowable();
|
||||
|
||||
echo "\n测试结果: {$this->passed} 通过, {$this->failed} 失败\n";
|
||||
if ($this->failed > 0) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private function assert($condition, $message)
|
||||
{
|
||||
if ($condition) {
|
||||
echo " [PASS] {$message}\n";
|
||||
$this->passed++;
|
||||
} else {
|
||||
echo " [FAIL] {$message}\n";
|
||||
$this->failed++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 Aftersale 控制器 refund 方法是否具备 try catch (\Throwable $e) 与 $this->error($e->getMessage())
|
||||
*/
|
||||
public function testAftersaleRefundCatchesThrowable()
|
||||
{
|
||||
$filePath = __DIR__ . '/../application/admin/controller/shopro/order/Aftersale.php';
|
||||
$this->assert(is_file($filePath), "Aftersale.php 控制器必须存在");
|
||||
|
||||
$content = file_get_contents($filePath);
|
||||
|
||||
// 提取 refund 方法体
|
||||
$startPos = strpos($content, 'function refund(');
|
||||
$this->assert($startPos !== false, "Aftersale.php 必须包含 refund 方法");
|
||||
|
||||
$nextFuncPos = strpos($content, 'public function ', $startPos + 20);
|
||||
$methodBody = substr($content, $startPos, $nextFuncPos - $startPos);
|
||||
|
||||
$hasTryCatch = preg_match('/try\s*\{\s*Db::transaction\(.+?\}\s*catch\s*\(\\\\?Throwable\s+\$e\)\s*\{\s*\$this->error\(\$e->getMessage\(\)\);\s*\}/s', $methodBody);
|
||||
$this->assert((bool)$hasTryCatch, "Aftersale.php 的 refund 方法必须使用 try...catch(\\Throwable \$e) 包裹 Db::transaction 并在失败时调用 \$this->error(\$e->getMessage())");
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试 Order 控制器 refund 方法是否具备 try catch (\Throwable $e) 与 $this->error($e->getMessage())
|
||||
*/
|
||||
public function testOrderRefundCatchesThrowable()
|
||||
{
|
||||
$filePath = __DIR__ . '/../application/admin/controller/shopro/order/Order.php';
|
||||
$this->assert(is_file($filePath), "Order.php 控制器必须存在");
|
||||
|
||||
$content = file_get_contents($filePath);
|
||||
|
||||
// 提取 refund 方法体
|
||||
$startPos = strpos($content, 'function refund(');
|
||||
$this->assert($startPos !== false, "Order.php 必须包含 refund 方法");
|
||||
|
||||
$nextFuncPos = strpos($content, 'public function ', $startPos + 20);
|
||||
$methodBody = substr($content, $startPos, $nextFuncPos - $startPos);
|
||||
|
||||
$hasTryCatch = preg_match('/try\s*\{\s*Db::transaction\(.+?\}\s*catch\s*\(\\\\?Throwable\s+\$e\)\s*\{\s*\$this->error\(\$e->getMessage\(\)\);\s*\}/s', $methodBody);
|
||||
$this->assert((bool)$hasTryCatch, "Order.php 的 refund 方法必须使用 try...catch(\\Throwable \$e) 包裹 Db::transaction 并在失败时调用 \$this->error(\$e->getMessage())");
|
||||
}
|
||||
}
|
||||
|
||||
$test = new OrderRefundExceptionCatchTest();
|
||||
$test->run();
|
||||
在新工单中引用
屏蔽一个用户