KuaishouSettlementJob.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Jobs;
  3. use App\Helper\Kuaishou;
  4. use App\Helper\UniPlatform\Kuaishou\KuaishouAPI;
  5. use App\Models\Pay;
  6. use App\Models\PayConfig;
  7. use Carbon\Carbon;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. class KuaishouSettlementJob implements ShouldQueue
  14. {
  15. use Dispatchable;
  16. use InteractsWithQueue;
  17. use Queueable;
  18. use SerializesModels;
  19. /**
  20. * Create a new job instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. }
  27. /**
  28. * Execute the job.
  29. *
  30. * @return void
  31. */
  32. public function handle()
  33. {
  34. $app = $this->getKuishouFactory();
  35. $orders = Pay::with(['user.info'])->whereHas('user.info', function ($query) {
  36. $query->where('platform', 2);
  37. })->where('status', 1)
  38. ->where('created_at', '<=', Carbon::now()->subDays(7)->toDateString() . ' 00:00:00')
  39. ->where('is_settle', 0)
  40. ->get()
  41. ->chunk(30);
  42. foreach ($orders as $arr) {
  43. /* @var Pay $order */
  44. foreach ($arr as $order) {
  45. try {
  46. $res = $app->settle($order->pay_id, $order->order_fee * 100);
  47. $order->is_settle = 1;
  48. $order->settle_no = $res['settle_no'];
  49. $order->save();
  50. \Log::info("快手结算--> 支付订单号:{$order->pay_id}\t结算单号:{$res['settle_no']}");
  51. } catch (\Exception $e) {
  52. \Log::info("快手结算错误--> 支付订单号:{$order->pay_id}\n错误信息:{$e->getMessage()}");
  53. }
  54. }
  55. // 30QPS
  56. sleep(1);
  57. }
  58. }
  59. protected function getKuishouFactory(): Kuaishou
  60. {
  61. $setting = PayConfig::first();
  62. return (new Kuaishou(app(KuaishouAPI::class)))->factory([
  63. 'app_id' => $setting->kuaishou_app_id,
  64. 'app_secret' => $setting->kuaishou_app_secret,
  65. ]);
  66. }
  67. }