Ver Fonte

feat: 抖音结算任务

xiansin há 2 anos atrás
pai
commit
13f06dcc3d

+ 2 - 0
server/app/Console/Kernel.php

xqd xqd
@@ -2,6 +2,7 @@
 
 namespace App\Console;
 
+use App\Jobs\ByteDanceSettlementJob;
 use Illuminate\Console\Scheduling\Schedule;
 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 
@@ -25,6 +26,7 @@ class Kernel extends ConsoleKernel
     protected function schedule(Schedule $schedule)
     {
         // $schedule->command('inspire')->hourly();
+        $schedule->job(ByteDanceSettlementJob::class)->dailyAt('00:00');
     }
 
     /**

+ 24 - 1
server/app/Helper/ByteDance.php

xqd xqd
@@ -80,6 +80,29 @@ class ByteDance extends BaseUniPlatform
         );
     }
 
+    /**
+     * 结算
+     * @param $outOrderNo
+     * @return array|mixed|string[]
+     * @throws \Exception
+     */
+    public function settle($outOrderNo)
+    {
+        $data = [
+            'app_id'        => $this->appId,
+            'out_settle_no' => $outOrderNo,
+            'out_order_no'  => $outOrderNo,
+            'settle_desc'   => '主动结算',
+        ];
+        $data = array_filter($data);
+        $data['sign'] = $this->getSign($data);
+
+        return $this->post(
+            $this->API::SETTLE,
+            $data
+        );
+    }
+
     /**
      * @param array  $data
      * @return string
@@ -220,7 +243,7 @@ class ByteDance extends BaseUniPlatform
             if(isset($res['err_code']) && !empty($res['err_code'])){
                 throw new \Exception("请求字节跳动API接口错误,错误码:{$res['err_msg']},错误信息:{$res['err_msg']}");
             }
-            return $res['data']?? [];
+            return $res['data']?? $res;
         } catch (GuzzleException $e) {
             \Log::error($e->getMessage());
             throw new \Exception($e->getMessage());

+ 1 - 0
server/app/Helper/UniPlatform/BaseAPI.php

xqd
@@ -8,4 +8,5 @@ abstract class BaseAPI
     const CREATE_ORDER = '';
     const ORDER_PUSH = '';
     const CREATE_QRCODE = '';
+    const SETTLE = '';
 }

+ 6 - 0
server/app/Helper/UniPlatform/Bytedance/ByteDanceAPI.php

xqd
@@ -38,4 +38,10 @@ final class ByteDanceAPI extends BaseAPI
      * @url https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/qr-code/create-qr-code
      */
     const CREATE_QRCODE =  PAY_URL.'/qrcode';
+
+    /**
+     * 结算
+     * @url https://developer.open-douyin.com/docs/resource/zh-CN/mini-app/develop/server/ecpay/settlements/settlement/
+     */
+    const SETTLE = PAY_URL.'/ecpay/v1/settle';
 }

+ 75 - 0
server/app/Jobs/ByteDanceSettlementJob.php

xqd
@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Helper\ByteDance;
+use App\Helper\UniPlatform\Bytedance\ByteDanceAPI;
+use App\Models\Pay;
+use App\Models\PayConfig;
+use Carbon\Carbon;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class ByteDanceSettlementJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $app = $this->getByteDanceFactory();
+
+        $orders = Pay::with(['user.info'])->whereHas('user.info', function ($query){
+            $query->where('platform', 1);
+        })->where('status', 1)
+            ->where('created_at', '<=',Carbon::now()->subDays(7)->toDateString().' 00:00:00')
+            ->where('is_settle', 0)
+            ->get()
+            ->chunk(30);
+        foreach ($orders as $arr){
+            /* @var Pay $order*/
+            foreach ($arr as $order){
+                try {
+                    $res = $app->settle($order->pay_id);
+                    $order->is_settle = 1;
+                    $order->settle_no = $res['settle_no'];
+                    $order->save();
+                    \Log::info("抖音结算--> 支付订单号:{$order->pay_id}\t结算单号:{$res['settle_no']}");
+                }catch (\Exception $e) {
+                    \Log::info("抖音结算错误--> 支付订单号:{$order->pay_id}\n错误信息:{$e->getMessage()}");
+                }
+            }
+            // 30QPS
+            sleep(1);
+        }
+
+    }
+
+    protected function getByteDanceFactory(): ByteDance
+    {
+        $setting = PayConfig::first();
+        return (new ByteDance(app(ByteDanceAPI::class)))->factory([
+            'app_id'     => $setting->douyin_app_id,
+            'app_secret' => $setting->douyin_app_secret,
+            'slat'       => $setting->douyin_salt,
+            'token'      => $setting->douyin_token,
+        ]);
+    }
+}

+ 4 - 0
server/app/Models/Pay.php

xqd
@@ -51,6 +51,10 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
  * @property string|null $token 支付token
  * @method static \Illuminate\Database\Eloquent\Builder|Pay whereToken($value)
  * @property-read \App\Models\User|null $user
+ * @property int|null $is_settle 是否结算
+ * @property string|null $settle_no 结算ID
+ * @method static \Illuminate\Database\Eloquent\Builder|Pay whereIsSettle($value)
+ * @method static \Illuminate\Database\Eloquent\Builder|Pay whereSettleNo($value)
  */
 class Pay extends Model
 {