AutoCloseOrderJob.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Order;
  4. use App\Http\Models\Payment;
  5. use Illuminate\Console\Command;
  6. use Log;
  7. use DB;
  8. class AutoCloseOrderJob extends Command
  9. {
  10. protected $signature = 'autoCloseOrderJob';
  11. protected $description = '自动关闭超时未支付订单';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. // 有赞云超过60分钟未支付则自动关闭,我们这则只给30分钟
  19. $paymentList = Payment::query()->where('status', 0)->where('created_at', '<=', date("Y-m-d H:i:s", strtotime("-30 minutes")))->get();
  20. if (!$paymentList->isEmpty()) {
  21. DB::beginTransaction();
  22. try {
  23. foreach ($paymentList as $payment) {
  24. Payment::query()->where('id', $payment->id)->update(['status' => -1]); // 关闭支付单
  25. Order::query()->where('oid', $payment->oid)->update(['status' => -1]); // 关闭订单
  26. //TODO:记录订单ID,去有赞关闭订单
  27. }
  28. DB::commit();
  29. } catch (\Exception $e) {
  30. Log::info('【异常】自动关闭超时未支付订单:' . $e->getMessage());
  31. DB::rollBack();
  32. }
  33. }
  34. Log::info('定时任务:' . $this->description);
  35. }
  36. }