ByteDanceSettlementJob.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct()
  22. {
  23. //
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $app = $this->getByteDanceFactory();
  33. $orders = Pay::with(['user.info'])->whereHas('user.info', function ($query){
  34. $query->where('platform', 1);
  35. })->where('status', 1)
  36. ->where('created_at', '<=',Carbon::now()->subDays(7)->toDateString().' 00:00:00')
  37. ->where('is_settle', 0)
  38. ->get()
  39. ->chunk(30);
  40. foreach ($orders as $arr){
  41. /* @var Pay $order*/
  42. foreach ($arr as $order){
  43. try {
  44. $res = $app->settle($order->pay_id);
  45. $order->is_settle = 1;
  46. $order->settle_no = $res['settle_no'];
  47. $order->save();
  48. \Log::info("抖音结算--> 支付订单号:{$order->pay_id}\t结算单号:{$res['settle_no']}");
  49. }catch (\Exception $e) {
  50. \Log::info("抖音结算错误--> 支付订单号:{$order->pay_id}\n错误信息:{$e->getMessage()}");
  51. }
  52. }
  53. // 30QPS
  54. sleep(1);
  55. }
  56. }
  57. protected function getByteDanceFactory(): ByteDance
  58. {
  59. $setting = PayConfig::first();
  60. return (new ByteDance(app(ByteDanceAPI::class)))->factory([
  61. 'app_id' => $setting->douyin_app_id,
  62. 'app_secret' => $setting->douyin_app_secret,
  63. 'slat' => $setting->douyin_salt,
  64. 'token' => $setting->douyin_token,
  65. ]);
  66. }
  67. }