chunk(100, function ($balances) { foreach ($balances as $balance) { if (!$this->checkAndCharge($balance, true)) { if (now()->diffInDays($balance->created_at) > 1) { $balance->delete(); } } } }); Balance::where('paid_at', null)->where('created_at', '<', now()->subDays(2))->delete(); } public function checkAndCharge(Balance $balance, $check = false): bool { if ($check) { $alipay = Pay::alipay()->find(['out_trade_no' => $balance->order_id]); if ($alipay->trade_status !== 'TRADE_SUCCESS') { return false; } } if ($balance->paid_at !== null) { return true; } try { (new Transaction)->addAmount($balance->user_id, 'alipay', $balance->amount); $balance->update([ 'paid_at' => now() ]); } catch (InvalidResponseException $e) { Log::error($e->getMessage()); return false; } catch (ChargeException $e) { Log::error($e->getMessage()); return false; } return true; } }