feat(admin): 续费订单支持手工绑定订阅(attach-subscription)+ 时区改为 Asia/Shanghai
This commit is contained in:
@@ -645,6 +645,67 @@ class PlatformOrderController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 治理动作:为订单手工绑定订阅(用于“续费缺订阅”脏数据修复)。
|
||||||
|
*
|
||||||
|
* 口径:
|
||||||
|
* - 仅平台管理员可操作(ensurePlatformAdmin)
|
||||||
|
* - 仅允许续费单绑定订阅
|
||||||
|
* - 订阅必须与订单 merchant_id / plan_id 一致,避免串单
|
||||||
|
* - 写入 meta.audit 留痕
|
||||||
|
*/
|
||||||
|
public function attachSubscription(Request $request, PlatformOrder $order): RedirectResponse
|
||||||
|
{
|
||||||
|
$admin = $this->ensurePlatformAdmin($request);
|
||||||
|
|
||||||
|
$data = $request->validate([
|
||||||
|
'site_subscription_id' => ['required', 'integer', 'exists:site_subscriptions,id'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ((string) ($order->order_type ?? '') !== 'renewal') {
|
||||||
|
return redirect()->back()->with('warning', '仅「续费」类型订单允许绑定订阅。');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((int) ($order->site_subscription_id ?? 0) > 0) {
|
||||||
|
return redirect()->back()->with('warning', '该订单已绑定订阅,无需重复操作。');
|
||||||
|
}
|
||||||
|
|
||||||
|
$subId = (int) $data['site_subscription_id'];
|
||||||
|
$sub = SiteSubscription::query()->with(['merchant', 'plan'])->findOrFail($subId);
|
||||||
|
|
||||||
|
// 强约束:订阅上下文必须与订单一致
|
||||||
|
if ((int) ($sub->merchant_id ?? 0) !== (int) ($order->merchant_id ?? 0)) {
|
||||||
|
return redirect()->back()->withErrors([
|
||||||
|
'site_subscription_id' => '订阅所属站点与订单站点不一致,禁止绑定(避免串单)。',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if ((int) ($sub->plan_id ?? 0) !== (int) ($order->plan_id ?? 0)) {
|
||||||
|
return redirect()->back()->withErrors([
|
||||||
|
'site_subscription_id' => '订阅套餐与订单套餐不一致,禁止绑定(避免跨套餐续费)。',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$order->site_subscription_id = $sub->id;
|
||||||
|
|
||||||
|
$meta = (array) ($order->meta ?? []);
|
||||||
|
$audit = (array) (data_get($meta, 'audit', []) ?? []);
|
||||||
|
$audit[] = [
|
||||||
|
'action' => 'attach_subscription',
|
||||||
|
'scope' => 'single',
|
||||||
|
'at' => now()->toDateTimeString(),
|
||||||
|
'admin_id' => $admin->id,
|
||||||
|
'subscription_id' => $sub->id,
|
||||||
|
'subscription_no' => (string) ($sub->subscription_no ?? ''),
|
||||||
|
'note' => '续费缺订阅治理:手工绑定订阅',
|
||||||
|
];
|
||||||
|
data_set($meta, 'audit', $audit);
|
||||||
|
$order->meta = $meta;
|
||||||
|
|
||||||
|
$order->save();
|
||||||
|
|
||||||
|
return redirect()->back()->with('success', '已绑定订阅:' . (string) ($sub->subscription_no ?? $sub->id));
|
||||||
|
}
|
||||||
|
|
||||||
public function activateSubscription(Request $request, PlatformOrder $order, SubscriptionActivationService $service): RedirectResponse
|
public function activateSubscription(Request $request, PlatformOrder $order, SubscriptionActivationService $service): RedirectResponse
|
||||||
{
|
{
|
||||||
$admin = $this->ensurePlatformAdmin($request);
|
$admin = $this->ensurePlatformAdmin($request);
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ return [
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'timezone' => 'UTC',
|
'timezone' => env('APP_TIMEZONE', 'Asia/Shanghai'),
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -129,6 +129,8 @@ Route::prefix('admin')->group(function () {
|
|||||||
Route::post('/platform-orders/{order}/mark-partially-refunded', [PlatformOrderController::class, 'markPartiallyRefunded']);
|
Route::post('/platform-orders/{order}/mark-partially-refunded', [PlatformOrderController::class, 'markPartiallyRefunded']);
|
||||||
Route::post('/platform-orders/{order}/mark-paid-status', [PlatformOrderController::class, 'markPaidStatus']);
|
Route::post('/platform-orders/{order}/mark-paid-status', [PlatformOrderController::class, 'markPaidStatus']);
|
||||||
Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']);
|
Route::post('/platform-orders/{order}/mark-activated', [PlatformOrderController::class, 'markActivated']);
|
||||||
|
// 治理动作:续费缺订阅时,手工绑定订阅(补齐闭环)
|
||||||
|
Route::post('/platform-orders/{order}/attach-subscription', [PlatformOrderController::class, 'attachSubscription']);
|
||||||
Route::post('/platform-orders/{order}/clear-sync-error', [PlatformOrderController::class, 'clearSyncError']);
|
Route::post('/platform-orders/{order}/clear-sync-error', [PlatformOrderController::class, 'clearSyncError']);
|
||||||
Route::post('/platform-orders/{order}/clear-bmpa-error', [PlatformOrderController::class, 'clearBmpaError']);
|
Route::post('/platform-orders/{order}/clear-bmpa-error', [PlatformOrderController::class, 'clearBmpaError']);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\PlatformOrder;
|
||||||
|
use App\Models\SiteSubscription;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class AdminPlatformOrderAttachSubscriptionShouldGuardTest 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_attach_subscription_should_guard_non_renewal_orders(): void
|
||||||
|
{
|
||||||
|
$this->loginAsPlatformAdmin();
|
||||||
|
|
||||||
|
$order = PlatformOrder::query()->create([
|
||||||
|
'merchant_id' => 1,
|
||||||
|
'plan_id' => 1,
|
||||||
|
'order_no' => 'PO_TEST_ATTACH_1',
|
||||||
|
'order_type' => 'new_purchase',
|
||||||
|
'status' => 'placed',
|
||||||
|
'payment_status' => 'unpaid',
|
||||||
|
'payment_channel' => '',
|
||||||
|
'plan_name' => 'Test',
|
||||||
|
'billing_cycle' => 'monthly',
|
||||||
|
'period_months' => 1,
|
||||||
|
'quantity' => 1,
|
||||||
|
'list_amount' => 0,
|
||||||
|
'discount_amount' => 0,
|
||||||
|
'payable_amount' => 0,
|
||||||
|
'paid_amount' => 0,
|
||||||
|
'meta' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$sub = SiteSubscription::query()->firstOrFail();
|
||||||
|
|
||||||
|
$res = $this->post("/admin/platform-orders/{$order->id}/attach-subscription", [
|
||||||
|
'site_subscription_id' => $sub->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$res->assertRedirect();
|
||||||
|
|
||||||
|
$order->refresh();
|
||||||
|
$this->assertTrue((int) ($order->site_subscription_id ?? 0) === 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user