标记支付并生效:回执差额存在时阻断并提示先治理

This commit is contained in:
萝卜
2026-03-11 08:41:09 +00:00
parent 9a566da982
commit 87df857d4d
2 changed files with 83 additions and 0 deletions

View File

@@ -410,6 +410,16 @@ class PlatformOrderController extends Controller
return redirect()->back()->with('warning', '当前订单已存在退款记录/退款汇总,请先核对退款轨迹与订单状态后再处理(不建议直接标记支付并生效)。');
}
// 治理优先:若该订单已有回执但回执总额与应标记的已付金额不一致,不允许直接“标记支付并生效”
// 说明:该动作会尝试补回执并同步订阅,若原回执存在差额,直接推进可能掩盖问题
$receiptTotal = (float) $order->receiptTotal();
$expectedPaid = (float) (($order->paid_amount ?? 0) > 0 ? $order->paid_amount : $order->payable_amount);
$receiptCents = (int) round($receiptTotal * 100);
$expectedCents = (int) round($expectedPaid * 100);
if ($receiptCents > 0 && abs($receiptCents - $expectedCents) >= 1) {
return redirect()->back()->with('warning', '当前订单已存在支付回执,但回执总额与订单金额不一致。为避免带病推进,请先补齐/修正支付回执后再执行「标记支付并生效」。');
}
// 最小状态推进:将订单标记为已支付 + 已生效,并补齐时间与金额字段
$now = now();
$order->payment_status = 'paid';

View File

@@ -0,0 +1,73 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderMarkPaidAndActivateReconcileMismatchSafetyValveTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_mark_paid_and_activate_blocked_when_receipt_total_mismatch_exists(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'mark_paid_receipt_mismatch_safety_valve_test',
'name' => '标记支付回执差额安全阀测试(月付)',
'billing_cycle' => 'monthly',
'price' => 30,
'list_price' => 30,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
// 已存在回执汇总,但与订单应付金额不一致
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_MARK_PAID_RECEIPT_MISMATCH_BLOCK_0001',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 30,
'paid_amount' => 0,
'placed_at' => now(),
'meta' => [
'payment_summary' => [
'count' => 1,
'total_amount' => 29.98,
],
],
]);
$res = $this->post('/admin/platform-orders/' . $order->id . '/mark-paid-and-activate');
$res->assertRedirect();
$res->assertSessionHas('warning');
$order->refresh();
$this->assertSame('unpaid', $order->payment_status);
$this->assertSame('pending', $order->status);
$this->assertNull($order->site_subscription_id);
}
}