From 5343a210b07b99a0dae20c3eba6b3601671f2c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Fri, 13 Mar 2026 13:37:32 +0000 Subject: [PATCH] platform orders: bmpa receipt mismatch safety valve uses configured tolerance --- .../Admin/PlatformOrderController.php | 6 +- ...MarkPaidAndActivateToleranceConfigTest.php | 93 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/AdminPlatformOrderBatchMarkPaidAndActivateToleranceConfigTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 793d8c7..ed5c2a5 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -1240,7 +1240,11 @@ class PlatformOrderController extends Controller $receiptCents = (int) round($receiptTotal * 100); $expectedCents = (int) round($expectedPaid * 100); - if (abs($receiptCents - $expectedCents) >= 1) { + $tol = (float) config('saasshop.amounts.tolerance', 0.01); + $tolCents = (int) round($tol * 100); + $tolCents = max(1, $tolCents); + + if (abs($receiptCents - $expectedCents) >= $tolCents) { throw new \InvalidArgumentException('订单回执总额与应付金额不一致,不允许批量推进,请先修正回执/金额后再处理。'); } } diff --git a/tests/Feature/AdminPlatformOrderBatchMarkPaidAndActivateToleranceConfigTest.php b/tests/Feature/AdminPlatformOrderBatchMarkPaidAndActivateToleranceConfigTest.php new file mode 100644 index 0000000..b396f36 --- /dev/null +++ b/tests/Feature/AdminPlatformOrderBatchMarkPaidAndActivateToleranceConfigTest.php @@ -0,0 +1,93 @@ +seed(); + + $this->post('/admin/login', [ + 'email' => 'platform.admin@demo.local', + 'password' => 'Platform@123456', + ])->assertRedirect('/admin'); + } + + public function test_batch_mark_paid_and_activate_uses_configured_tolerance_for_receipt_mismatch_safety_valve(): void + { + // 将容差放大到 0.05(5 分),以验证 safety valve 使用配置,而不是硬编码 0.01(1 分) + config()->set('saasshop.amounts.tolerance', 0.05); + + $this->loginAsPlatformAdmin(); + + $merchant = Merchant::query()->firstOrFail(); + $plan = Plan::query()->create([ + 'code' => 'bmpa_tol_test_plan', + 'name' => 'BMPA 容差配置测试套餐', + 'billing_cycle' => 'monthly', + 'price' => 10, + 'list_price' => 10, + 'status' => 'active', + 'sort' => 10, + 'published_at' => now(), + ]); + + // 应付 10.00,回执 10.02(差 2 分) + // 如果容差=5 分,则允许通过,不应写入 batch_mark_paid_and_activate_error + $order = PlatformOrder::query()->create([ + 'merchant_id' => $merchant->id, + 'plan_id' => $plan->id, + 'order_no' => 'PO_BMPA_TOL_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' => 10, + 'paid_amount' => 0, + 'placed_at' => now(), + 'meta' => [ + 'payment_receipts' => [ + [ + 'type' => 'manual', + 'channel' => 'test', + 'amount' => 10.02, + 'paid_at' => now()->toDateTimeString(), + 'note' => '回执略微偏差', + ], + ], + 'payment_summary' => [ + 'count' => 1, + 'total_amount' => 10.02, + 'last_at' => now()->toDateTimeString(), + 'last_amount' => 10.02, + 'last_channel' => 'test', + ], + ], + ]); + + $this->post('/admin/platform-orders/batch-mark-paid-and-activate', [ + 'scope' => 'filtered', + 'status' => 'pending', + 'payment_status' => 'unpaid', + 'limit' => 50, + ])->assertRedirect(); + + $order->refresh(); + + $this->assertEmpty(data_get($order->meta, 'batch_mark_paid_and_activate_error.message')); + $this->assertSame('paid', $order->payment_status); + $this->assertSame('activated', $order->status); + } +}