refactor: centralize receipt total logic on PlatformOrder

This commit is contained in:
萝卜
2026-03-11 06:41:36 +00:00
parent 174aaa5708
commit b4f1e194f6
4 changed files with 79 additions and 21 deletions

View File

@@ -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

View File

@@ -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++;

View File

@@ -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