浏览代码

feat: 快手接口

xiansin 2 年之前
父节点
当前提交
3b5cfb706b

+ 1 - 2
mini/pages/login.vue

xqd
@@ -18,9 +18,8 @@ export default {
         const { token, user_info } = res.data
         await this.$store.dispatch('user/token', token)
         await this.$store.dispatch('user/info', user_info)
-        console.log('-->data', this.path)
         uni.reLaunch({
-          url: this.path
+          url: this.path.replace('//', '/')
         })
       })
     }

+ 30 - 0
mini/pages/my/index.vue

xqd xqd
@@ -109,6 +109,35 @@ export default {
       }
     },
     handleGetUserInfo() {
+      // #ifdef  MP-KUAISHOU
+      this.$loading('数据刷新中...')
+      uni.authorize({
+        scope: 'scope.userInfo',
+        success: () => {
+          uni.getUserInfo({
+            withCredentials: true,
+            success: res => {
+              this.getCode().then(code => {
+                const params = {
+                  encryptedData: res.encryptedData,
+                  iv: res.iv,
+                  signature: res.signature,
+                  code: code
+                }
+                this.$api.user.update(params).then(res => {
+                  this.code = null
+                  this.$hideLoading()
+                  this.$store.dispatch('user/info', res.data)
+                }).catch(() => {
+                  this.$hideLoading()
+                })
+              })
+            }
+          })
+        }
+      })
+      // #endif
+      // #ifdef  MP-TOUTIAO
       uni.getUserProfile({
         success: res => {
           this.getCode().then(code => {
@@ -129,6 +158,7 @@ export default {
           })
         }
       })
+      // #endif
     },
     getCode() {
       return new Promise(resolve => {

+ 4 - 4
server/app/Admin/Controllers/Setting/PayConfigController.php

xqd xqd
@@ -27,8 +27,8 @@ class PayConfigController extends AdminController
 
             $grid->column('kuaishou_app_id')->editable();
             $grid->column('kuaishou_app_secret')->display('*****');
-            $grid->column('kuaishou_salt')->display('*****');
-            $grid->column('kuaishou_token')->display('*****');
+//            $grid->column('kuaishou_salt')->display('*****');
+//            $grid->column('kuaishou_token')->display('*****');
 
             $grid->column('setting.is_watch_auto_pay',trans('setting.fields.is_watch_auto_pay'))->switch();
 
@@ -56,8 +56,8 @@ class PayConfigController extends AdminController
 
             $form->text('kuaishou_app_id')->required();
             $form->text('kuaishou_app_secret')->required();
-            $form->text('kuaishou_salt')->required();
-            $form->text('kuaishou_token')->required();
+//            $form->text('kuaishou_salt')->required();
+//            $form->text('kuaishou_token')->required();
 
             $form->switch('setting.is_watch_auto_pay',trans('setting.fields.is_watch_auto_pay'))->default(0);
 

+ 7 - 2
server/app/Helper/ByteDance.php

xqd xqd xqd
@@ -54,10 +54,11 @@ class ByteDance extends BaseUniPlatform
     /**
      * @param $outOrderNo
      * @param $totalAmount
+     * @param $openId
      * @return array|mixed
      * @throws \Exception
      */
-    public function createOrder($outOrderNo, $totalAmount): array
+    public function createOrder($outOrderNo, $totalAmount, $openId): array
     {
         $data = [
             'app_id'       => $this->appId,
@@ -84,7 +85,6 @@ class ByteDance extends BaseUniPlatform
      */
     public function getSign(array $data)
     {
-        $rList = array();
         foreach ($data as $k => $v) {
             if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
                 continue;
@@ -181,4 +181,9 @@ class ByteDance extends BaseUniPlatform
     {
         $this->accessTokenFile = storage_path('app/bytedance/bytedance_access_token.json');
     }
+
+    protected function setNoticeUrl(): void
+    {
+        $this->noticeUrl = env('APP_URL').'/api/pay/bytedance/notify';
+    }
 }

+ 79 - 8
server/app/Helper/Kuaishou.php

xqd xqd xqd
@@ -41,9 +41,56 @@ class Kuaishou extends BaseUniPlatform
     }
 
 
-    protected function createOrder($outOrderNo, $totalAmount): array
+    public function createOrder($outOrderNo, $totalAmount, $openId): array
     {
-        // TODO: Implement createOrder() method.
+        $data = [
+            'app_id'        => $this->appId,
+            'out_order_no'  => $outOrderNo,
+            'open_id'       => $openId,
+            'total_amount'  => intval($totalAmount * 100),
+            'subject'       => "订单号:".$outOrderNo,
+            'detail'        => '快手担保支付',
+            'type'          => 1233, // @url https://mp.kuaishou.com/docs/operate/platformAgreement/epayServiceCharge.html
+            'expire_time'   => $this->validTimestamp,
+            'notify_url'    => $this->noticeUrl
+        ];
+        $data['sign'] = $this->getSign($data);
+        $url = $this->API::CREATE_ORDER.'?'.http_build_query([
+                'app_id' => $this->appId,
+                'access_token' =>  $this->accessToken,
+            ]);
+        return $this->post(
+            $url,
+            $data,
+            'json'
+        );
+    }
+
+
+    /**
+     * @param array  $data
+     * @return string
+     */
+    public function getSign(array $data)
+    {
+        $filterArray = ['sign','access_token'];
+        $rList = array();
+        foreach ($data as $k => $v) {
+            if (in_array($k, $filterArray))
+                continue;
+            $value = trim(strval($v));
+            $len = strlen($value);
+            if ($len > 1 && substr($value, 0, 1) == "\"" && substr($value, $len, $len - 1) == "\"")
+                $value = substr($value, 1, $len - 1);
+            $value = trim($value);
+            if ($value == "" || $value == "null")
+                continue;
+            array_push($rList, "$k=$value");
+        }
+        sort($rList, SORT_STRING);
+        $str = implode('&', $rList);
+        $str .= $this->secret;
+        return md5($str);
     }
 
 
@@ -76,21 +123,39 @@ class Kuaishou extends BaseUniPlatform
         $this->accessTokenFile = storage_path('app/kuaishou/kuaishou_access_token.json');
     }
 
+
+    public function getNotifySign(array $data)
+    {
+        $str = json_encode($data,JSON_UNESCAPED_UNICODE).$this->secret;
+        return md5($str);
+    }
+
     /**
      * @param string $uri
      * @param array  $data
+     * @param string $type
      * @return array
      * @throws \Exception
      */
-    protected function post($uri = '', $data = []): array
+    protected function post($uri = '', $data = [], $type = 'urlencoded'): array
     {
         try {
             $client = new Client();
-            $url = $uri.'?'.http_build_query($data);
-            $res = $client->post($url, [
-                'verify'  => false,
-                'headers' => ['Content-Type' => 'x-www-form-urlencoded'],
-            ]);
+            if($type == 'urlencoded'){
+                $url = $uri.'?'.http_build_query($data);
+                $options = [
+                    'verify'  => false,
+                    'headers' => ['Content-Type' => 'x-www-form-urlencoded'],
+                ];
+            }else{
+                $url = $uri;
+                $options = [
+                    'verify'  => false,
+                    'headers' => ['Content-Type' => 'application/json'],
+                    'body' => json_encode($data)
+                ];
+            }
+            $res = $client->post($url, $options);
             $stringBody = (string)$res->getBody();
             $res = json_decode($stringBody, true);
 
@@ -103,4 +168,10 @@ class Kuaishou extends BaseUniPlatform
             throw new \Exception($e->getMessage());
         }
     }
+
+    protected function setNoticeUrl(): void
+    {
+        $this->noticeUrl = env('APP_URL').'/api/pay/kuaishou/notify';
+    }
+
 }

+ 9 - 4
server/app/Helper/UniPlatform/BaseUniPlatform.php

xqd xqd xqd xqd
@@ -41,14 +41,14 @@ abstract class BaseUniPlatform
     {
         $this->appId = $config['app_id'];
         $this->secret = $config['app_secret'];
-        $this->slat = $config['slat'];
-        $this->token = $config['token'];
+        $this->slat = isset($config['slat']) ? $config['slat'] : null;
+        $this->token = isset($config['token']) ?  $config['token'] : null;
 
         $this->setAccessFileDir();
         $this->setAccessFilePath();
+        $this->setNoticeUrl();
         $this->accessToken = $this->checkAccessToken();
 
-        $this->noticeUrl = env('APP_URL').'/api/pay/bytedance/notify';
         return $this;
     }
 
@@ -104,6 +104,7 @@ abstract class BaseUniPlatform
     }
 
 
+
     /**
      * 登陆
      * @param $code
@@ -128,11 +129,13 @@ abstract class BaseUniPlatform
 
     /**
      * 创建支付订单
+     *
      * @param $outOrderNo
      * @param $totalAmount
+     * @param $openId
      * @return array
      */
-    protected abstract function createOrder($outOrderNo, $totalAmount): array ;
+    abstract public function createOrder($outOrderNo, $totalAmount, $openId): array ;
 
     /**
      * 设置 access token 目录
@@ -143,4 +146,6 @@ abstract class BaseUniPlatform
      * 设置 access token路径
      */
     protected abstract function setAccessFilePath(): void;
+
+    protected abstract function setNoticeUrl(): void;
 }

+ 1 - 0
server/app/Helper/UniPlatform/Kuaishou/KuaishouAPI.php

xqd
@@ -5,6 +5,7 @@ use App\Helper\UniPlatform\BaseAPI;
 define('BASE_URL','https://open.kuaishou.com/oauth2');
 define('PAY_URL','https://open.kuaishou.com/openapi/mp/developer');
 
+
 final class KuaishouAPI extends BaseAPI
 {
 

+ 84 - 0
server/app/Helper/UniPlatform/Kuaishou/Payment.php

xqd
@@ -0,0 +1,84 @@
+<?php
+
+namespace App\Helper\UniPlatform\Kuaishou;
+
+use App\Helper\Kuaishou;
+use App\Models\Pay;
+use Carbon\Carbon;
+
+/**
+ * Class Payment
+ *
+ * @package App\Helper\Bytedance
+ * @property-read Kuaishou $app
+ */
+class Payment
+{
+
+    private $app;
+
+    private $messageId = '';
+
+    private $fail = null;
+
+
+    public function __construct(Kuaishou $app)
+    {
+        $this->app = $app;
+    }
+
+    /**
+     * 支付通知
+     * @param \Closure $closure
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function payNotify(\Closure $closure)
+    {
+        try {
+            call_user_func($closure, $this->getNoticeData() ,[$this, 'fail']);
+        }catch (\Exception $e){
+            $this->fail($e->getMessage());
+        }
+
+        return $this->toResponse();
+    }
+
+    public function fail($msg)
+    {
+        $this->fail = $msg;
+    }
+
+    /**
+     * 获取支付通知数据
+     * @return mixed
+     * @throws \Exception
+     */
+    private function getNoticeData()
+    {
+        $notify = \request()->all();
+        $kwaisign = \request()->header('kwaisign');
+
+        $this->messageId = $notify['message_id'];
+        //获取订单信息
+        \Log::info('快手支付回调==>'.json_encode($notify,JSON_UNESCAPED_SLASHES));
+        if($kwaisign !== $this->app->getNotifySign($notify)){
+            //throw new \Exception('签名验证错误');
+        }
+
+        return $notify['data'];
+    }
+
+    /**
+     * 返回数据
+     * @return \Illuminate\Http\JsonResponse
+     */
+    private function toResponse()
+    {
+        $data = [
+            'result'        => is_null($this->fail) ? 1  : 0,
+            'message_id'    => $this->messageId,
+            'fail'          => $this->fail
+        ];
+        return response()->json($data);
+    }
+}

+ 0 - 1
server/app/Http/Controllers/V1/AuthController.php

xqd
@@ -191,7 +191,6 @@ class AuthController extends Controller
             $data = [
                 'token' => "Bearer " . $token,
                 'user_info' => $user,
-                '$res' =>  $res
             ];
             return $this->success($data);
         } catch (\Exception $e) {

+ 0 - 2
server/app/Http/Controllers/V1/Controller.php

xqd
@@ -92,8 +92,6 @@ class Controller extends BaseController
         return (new Kuaishou(app(KuaishouAPI::class)))->factory([
             'app_id'     => $setting->kuaishou_app_id,
             'app_secret' => $setting->kuaishou_app_secret,
-            'slat'       => $setting->kuaishou_salt,
-            'token'      => $setting->kuaishou_token,
         ]);
     }
 

+ 37 - 0
server/app/Http/Controllers/V1/PayNoticeController.php

xqd xqd
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\V1;
 
 
 use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
+use App\Helper\UniPlatform\Kuaishou\Payment as KuaishouPayment;
 use App\Models\Pay;
 use App\Models\UserConsumeRecord;
 use App\Models\UserInfo;
@@ -48,9 +49,45 @@ class PayNoticeController extends Controller
 
     public function kuaishou()
     {
+        $app = new KuaishouPayment($this->getUniFactory(2));
+        return $app->payNotify(function ($data, $fail) {
+            //处理支付订单
+            try {
+                \DB::beginTransaction();
+                $payId = $data['out_order_no'];
+                $pay = Pay::find($payId);
+                if (!$pay) { // 如果订单不存在
+                    return true;
+                }
+                $payType = 11;
+                if($data['channel'] == 'WECHAT'){
+                    $payType = 1;
+                }
+                if($data['channel'] == 'ALIPAY'){
+                    $payType = 2;
+                }
+                $pay->pay_type = $data['channel'];
+                $pay->serial_number = $data['trade_no'];
+                $pay->status = $data['status'] == 'SUCCESS';
+                $pay->pay_dt = Carbon::now()->toDateString();
+                $pay->save();
+                // 处理
+                if ($pay->source == Pay::SOURCE_RECHARGE) {
+                    $this->recharge($payId);
+                } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
+                    $this->buyVip($payId);
+                }
 
+                \DB::commit();
+            } catch (\Exception $e) {
+                \DB::rollBack();
+                return $fail('通信失败,请稍后再通知我');
+            }
+            return true;
+        });
     }
 
+
     private function recharge($payId)
     {
         /* @var UserRechargeRecord $record */

+ 0 - 2
server/app/Http/Controllers/V1/User/RechargeController.php

xqd
@@ -74,11 +74,9 @@ class RechargeController extends Controller
             return $this->success($res);
         }catch (QueryException $e){
             \DB::rollBack();
-            dd($e->getMessage());
             return $this->error('下单失败');
         }catch (\Exception $e){
             \DB::rollBack();
-            dd($e->getMessage());
             return $this->error($e->getMessage());
         }
     }

