PlatformOrder create: default order_type to renewal when from subscription

This commit is contained in:
萝卜
2026-03-15 02:15:48 +00:00
parent 95116e9b7c
commit e66ac765e0
2 changed files with 72 additions and 1 deletions

View File

@@ -30,11 +30,14 @@ class PlatformOrderController extends Controller
$plans = Plan::query()->orderBy('sort')->orderByDesc('id')->get();
// 支持从其它页面(例如订阅详情)带默认值跳转过来,提高运营效率
$orderTypeFromQuery = $request->query('order_type');
$defaults = [
'merchant_id' => (int) $request->query('merchant_id', 0),
'plan_id' => (int) $request->query('plan_id', 0),
'site_subscription_id' => (int) $request->query('site_subscription_id', 0),
'order_type' => (string) $request->query('order_type', 'new_purchase'),
// 未显式传 order_type 时默认 new_purchase若是从订阅进入site_subscription_id 存在),稍后会进一步偏向 renewal。
'order_type' => $orderTypeFromQuery === null ? 'new_purchase' : (string) $orderTypeFromQuery,
'quantity' => (int) $request->query('quantity', 1),
'discount_amount' => (float) $request->query('discount_amount', 0),
'payment_channel' => (string) $request->query('payment_channel', ''),
@@ -66,6 +69,11 @@ class PlatformOrderController extends Controller
$defaults['plan_id'] = (int) ($siteSubscription->plan_id ?? 0);
}
// 治理口径从订阅进入site_subscription_id 存在)时,若未显式指定 order_type则默认偏向续费。
if ($orderTypeFromQuery === null && trim((string) ($defaults['order_type'] ?? '')) === 'new_purchase') {
$defaults['order_type'] = 'renewal';
}
// 续费默认备注:若未显式传 remark自动补齐“来自订阅{subscription_no}”用于追溯/检索。
if ((string) ($defaults['order_type'] ?? '') === 'renewal' && trim((string) ($defaults['remark'] ?? '')) === '') {
$remarkPrefix = (string) config('saasshop.platform_orders.renewal_order_remark_prefix', '来自订阅:');

View File

@@ -0,0 +1,63 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\SiteSubscription;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderCreateDefaultOrderTypeRenewalWhenFromSubscriptionTest 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_create_should_default_to_renewal_when_site_subscription_id_present_and_order_type_not_provided(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'po_create_default_type_renewal_plan',
'name' => '从订阅进入默认续费测试套餐',
'billing_cycle' => 'monthly',
'price' => 10,
'list_price' => 10,
'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' => 'SUB_DEFAULT_TYPE_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(),
]);
// 注意:不传 order_type
$res = $this->get('/admin/platform-orders/create?site_subscription_id=' . $sub->id);
$res->assertOk();
$res->assertSee('value="renewal" selected', false);
}
}