where('paid_at', null)->chunk(100, function ($balances) { foreach ($balances as $balance) { if (!$this->checkAndCharge($balance, true)) { if (now()->diffInDays($balance->created_at) > 1) { $balance->delete(); } } } }); (new Balance)->where('paid_at', null)->where('created_at', '<', now()->subDays(2))->delete(); } public function checkAndCharge(Balance $balance, $check = false): bool { if ($check) { try { $alipay = Pay::alipay()->find(['out_trade_no' => $balance->order_id]); } catch (ContainerException|InvalidParamsException|ServiceNotFoundException) { return false; } if ($alipay->trade_status !== 'TRADE_SUCCESS') { return false; } } if ($balance->paid_at !== null) { return true; } $balance->update([ 'paid_at' => now(), ]); return true; } }