diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index b7a8b61..f7fe724 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -1252,6 +1252,14 @@ class PlatformOrderController extends Controller return redirect()->back()->with('warning', '当前订单类型为「续费」,但未绑定订阅(site_subscription_id 为空)。为避免续期串单,请先补齐订阅关联后再标记为已生效。'); } + // 治理优先:对账不一致/存在退款轨迹时,不建议直接“仅标记为已生效”,避免把带病订单推进到 activated。 + if ($order->isReconcileMismatch()) { + return redirect()->back()->with('warning', '当前订单存在对账不一致(支付回执总额与已付金额不一致),请先补齐/修正回执轨迹后再标记为已生效。'); + } + if ((float) $order->refundTotal() > 0) { + return redirect()->back()->with('warning', '当前订单已存在退款记录/退款汇总,请先核对退款轨迹与订单状态后再标记为已生效。'); + } + // 仅标记“已生效”:用于处理已支付但未生效的订单(不改 payment_status) if ($order->payment_status !== 'paid') { return redirect()->back()->with('warning', '当前订单尚未支付,无法仅标记为已生效。'); diff --git a/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenReconcileMismatchTest.php b/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenReconcileMismatchTest.php new file mode 100644 index 0000000..a0e6f00 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenReconcileMismatchTest.php @@ -0,0 +1,77 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_mark_activated_should_be_blocked_when_reconcile_mismatch(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'po_mark_activated_guard_reconcile_mismatch_plan', + 'name' => '仅标记生效阻断(对账不一致)测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_MARK_ACTIVATED_GUARD_RECON_MISMATCH_0001', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'paid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 10, + 'paid_amount' => 10, + 'placed_at' => now()->subMinutes(10), + 'paid_at' => now()->subMinutes(5), + 'meta' => [ + // 存在回执证据,但与 paid_amount 不一致 => isReconcileMismatch=true + 'payment_summary' => [ + 'count' => 1, + 'total_amount' => 1.00, + ], + ], + ]); + + $this->assertTrue($order->isReconcileMismatch()); + + $res = $this->from('/admin/platform-orders/' . $order->id) + ->post('/admin/platform-orders/' . $order->id . '/mark-activated'); + + $res->assertRedirect(); + $res->assertSessionHas('warning'); + + $order->refresh(); + $this->assertSame('pending', (string) $order->status); + $this->assertSame('paid', (string) $order->payment_status); + } +} diff --git a/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenRefundExistsTest.php b/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenRefundExistsTest.php new file mode 100644 index 0000000..cf7522f --- /dev/null +++ b/tests/Feature/AdminPlatformOrderMarkActivatedShouldBlockWhenRefundExistsTest.php @@ -0,0 +1,76 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_mark_activated_should_be_blocked_when_refund_exists(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'po_mark_activated_guard_refund_exists_plan', + 'name' => '仅标记生效阻断(存在退款轨迹)测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_MARK_ACTIVATED_GUARD_REFUND_EXISTS_0001', + 'order_type' => 'new_purchase', + 'status' => 'pending', + 'payment_status' => 'paid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 10, + 'paid_amount' => 10, + 'placed_at' => now()->subMinutes(10), + 'paid_at' => now()->subMinutes(5), + 'meta' => [ + 'refund_summary' => [ + 'count' => 1, + 'total_amount' => 1.00, + ], + ], + ]); + + $this->assertGreaterThan(0, (float) $order->refundTotal()); + + $res = $this->from('/admin/platform-orders/' . $order->id) + ->post('/admin/platform-orders/' . $order->id . '/mark-activated'); + + $res->assertRedirect(); + $res->assertSessionHas('warning'); + + $order->refresh(); + $this->assertSame('pending', (string) $order->status); + $this->assertSame('paid', (string) $order->payment_status); + } +}