PaymentPayTimeout.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Agent\AgentPaymentOrder;
  4. use App\Models\PaymentOrder;
  5. use App\Services\AdminRealtime\AdminRealtime;
  6. use App\Services\PaymentOrderService;
  7. use Illuminate\Console\Command;
  8. class PaymentPayTimeout extends Command
  9. {
  10. protected $signature = 'payment:pay-timeout';
  11. protected $description = '在线充值订单处理中超过30分钟未支付的标记为超时(迟到的支付回调仍可入账)';
  12. public function handle()
  13. {
  14. $timeoutAt = date('Y-m-d H:i:s', strtotime('-' . PaymentOrderService::PAY_TIMEOUT_MINUTES . ' minutes'));
  15. do {
  16. $ids = PaymentOrder::where('type', PaymentOrderService::TYPE_PAY)
  17. ->where('status', PaymentOrderService::STATUS_PROCESS)
  18. ->where('created_at', '<', $timeoutAt)
  19. ->limit(500)->pluck('id')->all();
  20. if (!$ids) break;
  21. // 带状态条件更新,避免覆盖同一时刻回调已改成成功/失败的订单
  22. PaymentOrder::whereIn('id', $ids)->where('status', PaymentOrderService::STATUS_PROCESS)
  23. ->update(['status' => PaymentOrderService::STATUS_TIMEOUT]);
  24. AdminRealtime::record('payment_orders', $ids);
  25. } while (count($ids) === 500);
  26. // 代理在线充值同样处理(不推送后台实时消息);迁移未执行前跳过。
  27. if (!\Illuminate\Support\Facades\DB::getSchemaBuilder()->hasTable('agent_payment_orders')) return 0;
  28. AgentPaymentOrder::query()->where('type', PaymentOrderService::TYPE_PAY)
  29. ->where('status', PaymentOrderService::STATUS_PROCESS)->where('created_at', '<', $timeoutAt)
  30. ->update(['status' => PaymentOrderService::STATUS_TIMEOUT, 'updated_at' => now()]);
  31. return 0;
  32. }
  33. }