diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 251e046..31945b1 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -122,6 +122,30 @@ class PlatformOrderController extends Controller 'lead_id' => ['nullable', 'integer', 'exists:platform_leads,id'], ]); + // 治理口径:续费单必须与订阅上下文一致(站点/套餐)。 + // 当前阶段暂不支持“跨站点/跨套餐续费”,避免误续费/串单风险;升级/降级后续再单独建设。 + if ((string) ($data['order_type'] ?? '') === 'renewal') { + $subId = (int) ($data['site_subscription_id'] ?? 0); + $sub = $subId > 0 ? SiteSubscription::query()->find($subId) : null; + if (! $sub) { + return redirect()->back()->withErrors([ + 'site_subscription_id' => '续费单必须绑定有效订阅。', + ])->withInput(); + } + + if ((int) ($data['merchant_id'] ?? 0) !== (int) ($sub->merchant_id ?? 0)) { + return redirect()->back()->withErrors([ + 'merchant_id' => '续费单站点必须与订阅所属站点一致(请从订阅维度进入下单)。', + ])->withInput(); + } + + if ((int) ($data['plan_id'] ?? 0) !== (int) ($sub->plan_id ?? 0)) { + return redirect()->back()->withErrors([ + 'plan_id' => '续费单套餐必须与订阅当前套餐一致(当前阶段暂不支持跨套餐续费)。', + ])->withInput(); + } + } + $plan = Plan::query()->findOrFail((int) $data['plan_id']); $periodMonths = $this->periodMonthsFromBillingCycle((string) $plan->billing_cycle); diff --git a/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectMerchantMismatchTest.php b/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectMerchantMismatchTest.php new file mode 100644 index 0000000..1245b37 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectMerchantMismatchTest.php @@ -0,0 +1,76 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_store_should_reject_renewal_when_merchant_mismatch_with_subscription(): void + { + $this->loginAsPlatformAdmin(); + + $merchantA = Merchant::query()->firstOrFail(); + $merchantB = Merchant::query()->create([ + 'name' => '站点B', + 'slug' => 'merchant-b', + 'status' => 'active', + ]); + + $plan = Plan::query()->create([ + 'code' => 'po_store_renewal_merchant_mismatch_plan', + 'name' => '续费下单站点不一致拒绝测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $sub = SiteSubscription::query()->create([ + 'merchant_id' => $merchantA->id, + 'plan_id' => $plan->id, + 'status' => 'activated', + 'source' => 'manual', + 'subscription_no' => 'SUB_PO_STORE_MERCHANT_MISMATCH_0001', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'amount' => 10, + 'starts_at' => now()->subDay(), + 'ends_at' => now()->addMonth(), + 'activated_at' => now()->subDay(), + ]); + + $res = $this->from('/admin/platform-orders/create')->post('/admin/platform-orders', [ + 'merchant_id' => $merchantB->id, + 'plan_id' => $plan->id, + 'site_subscription_id' => $sub->id, + 'order_type' => 'renewal', + 'quantity' => 1, + 'discount_amount' => 0, + 'payment_channel' => 'offline', + 'remark' => 'merchant mismatch', + ]); + + $res->assertStatus(302); + $res->assertSessionHasErrors('merchant_id'); + } +} diff --git a/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectPlanMismatchTest.php b/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectPlanMismatchTest.php new file mode 100644 index 0000000..26c4661 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderStoreRenewalShouldRejectPlanMismatchTest.php @@ -0,0 +1,82 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_store_should_reject_renewal_when_plan_mismatch_with_subscription(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + + $planA = Plan::query()->create([ + 'code' => 'po_store_renewal_plan_mismatch_plan_a', + 'name' => '续费下单套餐不一致拒绝测试套餐A', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $planB = Plan::query()->create([ + 'code' => 'po_store_renewal_plan_mismatch_plan_b', + 'name' => '续费下单套餐不一致拒绝测试套餐B', + 'billing_cycle' => 'monthly', + 'price' => 20, + 'list_price' => 20, + 'status' => 'active', + 'sort' => 9, + 'published_at' => now(), + ]); + + $sub = SiteSubscription::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $planA->id, + 'status' => 'activated', + 'source' => 'manual', + 'subscription_no' => 'SUB_PO_STORE_PLAN_MISMATCH_0001', + 'plan_name' => $planA->name, + 'billing_cycle' => $planA->billing_cycle, + 'period_months' => 1, + 'amount' => 10, + 'starts_at' => now()->subDay(), + 'ends_at' => now()->addMonth(), + 'activated_at' => now()->subDay(), + ]); + + $res = $this->from('/admin/platform-orders/create')->post('/admin/platform-orders', [ + 'merchant_id' => $merchant->id, + 'plan_id' => $planB->id, + 'site_subscription_id' => $sub->id, + 'order_type' => 'renewal', + 'quantity' => 1, + 'discount_amount' => 0, + 'payment_channel' => 'offline', + 'remark' => 'plan mismatch', + ]); + + $res->assertStatus(302); + $res->assertSessionHasErrors('plan_id'); + } +}