diff --git a/tests/Feature/AdminBillingClosedLoopRenewalMissingSubscriptionSopTest.php b/tests/Feature/AdminBillingClosedLoopRenewalMissingSubscriptionSopTest.php new file mode 100644 index 0000000..41a68cf --- /dev/null +++ b/tests/Feature/AdminBillingClosedLoopRenewalMissingSubscriptionSopTest.php @@ -0,0 +1,129 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_sop_renewal_missing_subscription_bind_then_receipt_then_bmpa_should_activate_and_keep_subscription(): void + { + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + + $plan = Plan::query()->create([ + 'code' => 'sop_renewal_missing_sub_monthly', + 'name' => 'SOP续费缺订阅(月付)', + 'billing_cycle' => 'monthly', + 'price' => 30, + 'list_price' => 30, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + // 准备一条“正确订阅”(模拟真实续费对应的订阅) + $sub = SiteSubscription::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'status' => 'activated', + 'source' => 'manual', + 'subscription_no' => 'SS_SOP_RENEWAL_0001', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'amount' => 30, + 'starts_at' => now()->subDays(10), + 'ends_at' => now()->addDays(10), + 'snapshot' => [], + 'meta' => [], + ]); + + // 准备一条“续费单但缺订阅”订单(存量脏数据) + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_SOP_RENEWAL_MISS_SUB_0001', + 'order_type' => 'renewal', + 'site_subscription_id' => null, + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'plan_name' => $plan->name, + 'billing_cycle' => $plan->billing_cycle, + 'period_months' => 1, + 'quantity' => 1, + 'payable_amount' => 30, + 'paid_amount' => 0, + 'placed_at' => now()->subMinutes(10), + 'meta' => [], + ]); + + // 1) 缺订阅时 BMPA 必须阻断 + $res = $this->from('/admin/platform-orders/' . $order->id) + ->post('/admin/platform-orders/' . $order->id . '/mark-paid-and-activate'); + $res->assertRedirect('/admin/platform-orders/' . $order->id); + $res->assertSessionHas('warning'); + + $order->refresh(); + $this->assertNull($order->site_subscription_id); + $this->assertSame('pending', (string) $order->status); + $this->assertSame('unpaid', (string) $order->payment_status); + + // 2) 运营先绑定正确订阅 + $this->post('/admin/platform-orders/' . $order->id . '/attach-subscription', [ + 'site_subscription_id' => $sub->id, + ])->assertRedirect(); + + $order->refresh(); + $this->assertSame($sub->id, (int) $order->site_subscription_id); + + // 3) 补回执(用于对账留痕) + $this->post('/admin/platform-orders/' . $order->id . '/add-payment-receipt', [ + 'type' => 'bank_transfer', + 'channel' => 'icbc', + 'amount' => 30, + 'paid_at' => now()->format('Y-m-d H:i:s'), + 'note' => '续费回执(测试)', + ])->assertRedirect(); + + // 4) 再执行 BMPA:应推进订单为 activated,且仍绑定原订阅(不应生成新订阅) + $this->post('/admin/platform-orders/' . $order->id . '/mark-paid-and-activate') + ->assertRedirect(); + + $order->refresh(); + $this->assertSame('paid', (string) $order->payment_status); + $this->assertSame('activated', (string) $order->status); + $this->assertSame($sub->id, (int) $order->site_subscription_id); + + // 订阅应仍可用(至少能被正常读取) + $sub->refresh(); + $this->assertSame($merchant->id, (int) $sub->merchant_id); + $this->assertSame($plan->id, (int) $sub->plan_id); + } +}