refactor: centralize receipt total logic on PlatformOrder
This commit is contained in:
@@ -1470,20 +1470,8 @@ class PlatformOrderController extends Controller
|
||||
|
||||
private function receiptTotalForOrder(PlatformOrder $order): float
|
||||
{
|
||||
// 优先读扁平字段 payment_summary.total_amount(更稳定、避免遍历 receipts)
|
||||
$total = (float) (data_get($order->meta, 'payment_summary.total_amount') ?? 0);
|
||||
if ($total > 0) {
|
||||
return $total;
|
||||
}
|
||||
|
||||
// 回退:遍历 payment_receipts[].amount
|
||||
$receipts = (array) (data_get($order->meta, 'payment_receipts', []) ?? []);
|
||||
$sum = 0.0;
|
||||
foreach ($receipts as $r) {
|
||||
$sum += (float) (data_get($r, 'amount') ?? 0);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
// 口径统一:集中到模型方法,避免多处复制导致漂移
|
||||
return (float) $order->receiptTotal();
|
||||
}
|
||||
|
||||
private function refundTotalForOrder(PlatformOrder $order): float
|
||||
|
||||
@@ -62,13 +62,7 @@ class SiteSubscriptionController extends Controller
|
||||
foreach ($metaOrders as $o) {
|
||||
$meta = $o->meta ?? [];
|
||||
|
||||
$receiptTotal = (float) (data_get($meta, 'payment_summary.total_amount') ?? 0);
|
||||
if ($receiptTotal <= 0) {
|
||||
$receipts = (array) (data_get($meta, 'payment_receipts', []) ?? []);
|
||||
foreach ($receipts as $r) {
|
||||
$receiptTotal += (float) (data_get($r, 'amount') ?? 0);
|
||||
}
|
||||
}
|
||||
$receiptTotal = (float) $o->receiptTotal();
|
||||
|
||||
if ($receiptTotal > 0) {
|
||||
$receiptOrders++;
|
||||
|
||||
@@ -10,6 +10,24 @@ class PlatformOrder extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public function receiptTotal(): float
|
||||
{
|
||||
// 优先读扁平字段 payment_summary.total_amount(更稳定、避免遍历 receipts)
|
||||
$total = (float) (data_get($this->meta, 'payment_summary.total_amount') ?? 0);
|
||||
if ($total > 0) {
|
||||
return $total;
|
||||
}
|
||||
|
||||
// 回退:遍历 payment_receipts[].amount
|
||||
$receipts = (array) (data_get($this->meta, 'payment_receipts', []) ?? []);
|
||||
$sum = 0.0;
|
||||
foreach ($receipts as $r) {
|
||||
$sum += (float) (data_get($r, 'amount') ?? 0);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
public function refundTotal(): float
|
||||
{
|
||||
// 优先读扁平字段 refund_summary.total_amount
|
||||
|
||||
58
tests/Unit/PlatformOrderReceiptTotalTest.php
Normal file
58
tests/Unit/PlatformOrderReceiptTotalTest.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\Merchant;
|
||||
use App\Models\Plan;
|
||||
use App\Models\PlatformOrder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PlatformOrderReceiptTotalTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_receipt_total_can_fallback_to_receipts_sum_when_payment_summary_missing(): void
|
||||
{
|
||||
$merchant = Merchant::query()->create([
|
||||
'name' => '单测站点_receipt',
|
||||
'slug' => 'unit-test-merchant-receipt',
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$plan = Plan::query()->create([
|
||||
'code' => 'unit_test_plan_receipt_total',
|
||||
'name' => '单测套餐_receipt',
|
||||
'billing_cycle' => 'monthly',
|
||||
'price' => 10,
|
||||
'list_price' => 10,
|
||||
'status' => 'active',
|
||||
'sort' => 10,
|
||||
'published_at' => now(),
|
||||
]);
|
||||
|
||||
$order = PlatformOrder::query()->create([
|
||||
'merchant_id' => $merchant->id,
|
||||
'plan_id' => $plan->id,
|
||||
'order_no' => 'PO_UNIT_RECEIPT_TOTAL_0001',
|
||||
'order_type' => 'subscription',
|
||||
'status' => 'activated',
|
||||
'payment_status' => 'paid',
|
||||
'plan_name' => 'Unit Test Plan',
|
||||
'billing_cycle' => 'monthly',
|
||||
'period_months' => 1,
|
||||
'quantity' => 1,
|
||||
'payable_amount' => 10,
|
||||
'paid_amount' => 10,
|
||||
'placed_at' => now(),
|
||||
'meta' => [
|
||||
'payment_receipts' => [
|
||||
['amount' => 3.25],
|
||||
['amount' => 1.15],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(4.40, $order->receiptTotal());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user