feat(platform-orders): 支持仅标记订单为已生效并写入审计
This commit is contained in:
@@ -310,6 +310,40 @@ class PlatformOrderController extends Controller
|
|||||||
return redirect()->back()->with('success', '订单已标记支付并生效,订阅已同步:' . $subscription->subscription_no);
|
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
|
public function export(Request $request): StreamedResponse
|
||||||
{
|
{
|
||||||
$this->ensurePlatformAdmin($request);
|
$this->ensurePlatformAdmin($request);
|
||||||
|
|||||||
@@ -354,6 +354,14 @@
|
|||||||
<button type="submit" @disabled(! $canMarkPaid)>标记支付并生效</button>
|
<button type="submit" @disabled(! $canMarkPaid)>标记支付并生效</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
@php
|
||||||
|
$canMarkActivatedOnly = ($order->payment_status === 'paid') && ($order->status !== 'activated');
|
||||||
|
@endphp
|
||||||
|
<form method="post" action="/admin/platform-orders/{{ $order->id }}/mark-activated" onsubmit="return confirm('确认将该订单标记为已生效?(不修改支付状态,不自动同步订阅)');" class="mb-6">
|
||||||
|
@csrf
|
||||||
|
<button type="submit" @disabled(! $canMarkActivatedOnly)>仅标记为已生效</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
<form method="post" action="/admin/platform-orders/{{ $order->id }}/activate-subscription" onsubmit="return confirm('确认同步订阅?将根据该订单创建/续期订阅');">
|
<form method="post" action="/admin/platform-orders/{{ $order->id }}/activate-subscription" onsubmit="return confirm('确认同步订阅?将根据该订单创建/续期订阅');">
|
||||||
@csrf
|
@csrf
|
||||||
<button type="submit" @disabled(! $canActivate || $alreadySynced)>{{ $alreadySynced ? '已同步' : '同步订阅' }}</button>
|
<button type="submit" @disabled(! $canActivate || $alreadySynced)>{{ $alreadySynced ? '已同步' : '同步订阅' }}</button>
|
||||||
|
|||||||
@@ -44,6 +44,14 @@
|
|||||||
@csrf
|
@csrf
|
||||||
<button type="submit" @disabled(! $canActivate || $alreadySynced)>{{ $alreadySynced ? '已同步' : '同步订阅' }}</button>
|
<button type="submit" @disabled(! $canActivate || $alreadySynced)>{{ $alreadySynced ? '已同步' : '同步订阅' }}</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
@php
|
||||||
|
$canMarkActivatedOnly = ($order->payment_status === 'paid') && ($order->status !== 'activated');
|
||||||
|
@endphp
|
||||||
|
<form method="post" action="/admin/platform-orders/{{ $order->id }}/mark-activated" onsubmit="return confirm('确认将该订单标记为已生效?(不修改支付状态,不自动同步订阅)');">
|
||||||
|
@csrf
|
||||||
|
<button type="submit" @disabled(! $canMarkActivatedOnly)>仅标记为已生效</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if(! $canActivate)
|
@if(! $canActivate)
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ Route::prefix('admin')->group(function () {
|
|||||||
Route::get('/platform-orders/{order}', [PlatformOrderController::class, 'show']);
|
Route::get('/platform-orders/{order}', [PlatformOrderController::class, 'show']);
|
||||||
Route::post('/platform-orders/{order}/activate-subscription', [PlatformOrderController::class, 'activateSubscription']);
|
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-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', [SiteSubscriptionController::class, 'index']);
|
||||||
Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);
|
Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);
|
||||||
|
|||||||
148
tests/Feature/AdminPlatformOrderMarkActivatedTest.php
Normal file
148
tests/Feature/AdminPlatformOrderMarkActivatedTest.php
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
<?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 AdminPlatformOrderMarkActivatedTest 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_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');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user