平台订单:补齐 BMPA 单订单清理入口(路由+控制器+详情页按钮+测试)

This commit is contained in:
萝卜
2026-03-14 14:13:30 +00:00
parent f91be76e08
commit 98073f2b71
6 changed files with 202 additions and 0 deletions

0
-n Normal file
View File

View File

@@ -1732,6 +1732,34 @@ class PlatformOrderController extends Controller
return redirect()->back()->with('success', '已清除该订单的同步失败标记。');
}
public function clearBmpaError(Request $request, PlatformOrder $order): RedirectResponse
{
$admin = $this->ensurePlatformAdmin($request);
$meta = (array) ($order->meta ?? []);
if (! data_get($meta, 'batch_mark_paid_and_activate_error')) {
return redirect()->back()->with('warning', '当前订单暂无 BMPA 失败标记,无需清理。');
}
data_forget($meta, 'batch_mark_paid_and_activate_error');
// 轻量审计:记录清理动作(不做独立表,先落 meta便于排查
$audit = (array) (data_get($meta, 'audit', []) ?? []);
$audit[] = [
'action' => 'clear_bmpa_error',
'scope' => 'single',
'at' => now()->toDateTimeString(),
'admin_id' => $admin->id,
'note' => '手动点击订单详情【清除 BMPA 失败标记】',
];
data_set($meta, 'audit', $audit);
$order->meta = $meta;
$order->save();
return redirect()->back()->with('success', '已清除该订单的 BMPA 失败标记。');
}
public function clearSyncErrors(Request $request): RedirectResponse
{
$this->ensurePlatformAdmin($request);

View File

@@ -368,6 +368,7 @@
@php
$activation = data_get($order->meta, 'subscription_activation');
$activationError = data_get($order->meta, 'subscription_activation_error');
$bmpaError = data_get($order->meta, 'batch_mark_paid_and_activate_error');
$audit = (array) (data_get($order->meta, 'audit', []) ?? []);
$paymentReceipts = (array) (data_get($order->meta, 'payment_receipts', []) ?? []);
$refundReceipts = (array) (data_get($order->meta, 'refund_receipts', []) ?? []);
@@ -588,6 +589,31 @@
@endif
</div>
<div class="card mb-20">
<div class="flex-between">
<h3>最近一次 BMPA 失败</h3>
@if($bmpaError)
<form method="post" action="/admin/platform-orders/{{ $order->id }}/clear-bmpa-error" onsubmit="return confirm('确认清除该订单的 BMPA 失败标记?该操作仅清理 meta 标记,不会改变订阅/订单状态。');">
@csrf
<button class="btn btn-danger btn-sm" type="submit">清除 BMPA 失败标记</button>
</form>
@endif
</div>
@if($bmpaError)
<table>
<tbody>
<tr><th class="w-160">失败原因</th><td>{{ data_get($bmpaError, 'message') }}</td></tr>
<tr><th>失败时间</th><td>{{ data_get($bmpaError, 'at') ?: '-' }}</td></tr>
<tr><th>操作管理员</th><td>{{ data_get($bmpaError, 'admin_id') ?: '-' }}</td></tr>
</tbody>
</table>
<div class="muted muted-xs mt-6">提示:当你已修复导致 BMPA 失败的原因(回执/退款/权限/幂等等),但历史失败标记仍残留时,可先清理标记,再重新执行 BMPA。</div>
@else
<p class="muted">暂无失败记录。</p>
@endif
</div>
<div class="card mb-20">
<h3>审计记录(最近 20 条)</h3>
@if(count($audit) > 0)
@@ -595,6 +621,7 @@
$auditItems = array_slice(array_reverse($audit), 0, 20);
$auditActionLabels = [
'clear_sync_error' => '清除同步失败标记',
'clear_bmpa_error' => '清除 BMPA 失败标记',
'batch_activate_subscription' => '批量同步订阅',
'mark_activated' => '仅标记为已生效',
'batch_mark_activated' => '批量仅标记为已生效',

View File

@@ -129,6 +129,7 @@ Route::prefix('admin')->group(function () {
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}/clear-sync-error', [PlatformOrderController::class, 'clearSyncError']);
Route::post('/platform-orders/{order}/clear-bmpa-error', [PlatformOrderController::class, 'clearBmpaError']);
Route::get('/site-subscriptions', [SiteSubscriptionController::class, 'index']);
Route::get('/site-subscriptions/export', [SiteSubscriptionController::class, 'export']);

View File

@@ -0,0 +1,77 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderClearBmpaErrorSingleTest 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_can_clear_single_order_bmpa_error_and_append_audit(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'clear_bmpa_error_single_test',
'name' => '清理单订单 BMPA 失败标记测试',
'billing_cycle' => 'monthly',
'price' => 9,
'list_price' => 9,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_CLEAR_BMPA_ERR_SINGLE_0001',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 9,
'paid_amount' => 0,
'placed_at' => now(),
'meta' => [
'batch_mark_paid_and_activate_error' => [
'message' => '历史 BMPA 失败',
'at' => now()->subMinutes(3)->toDateTimeString(),
'admin_id' => 1,
],
],
]);
$this->post('/admin/platform-orders/' . $order->id . '/clear-bmpa-error')
->assertRedirect();
$order->refresh();
$this->assertEmpty(data_get($order->meta, 'batch_mark_paid_and_activate_error'));
$audit = (array) (data_get($order->meta, 'audit', []) ?? []);
$this->assertNotEmpty($audit);
$last = end($audit);
$this->assertSame('clear_bmpa_error', data_get($last, 'action'));
$this->assertSame('single', data_get($last, 'scope'));
$this->assertNotEmpty(data_get($last, 'admin_id'));
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderShowHasClearBmpaErrorButtonTest 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_show_page_renders_clear_bmpa_error_button_when_error_exists(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'show_clear_bmpa_error_btn_test',
'name' => '详情页清理 BMPA 失败标记按钮测试',
'billing_cycle' => 'monthly',
'price' => 9,
'list_price' => 9,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_SHOW_CLEAR_BMPA_ERR_0001',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 9,
'paid_amount' => 0,
'placed_at' => now(),
'meta' => [
'batch_mark_paid_and_activate_error' => [
'message' => '历史 BMPA 失败',
'at' => now()->subMinutes(2)->toDateTimeString(),
'admin_id' => 1,
],
],
]);
$this->get('/admin/platform-orders/' . $order->id)
->assertOk()
->assertSee('/admin/platform-orders/' . $order->id . '/clear-bmpa-error', false)
->assertSee('清除 BMPA 失败标记');
}
}