ByteDanceSettlementJob.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Jobs;
  3. use App\Helper\ByteDance;
  4. use App\Helper\UniPlatform\Bytedance\ByteDanceAPI;
  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 ByteDanceSettlementJob 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->getByteDanceFactory();
  35. $orders = Pay::with(['user.info'])->whereHas('user.info', function ($query) {
  36. $query->where('platform', 1);
  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);
  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 getByteDanceFactory(): ByteDance
  60. {
  61. $setting = PayConfig::first();
  62. return (new ByteDance(app(ByteDanceAPI::class)))->factory([
  63. 'app_id' => $setting->douyin_app_id,
  64. 'app_secret' => $setting->douyin_app_secret,
  65. 'slat' => $setting->douyin_salt,
  66. 'token' => $setting->douyin_token,
  67. ]);
  68. }
  69. }