退款治理:手动修正状态增加安全阀并补测试
This commit is contained in:
@@ -584,7 +584,8 @@ class PlatformOrderController extends Controller
|
||||
{
|
||||
$admin = $this->ensurePlatformAdmin($request);
|
||||
|
||||
if ((float) ($order->paid_amount ?? 0) <= 0) {
|
||||
$paidAmount = (float) ($order->paid_amount ?? 0);
|
||||
if ($paidAmount <= 0) {
|
||||
return redirect()->back()->with('warning', '当前订单已付金额为 0,无法标记为已退款。');
|
||||
}
|
||||
|
||||
@@ -592,6 +593,12 @@ class PlatformOrderController extends Controller
|
||||
return redirect()->back()->with('warning', '当前订单已是已退款状态,无需重复操作。');
|
||||
}
|
||||
|
||||
// 安全阀:仅允许在“退款总额已达到/超过已付金额”时标记为已退款
|
||||
$refundTotal = (float) $this->refundTotalForOrder($order);
|
||||
if (round($refundTotal * 100) + 1 < round($paidAmount * 100)) {
|
||||
return redirect()->back()->with('warning', '退款总额尚未达到已付金额,无法标记为已退款。请先核对/补齐退款记录。');
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$order->payment_status = 'refunded';
|
||||
$order->refunded_at = $order->refunded_at ?: $now;
|
||||
@@ -617,7 +624,8 @@ class PlatformOrderController extends Controller
|
||||
{
|
||||
$admin = $this->ensurePlatformAdmin($request);
|
||||
|
||||
if ((float) ($order->paid_amount ?? 0) <= 0) {
|
||||
$paidAmount = (float) ($order->paid_amount ?? 0);
|
||||
if ($paidAmount <= 0) {
|
||||
return redirect()->back()->with('warning', '当前订单已付金额为 0,无法标记为部分退款。');
|
||||
}
|
||||
|
||||
@@ -625,6 +633,15 @@ class PlatformOrderController extends Controller
|
||||
return redirect()->back()->with('warning', '当前订单已是部分退款状态,无需重复操作。');
|
||||
}
|
||||
|
||||
// 安全阀:部分退款需要“退款总额>0 且未达到已付金额”
|
||||
$refundTotal = (float) $this->refundTotalForOrder($order);
|
||||
if (round($refundTotal * 100) <= 0) {
|
||||
return redirect()->back()->with('warning', '退款总额为 0,无法标记为部分退款。');
|
||||
}
|
||||
if (round($refundTotal * 100) + 1 >= round($paidAmount * 100)) {
|
||||
return redirect()->back()->with('warning', '退款总额已达到/超过已付金额,建议标记为已退款。');
|
||||
}
|
||||
|
||||
$now = now();
|
||||
$order->payment_status = 'partially_refunded';
|
||||
$order->refunded_at = $order->refunded_at ?: $now;
|
||||
@@ -650,10 +667,15 @@ class PlatformOrderController extends Controller
|
||||
{
|
||||
$admin = $this->ensurePlatformAdmin($request);
|
||||
|
||||
if ((float) ($order->paid_amount ?? 0) <= 0) {
|
||||
$paidAmount = (float) ($order->paid_amount ?? 0);
|
||||
if ($paidAmount <= 0) {
|
||||
return redirect()->back()->with('warning', '当前订单已付金额为 0,无法标记为已支付。');
|
||||
}
|
||||
|
||||
if ((string) $order->payment_status === 'unpaid') {
|
||||
return redirect()->back()->with('warning', '当前订单为未支付状态,不允许直接标记为已支付,请使用「标记支付并生效」或补回执/金额后再处理。');
|
||||
}
|
||||
|
||||
if ((string) $order->payment_status === 'paid') {
|
||||
return redirect()->back()->with('warning', '当前订单已是已支付状态,无需重复操作。');
|
||||
}
|
||||
@@ -1451,6 +1473,25 @@ class PlatformOrderController extends Controller
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
private function refundTotalForOrder(PlatformOrder $order): float
|
||||
{
|
||||
// 优先读扁平字段 refund_summary.total_amount
|
||||
$total = data_get($order->meta, 'refund_summary.total_amount');
|
||||
if ($total !== null) {
|
||||
return (float) $total;
|
||||
}
|
||||
|
||||
// 回退:遍历 refund_receipts[].amount
|
||||
$refunds = (array) (data_get($order->meta, 'refund_receipts', []) ?? []);
|
||||
$sum = 0.0;
|
||||
foreach ($refunds as $r) {
|
||||
$sum += (float) (data_get($r, 'amount') ?? 0);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
protected function sumReceiptAmount($orders): float
|
||||
{
|
||||
$total = 0.0;
|
||||
|
||||
Reference in New Issue
Block a user