+ 3 - 4
server/app/Http/Controllers/V1/UserController.php

xqd
@@ -94,15 +94,14 @@ class UserController extends Controller
         {
             $req = $request->all();
             $app = $this->getUniFactory(\user()->info->platform);
-
-            $res = $app->login($req['code']);
-
             /* @var User $user*/
             $user = User::with(['info'])->find(\user()->id);
-            $user->remember_token = $res['session_key'];
+            $res = $app->login($req['code']);
 
+            $user->remember_token = $res['session_key'];
             $decryptedData = $app->decryptData($user->remember_token, $req['iv'], $req['encryptedData']);
 
+
             if (isset($decryptedData['nickName'])) {
                 $user->nickname = $decryptedData['nickName'];
                 $user->avatar = $decryptedData['avatarUrl'];

+ 5 - 3
server/app/Models/Pay.php

xqd xqd
@@ -4,6 +4,7 @@ namespace App\Models;
 
 use App\Helper\ByteDance;
 use App\Helper\SerialNumber;
+use App\Helper\UniPlatform\BaseUniPlatform;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
@@ -66,17 +67,18 @@ class Pay extends Model
     ];
 
     /**
-     * @param ByteDance $byteDance
+     * @param BaseUniPlatform $byteDance
      * @param           $price
      * @param int       $source
      * @return array|mixed
      * @throws \Exception
      */
-    public function create(ByteDance $byteDance, float $price, int $source)
+    public function create(BaseUniPlatform $byteDance, float $price, int $source)
     {
         $payId = SerialNumber::createOrderId(\user()->id);
         // 字节跳动下单
-        $res = $byteDance->createOrder($payId, $price);
+        $res = $byteDance->createOrder($payId, $price, \user()->open_id);
+        dd($res);
         $res['pay_id'] = $payId;
 
         $pay = new Pay();

+ 1 - 4
server/config/global.php

xqd
@@ -39,9 +39,6 @@ return [
         7 => ['name' => '第七天', 'award' => 50],
     ],
     'pay_type' => [
-        1 => '微信支付', 2  => '支付宝支付', 10 => '抖音支付'
+        1 => '微信支付', 2  => '支付宝支付', 10 => '抖音支付', 11 => '快手支付'
     ],
-    'pay_source' => [
-        1 => '微信支付', 2  => '支付宝支付', 10 => '抖音支付'
-    ]
 ];

+ 1 - 0
server/routes/api.php

xqd
@@ -168,5 +168,6 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api)
 
     $api->get('pay/{pay_id}/query', 'PayController@query'); //字节跳动支付
     $api->any('pay/bytedance/notify', 'PayNoticeController@bytedance'); //字节跳动支付回调
+    $api->any('pay/kuaishou/notify', 'PayNoticeController@kuaishou'); //快手支付回调
 
 });