refactor: centralize refund total and refund inconsistent logic on PlatformOrder

This commit is contained in:
萝卜
2026-03-11 06:25:43 +00:00
parent df3c16ce26
commit fb4d1fb99e
3 changed files with 181 additions and 24 deletions

View File

@@ -10,6 +10,41 @@ class PlatformOrder extends Model
{
use HasFactory;
public function refundTotal(): float
{
// 优先读扁平字段 refund_summary.total_amount
$total = data_get($this->meta, 'refund_summary.total_amount');
if ($total !== null) {
return (float) $total;
}
// 回退:遍历 refund_receipts[].amount
$refunds = (array) (data_get($this->meta, 'refund_receipts', []) ?? []);
$sum = 0.0;
foreach ($refunds as $r) {
$sum += (float) (data_get($r, 'amount') ?? 0);
}
return $sum;
}
public function isRefundInconsistent(): bool
{
// 口径与平台订单列表 refund_inconsistent 保持一致:按分取整 + 0.01 容差
$refundTotal = (float) $this->refundTotal();
$paidAmount = (float) ($this->paid_amount ?? 0);
$refundCents = (int) round($refundTotal * 100);
$paidCents = (int) round($paidAmount * 100);
if ((string) $this->payment_status === 'refunded') {
// 允许 0.01 容差refund_total + 0.01 < paid
return ($refundCents + 1) < $paidCents;
}
return $paidCents > 0 && $refundCents >= $paidCents;
}
protected $fillable = [
'merchant_id', 'plan_id', 'site_subscription_id', 'created_by_admin_id', 'order_no', 'order_type', 'status',
'payment_status', 'payment_channel', 'plan_name', 'billing_cycle', 'period_months', 'quantity', 'list_amount',