diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 9e882a9..929631e 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -310,6 +310,40 @@ class PlatformOrderController extends Controller return redirect()->back()->with('success', '订单已标记支付并生效,订阅已同步:' . $subscription->subscription_no); } + public function markActivated(Request $request, PlatformOrder $order): RedirectResponse + { + $admin = $this->ensurePlatformAdmin($request); + + // 仅标记“已生效”:用于处理已支付但未生效的订单(不改 payment_status) + if ($order->payment_status !== 'paid') { + return redirect()->back()->with('warning', '当前订单尚未支付,无法仅标记为已生效。'); + } + + if ($order->status === 'activated') { + return redirect()->back()->with('warning', '当前订单已是已生效状态,无需重复操作。'); + } + + $now = now(); + $order->status = 'activated'; + $order->activated_at = $order->activated_at ?: $now; + $order->save(); + + // 轻量审计:记录这次“仅标记生效”的动作,便于追溯 + $meta = (array) ($order->meta ?? []); + $audit = (array) (data_get($meta, 'audit', []) ?? []); + $audit[] = [ + 'action' => 'mark_activated', + 'scope' => 'single', + 'at' => $now->toDateTimeString(), + 'admin_id' => $admin->id, + ]; + data_set($meta, 'audit', $audit); + $order->meta = $meta; + $order->save(); + + return redirect()->back()->with('success', '订单已标记为已生效(未修改支付状态)。'); + } + public function export(Request $request): StreamedResponse { $this->ensurePlatformAdmin($request); diff --git a/resources/views/admin/platform_orders/index.blade.php b/resources/views/admin/platform_orders/index.blade.php index bebbc83..0f74d20 100644 --- a/resources/views/admin/platform_orders/index.blade.php +++ b/resources/views/admin/platform_orders/index.blade.php @@ -354,6 +354,14 @@ + @php + $canMarkActivatedOnly = ($order->payment_status === 'paid') && ($order->status !== 'activated'); + @endphp +
+ @csrf + +
+
@csrf diff --git a/resources/views/admin/platform_orders/show.blade.php b/resources/views/admin/platform_orders/show.blade.php index 495050b..d13bd7a 100644 --- a/resources/views/admin/platform_orders/show.blade.php +++ b/resources/views/admin/platform_orders/show.blade.php @@ -44,6 +44,14 @@ @csrf
+ + @php + $canMarkActivatedOnly = ($order->payment_status === 'paid') && ($order->status !== 'activated'); + @endphp +
+ @csrf + +
@if(! $canActivate) diff --git a/routes/web.php b/routes/web.php index 83439bd..578a5de 100644 --- a/routes/web.php +++ b/routes/web.php @@ -108,6 +108,7 @@ Route::prefix('admin')->group(function () { Route::get('/platform-orders/{order}', [PlatformOrderController::class, 'show']); Route::post('/platform-orders/{order}/activate-subscription', [PlatformOrderController::class, 'activateSubscription']); Route::post('/platform-orders/{order}/mark-paid-and-activate', [PlatformOrderController::class, 'markPaidAndActivate']); + Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']); Route::get('/site-subscriptions', [SiteSubscriptionController::class, 'index']); Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']); diff --git a/tests/Feature/AdminPlatformOrderMarkActivatedTest.php b/tests/Feature/AdminPlatformOrderMarkActivatedTest.php new file mode 100644 index 0000000..28c49d8 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderMarkActivatedTest.php @@ -0,0 +1,148 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_platform_admin_can_mark_paid_order_as_activated_without_changing_payment_status(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'mark_activated_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_0001', + 'order_type' => 'renewal', + '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' => [], + ]); + + $this->post('/admin/platform-orders/' . $order->id . '/mark-activated') + ->assertRedirect(); + + $order->refresh(); + $this->assertSame('activated', $order->status); + $this->assertSame('paid', $order->payment_status); + $this->assertNotNull($order->activated_at); + + $audit = (array) (data_get($order->meta, 'audit', []) ?? []); + $this->assertNotEmpty($audit); + $this->assertSame('mark_activated', (string) data_get(end($audit), 'action')); + } + + public function test_cannot_mark_unpaid_order_as_activated(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'mark_activated_plan2', + 'name' => '标记生效测试套餐2', + '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_0002', + 'order_type' => 'renewal', + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 10, + 'paid_amount' => 0, + 'placed_at' => now()->subMinutes(10), + ]); + + $this->post('/admin/platform-orders/' . $order->id . '/mark-activated') + ->assertRedirect(); + + $order->refresh(); + $this->assertSame('pending', $order->status); + $this->assertSame('unpaid', $order->payment_status); + } + + public function test_guest_cannot_mark_order_as_activated(): void + { + $this->seed(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'mark_activated_plan3', + 'name' => '标记生效测试套餐3', + '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_0003', + 'order_type' => 'renewal', + '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), + ]); + + $this->post('/admin/platform-orders/' . $order->id . '/mark-activated') + ->assertRedirect('/admin/login'); + } +}