WIN-2308041133\Administrator 6 månader sedan
förälder
incheckning
87a73ba977
3 ändrade filer med 141 tillägg och 137 borttagningar
  1. 139 136
      application/api/controller/Wechat.php
  2. 1 1
      application/route.php
  3. 1 0
      extend/liuniu/WechatService.php

+ 139 - 136
application/api/controller/Wechat.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\admin\model\WechatPlan;
+use app\admin\model\WechatPlanRecord;
 use app\common\model\UserRelation;
 use app\common\controller\Api;
 use app\common\library\Auth;
@@ -99,144 +100,146 @@ class Wechat extends Api
             @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
         }
     }
-//    /**
-//     * 扣款服务
-//     * @param Request $request
-//     * @return mixed
-//     * @throws \think\db\exception\DataNotFoundException
-//     * @throws \think\db\exception\ModelNotFoundException
-//     * @throws \think\exception\DbException
-//     */
-//    public function signing(Request $request)
-//    {
-//        $peice = intval($request->param('peice'));
-//        $uid = intval($request->param('uid'));
-//        $plan_id = WechatPlan::where('price',$peice)->value('plan_id');
-//        $login_type = $request->param('login_type', 1);
-//        @file_put_contents("auth.txt", json_encode(input()));
-//        try {
-//
-////            $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口
-//            $app_id='wx5681205d1ef4d9d3';
-//            $mch_id ='';
-//            $sub_mch_id ='';
-//            $contract_code =$this->generateRandomString(6);
-//            $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . 12; //回调接口  $cid 企业id
-////            $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
+    /**
+     * 扣款服务
+     * @param Request $request
+     * @return mixed
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws \think\exception\DbException
+     */
+    public function signing(Request $request)
+    {
+        $peice = intval($request->param('peice'));
+        $uid = intval($request->param('uid'));
+        $plan_id = WechatPlan::where('price',$peice)->value('plan_id');
+        $login_type = $request->param('login_type', 1);
+        @file_put_contents("auth.txt", json_encode(input()));
+        try {
+
+//            $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . $cid; //回调接口
+            $app_id='wx5681205d1ef4d9d3';
+            $mch_id ='';
+            $sub_mch_id ='';
+            $contract_code =$this->generateRandomString(6);
+            $notify_url=Request::instance()->domain() . "/api/wechat/notify/" . 12; //回调接口  $cid 企业id
+//            $num = time() + mt_rand(10, 999999) . '' . substr($msec, 2, 3);//生成随机数
 //            $request_serial=$this->generateUniqueSerialNumber();
-//            $contract_display_account='咸宁红十字';
-//            $timestamp=time();
-//            $version=1.0;
-//            $array = array(
-//                'appid' => $app_id,
-//                'mch_id' => $mch_id,
-//                'plan_id' => $plan_id,
+            $request_serial=WechatPlanRecord::where('is_signing',0)->order('request_serial desc')->value('request_serial');
+            $contract_display_account='咸宁红十字';
+            $timestamp=time();
+            $version=1.0;
+            $array = array(
+                'appid' => $app_id,
+                'mch_id' => $mch_id,
+                'plan_id' => $plan_id,
 //                'sub_mch_id' => $sub_mch_id,
-//                'contract_code' => $contract_code,
-//                'notify_url' => $notify_url,
-//                'contract_display_account' => $contract_display_account,
-//                'request_serial' => $request_serial,
-//                'timestamp' => $timestamp,
-//               'version' => $version,
-//
-//            );
-//            $arr=ksort($array);
-//            $sign=$this->md5_sign($arr,'');
-//            $url=$this->OfficialAccountSigning($app_id,$mch_id,$plan_id,$sub_mch_id,$contract_code,$request_serial,$contract_display_account,$notify_url,$version,$sign,$timestamp);
-//            return $url;
-//        } catch (Exception $e) {
-//            @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
-//        }
-//    }
-//
-//    public function generateRandomString($length) {
-//        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-//        $charactersLength = strlen($characters);
-//        $randomString = '';
-//        for ($i = 0; $i < $length; $i++) {
-//            $randomString .= $characters[rand(0, $charactersLength - 1)];
-//        }
-//        return $randomString;
-//    }
-////    生成唯一序列号
-//    public function generateUniqueSerialNumber() {
-//        // 使用uniqid生成一个唯一ID,并去除前缀的'0'
-//        $uniqueId = ltrim(uniqid('', true), '0');
-//
-//        // 确保生成的ID不以0开头且长度不超过19位(int64的最大长度)
-//        while (strlen($uniqueId) > 19 || substr($uniqueId, 0, 1) === '0') {
-//            $uniqueId = ltrim(uniqid('', true), '0');
-//        }
-//
-//        // 将生成的ID转换为纯数字
-//        $serialNumber = preg_replace('/\D/', '', $uniqueId);
-//
-//        // 确保生成的序列号不以0开头
-//        if (substr($serialNumber, 0, 1) === '0') {
-//            $serialNumber = generateUniqueSerialNumber(); // 递归调用以重新生成
-//        }
-//
-//        return $serialNumber;
-//    }
-//
-//
-//    public function curl_post($url = '', $name = array(), $timeout = 100)
-//    {
-//        // var_dump($url);die();
-//        $ch = curl_init();
-//        curl_setopt($ch, CURLOPT_URL, $url);
-//        curl_setopt($ch, CURLOPT_HEADER, false); //是否显示头部
-//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否直接输出到屏幕
-//        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
-//        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
-//        curl_setopt($ch, CURLOPT_POST, true); //是否以post方式
-//        //设置post数据
-//        $post_data = json_encode($name);
-//        // curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
-//        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
-//        $content = curl_exec($ch);
-//        // var_dump($content);die();
-//        curl_close($ch);
-//        $content = json_decode($content, true);
-//        return $content;
-//
-//    }
-//    public function md5_sign($data, $key){
-//        $stringSignTemp=$data."&key=$key"; //注:key为商户平台设置的密钥key
-//
-//        $sign=MD5($stringSignTemp); //MD5加密
-//        $sign=strtoupper($sign); //大写
-//        return $sign;
-//    }
-//
-//    /**
-//     * 公众号签约
-//     * see:https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter5_1.shtml
-//     * @param array $data
-//     *
-//     * @return array
-//     * @throws AuthorizeFailedException
-//     */
-//    public function OfficialAccountSigning(string $appid,string $mch_id, string $plan_id,string $sub_mch_id, string $contract_code, string $request_serial, string $contract_display_account, string $notify_url, string $version, string $sign,string $timestamp): array
-//    {
-//
-//        $url = 'https://api.mch.weixin.qq.com/papay/partner/entrustweb';
-//        $info = [
-//            'appid' => $appid,  //应用ID
-//            'mch_id' => $mch_id,  //商户号
-//            'plan_id' => $plan_id,  //子商户号
-//            'sub_mch_id' => $sub_mch_id,  //模板id
-//            'contract_code' => $contract_code, //签约协议号
-//            'request_serial' => $request_serial, //请求序列号
-//            'contract_display_account' => $contract_display_account, //用户账户展示名称
-//            'notify_url' => $notify_url, //回调通知url
-//            'version' => $version, //版本号 固定值1.0
-//            'sign' => $sign, //签名
-//            'timestamp' => $timestamp
-//        ];
-//        $response = $this->curl_post($url, $info);
-//        return $response;
-//    }
+                'contract_code' => $contract_code,
+                'notify_url' => $notify_url,
+                'contract_display_account' => $contract_display_account,
+                'request_serial' => $request_serial,
+                'timestamp' => $timestamp,
+               'version' => $version,
+
+            );
+            $arr=ksort($array);
+            $sign=$this->md5_sign($arr,'192006250b4c09247ec02edce69f6a2d');
+            var_dump($array);die();
+            $url=$this->OfficialAccountSigning($app_id,$mch_id,$plan_id,$contract_code,$request_serial,$contract_display_account,$notify_url,$version,$sign,$timestamp);
+            return $url;
+        } catch (Exception $e) {
+            @file_put_contents("error.txt", $e->getFile() . '-', $e->getLine(), '-' . $e->getMessage());
+        }
+    }
+
+    public function generateRandomString($length) {
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+        $charactersLength = strlen($characters);
+        $randomString = '';
+        for ($i = 0; $i < $length; $i++) {
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
+        }
+        return $randomString;
+    }
+//    生成唯一序列号
+    public function generateUniqueSerialNumber() {
+        // 使用uniqid生成一个唯一ID,并去除前缀的'0'
+        $uniqueId = ltrim(uniqid('', true), '0');
+
+        // 确保生成的ID不以0开头且长度不超过19位(int64的最大长度)
+        while (strlen($uniqueId) > 19 || substr($uniqueId, 0, 1) === '0') {
+            $uniqueId = ltrim(uniqid('', true), '0');
+        }
+
+        // 将生成的ID转换为纯数字
+        $serialNumber = preg_replace('/\D/', '', $uniqueId);
+
+        // 确保生成的序列号不以0开头
+        if (substr($serialNumber, 0, 1) === '0') {
+            $serialNumber = generateUniqueSerialNumber(); // 递归调用以重新生成
+        }
+
+        return $serialNumber;
+    }
+
+
+    public function curl_post($url = '', $name = array(), $timeout = 100)
+    {
+        // var_dump($url);die();
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_HEADER, false); //是否显示头部
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//是否直接输出到屏幕
+        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
+        curl_setopt($ch, CURLOPT_POST, true); //是否以post方式
+        //设置post数据
+        $post_data = json_encode($name);
+        // curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
+        $content = curl_exec($ch);
+        // var_dump($content);die();
+        curl_close($ch);
+        $content = json_decode($content, true);
+        return $content;
+
+    }
+    public function md5_sign($data, $key){
+        $stringSignTemp=$data."&key=$key"; //注:key为商户平台设置的密钥key
+
+        $sign=MD5($stringSignTemp); //MD5加密
+        $sign=strtoupper($sign); //大写
+        return $sign;
+    }
+
+    /**
+     * 公众号签约
+     * see:https://pay.weixin.qq.com/wiki/doc/api/wxpay_v2/papay/chapter3_1.shtml
+     * @param array $data
+     *
+     * @return array
+     * @throws AuthorizeFailedException
+     */
+    public function OfficialAccountSigning(string $appid,string $mch_id, string $plan_id, string $contract_code, string $request_serial, string $contract_display_account, string $notify_url, string $version, string $sign,string $timestamp): array
+    {
+
+        $url = 'https://api.mch.weixin.qq.com/papay/entrustweb';
+        $info = [
+            'appid' => $appid,  //应用ID
+            'mch_id' => $mch_id,  //商户号
+            'plan_id' => $plan_id,  //模板id
+//            'sub_mch_id' => $sub_mch_id,  //子商户号
+            'contract_code' => $contract_code, //签约协议号
+            'request_serial' => $request_serial, //请求序列号
+            'contract_display_account' => $contract_display_account, //用户账户展示名称
+            'notify_url' => $notify_url, //回调通知url
+            'version' => $version, //版本号 固定值1.0
+            'sign' => $sign, //签名
+            'timestamp' => $timestamp
+        ];
+        $response = $this->curl_post($url, $info);
+        return $response;
+    }
     public function test() {
         return '123456';
     }

+ 1 - 1
application/route.php

@@ -44,7 +44,7 @@ Route::group('api', function () {
     Route::group('wechat', function () {
         Route::get('auth', 'api/wechat/auth');
         Route::get('wxconfig', 'api/Index/wxconfig');
-//        Route::get('signing', 'api/wechat/signing');
+        Route::get('signing', 'api/wechat/signing');
         Route::get('test', 'api/wechat/test');
     });
     Route::group('mini', function () {

+ 1 - 0
extend/liuniu/WechatService.php

@@ -351,6 +351,7 @@ class WechatService
                     Hook::exec("\\liuniu\\repositories\\PaymentRepositories", "wechat" . ucfirst($notify['attach']), $params);
                 }
                 $data = ['eventkey' => 'notify', 'command' => '', 'refreshtime' => time(), 'openid' => $notify['openid'], 'message' => json_encode($notify)];
+                @file_put_contents("quanju.txt", json_encode($data)."-微信支付成功回调接口\r\n", 8);
                 $wechatContext = WechatContext::create($data, true);
                 return true;
             }