KuaishouSettlementJob.php 2.2 KB

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