fix(billing): 分次退款累计达到全额应推进为已退款

This commit is contained in:
萝卜
2026-03-11 03:57:14 +00:00
parent e9b559436f
commit bf5f83df6c
2 changed files with 92 additions and 2 deletions

View File

@@ -549,8 +549,10 @@ class PlatformOrderController extends Controller
'last_channel' => (string) (data_get($latestRefund, 'channel') ?? ''), 'last_channel' => (string) (data_get($latestRefund, 'channel') ?? ''),
]); ]);
// 可治理辅助:自动推进退款标记(仅当订单本身已支付,且退款金额>0 时) // 可治理辅助:自动推进退款标记(仅当退款金额>0 时)
if ($order->payment_status === 'paid' && (float) $data['amount'] > 0) { // 注意:允许从 paid / partially_refunded 推进到 partially_refunded / refunded
// 且不会把已 refunded 的订单降级。
if ((float) $data['amount'] > 0 && in_array($order->payment_status, ['paid', 'partially_refunded'], true)) {
$paidAmount = (float) ($order->paid_amount ?? 0); $paidAmount = (float) ($order->paid_amount ?? 0);
// 退款总额 >= 已付金额 => 视为已退款;否则视为部分退款 // 退款总额 >= 已付金额 => 视为已退款;否则视为部分退款

View File

@@ -0,0 +1,88 @@
<?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 AdminPlatformOrderRefundReceiptAccumulateToFullyRefundedTest 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_refund_receipts_can_accumulate_and_push_status_to_refunded(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'refund_acc_test',
'name' => '退款累计到全额测试',
'billing_cycle' => 'monthly',
'price' => 30,
'list_price' => 30,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
$order = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_REFUND_ACC_0001',
'order_type' => 'new_purchase',
'status' => 'activated',
'payment_status' => 'paid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 30,
'paid_amount' => 30,
'placed_at' => now(),
'paid_at' => now(),
'activated_at' => now(),
]);
// 第一次退款 10 -> 部分退款
$this->post('/admin/platform-orders/' . $order->id . '/add-refund-receipt', [
'type' => 'refund',
'channel' => 'wechat',
'amount' => 10,
'refunded_at' => now()->format('Y-m-d H:i:s'),
'note' => '第一次退款',
])->assertRedirect();
$order->refresh();
$this->assertSame('partially_refunded', $order->payment_status);
// 第二次退款 20 -> 累计 30应推进到 refunded
$this->post('/admin/platform-orders/' . $order->id . '/add-refund-receipt', [
'type' => 'refund',
'channel' => 'wechat',
'amount' => 20,
'refunded_at' => now()->addMinute()->format('Y-m-d H:i:s'),
'note' => '第二次退款',
])->assertRedirect();
$order->refresh();
$this->assertSame('refunded', $order->payment_status);
$this->assertNotNull($order->refunded_at);
$refundSummaryTotal = (float) data_get($order->meta, 'refund_summary.total_amount');
$this->assertSame(30.0, $refundSummaryTotal);
}
}