From 5ca76b7620394851df973722f477194a3e91d93b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Sun, 15 Mar 2026 05:18:46 +0000 Subject: [PATCH] platform orders: activate subscription guard for renewal missing subscription --- .../Admin/PlatformOrderController.php | 5 ++ ...ewalMissingSubscriptionShouldGuardTest.php | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/Feature/AdminPlatformOrderActivateSubscriptionRenewalMissingSubscriptionShouldGuardTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 434aae2..91837c0 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -638,6 +638,11 @@ class PlatformOrderController extends Controller { $admin = $this->ensurePlatformAdmin($request); + // 治理优先:续费单必须绑定订阅(兼容历史脏数据/手工改库等场景) + if ((string) ($order->order_type ?? '') === 'renewal' && ! (int) ($order->site_subscription_id ?? 0)) { + return redirect()->back()->with('warning', '当前订单类型为「续费」,但未绑定订阅(site_subscription_id 为空)。为避免误同步/续期串单,请先补齐订阅关联后再处理。'); + } + // 治理优先:当订单命中金额/状态不一致时,不建议直接同步订阅(避免把“带病订单”同步到订阅) if ($order->isReconcileMismatch() || $order->isRefundInconsistent()) { return redirect()->back()->with('warning', '当前订单命中「对账不一致/退款不一致」,为避免带病同步,请先完成金额/状态治理(补回执/核对退款/修正状态)后再同步订阅。'); diff --git a/tests/Feature/AdminPlatformOrderActivateSubscriptionRenewalMissingSubscriptionShouldGuardTest.php b/tests/Feature/AdminPlatformOrderActivateSubscriptionRenewalMissingSubscriptionShouldGuardTest.php new file mode 100644 index 0000000..4fa16fd --- /dev/null +++ b/tests/Feature/AdminPlatformOrderActivateSubscriptionRenewalMissingSubscriptionShouldGuardTest.php @@ -0,0 +1,68 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_activate_subscription_should_be_blocked_when_renewal_order_missing_subscription(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'activate_subscription_renewal_missing_sub_plan', + 'name' => '同步订阅阻断(续费无订阅)测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 88, + 'list_price' => 88, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_ACT_RENEW_NO_SUB_0001', + 'order_type' => 'renewal', + // 备注:这里模拟一种脏数据:续费单被标记为已支付/已生效,但未绑定订阅 + 'status' => 'activated', + 'payment_status' => 'paid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 88, + 'paid_amount' => 88, + 'placed_at' => now()->subMinutes(10), + 'paid_at' => now()->subMinutes(5), + 'activated_at' => now()->subMinutes(1), + 'meta' => [], + ]); + + $res = $this->post('/admin/platform-orders/' . $order->id . '/activate-subscription'); + $res->assertRedirect(); + $res->assertSessionHas('warning'); + + $order->refresh(); + $this->assertNull($order->site_subscription_id); + } +}