美文网首页
微信支付类

微信支付类

作者: 七百年前 | 来源:发表于2019-05-16 14:10 被阅读0次
    <?php
    
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    namespace app\tools;
    
    class Wxpay
    {
    
        public static $app_id       = '111';
        public static $mch_id       = '222';
        public static $app_secret   = '333';
        public static $app_key      = '444';
        public static $redirect_uri = 'www'; //授权之后跳转地址
    
        /**
         * 二维码扫码链接构造方式:
         * weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXXX&productid=XXXXXX×tamp=XXXXXX&noncestr=XXXXXX
         *
          appid 是字段名称:公众号id;字段来源:商户注册具有支付权限的公众号成功后即可获得;传入方式:由商户直接传入。
          timestamp 是字段名称:时间戳;字段来源:商户生成从1970 年1 月1 日00:00:00 至今的秒数,即当前的时间;由商户生成后传入。取值范围:32 字符以下
          noncestr 是字段名称:随机字符串;字段来源:商户生成的随机字符串;取值范围:长度为32 个字符以下。由商户生成后传入。取值范围:32 字符以下
          productid 是字段名称:商品唯一id;字段来源:商户需要定义并维护自己的商品id,这个id 与一张订单等价,微信后台凭借该id 通过Post商户后台获取交易必须信息。由商户生成后传入。取值范围:32字符以下
          sign 是字段名称:签名;字段来源:对前面的其他字段与appKey 按照字典序排序后,使用SHA1 算法得到的结果。由商户生成后传入。参与sign 签名的字段包括:appid、timestamp、noncestr、productid 以及appkey。
         */
        public static function createUrl($amount, $orderSN, $goodName = '套餐1', $note = '')
        {
            $nonce_str  = self::createNoncestr();
            $time_stamp = time();
            //访问微信获得信息
            $url        = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    
            $pp['appid']        = self::$app_id; //公众号appid
            $pp['mch_id']       = self::$mch_id;
            $pp['device_info']  = $orderSN;
            $pp['body']         = $goodName;
            $pp['nonce_str']    = $nonce_str;
            $pp['out_trade_no'] = $orderSN;
            $pp['total_fee']    = $amount * 100;
            $pp['notify_url']   = self::$redirect_uri;
            $pp['trade_type']   = "NATIVE";
            $pp['time_stamp']   = $time_stamp;
            $pp['sign']         = self::getSign($pp);
            // v($pp);
    
            $xml = self::arrayToXml($pp);
    
            $xml_res = self::postXmlCurl($xml, $url);
    
            $return_result = self::xmlToArray($xml_res); //把xml转为数组
            // v($return_result);
    
            return urlencode($return_result['code_url']);
        }
    
        /**
         * 微信js支付
         * @param type $param
         */
        public static function getJsApiParameters($orderSN = '11', $goodName = '11', $amount = '1')
        {
    
            $nonce_str  = self::createNoncestr();
            $time_stamp = time();
    //        var_dump($nonce_str);exit;
            //访问微信获得信息
            $url        = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            $openid     = self::getOpenId();
            var_dump($openid);
    
            $pp['appid']        = self::$app_id; //公众号appid
            $pp['mch_id']       = self::$mch_id;
            $pp['device_info']  = $orderSN;
            $pp['body']         = $goodName;
            $pp['nonce_str']    = $nonce_str;
            $pp['out_trade_no'] = $orderSN;
            $pp['total_fee']    = $amount * 100;
            $pp['notify_url']   = self::$redirect_uri;
            $pp['trade_type']   = "JSAPI";
            $pp['openid']       = $openid;
    
            $pp['time_stamp'] = $time_stamp;
            $pp['sign']       = self::getSign($pp);
            // v($pp);
    
            $xml = self::arrayToXml($pp);
    
            $xml_res = self::postXmlCurl($xml, $url);
    
            $return_result = self::xmlToArray($xml_res); //把xml转为数组
            var_dump($return_result);
            exit;
    
            return urlencode($return_result['code_url']);
        }
    
        /**
         * 获取openid
         * @return type
         */
        public static function getOpenId()
        {
            $code = $_GET['code'];
            if ($code) {
                $urlObj["appid"]      = self::$app_id;
                $urlObj["secret"]     = self::$app_secret;
                $urlObj["code"]       = $code;
                $urlObj["grant_type"] = "authorization_code";
                $bizString            = self::ToUrlParams($urlObj);
                $url                  = "https://api.weixin.qq.com/sns/oauth2/access_token?{$bizString}";
                //获取数据
                $re                   = self::getOpenArrByCode($url);
                return $re['open_id'];
            } else {
                $redirect_uri            = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING'];
                $urlObj["appid"]         = self::$app_id;
                $urlObj["redirect_uri"]  = $redirect_uri;
                $urlObj["response_type"] = "code";
                $urlObj["scope"]         = "snsapi_base";
                $urlObj["state"]         = "STATE" . "#wechat_redirect";
                $bizString               = self::ToUrlParams($urlObj);
                $url                     = "https://open.weixin.qq.com/connect/oauth2/authorize?{$bizString}";
                Header("Location: $url");
                exit;
            }
        }
    
        /**
         * 根据凑得获取 openid
         * @param type $url
         * @return type
         */
        public static function getOpenArrByCode($url)
        {
            //初始化curl
            $ch          = curl_init();
            $curlVersion = curl_version();
    
            $ua = "WXPaySDK/3.0.9 (" . PHP_OS . ") PHP/" . PHP_VERSION . " CURL/" . $curlVersion['version'] . " " . self::$mch_id;
    
            //设置超时
            curl_setopt($ch, CURLOPT_TIMEOUT, 600);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_USERAGENT, $ua);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
            //运行curl,结果以jason形式返回
            $res  = curl_exec($ch);
            curl_close($ch);
            //取出openid
            $data = json_decode($res, true);
            return $data;
        }
    
        /**
         * 
         * 拼接签名字符串
         * @param array $urlObj
         * 
         * @return 返回已经拼接好的字符串
         */
        public static function ToUrlParams($urlObj)
        {
            $buff = "";
            foreach ($urlObj as $k => $v) {
                if ($k != "sign") {
                    $buff .= $k . "=" . $v . "&";
                }
            }
    
            $buff = trim($buff, "&");
            return $buff;
        }
    
        /**
         *  作用:生成签名
         */
        public static function getSign($Obj)
        {
            $key = self::$app_key;
            foreach ($Obj as $k => $v) {
                $Parameters[$k] = $v;
            }
            //签名步骤一:按字典序排序参数
            ksort($Parameters);
            $String  = self::formatBizQueryParaMap($Parameters, false);
            //echo '【string1】'.$String.'</br>';
            //签名步骤二:在string后加入KEY
            $String  = $String . "&key=" . $key;
            //echo "【string2】".$String."</br>";
            //签名步骤三:MD5加密
            $String  = md5($String);
            //echo "【string3】 ".$String."</br>";
            //签名步骤四:所有字符转为大写
            $result_ = strtoupper($String);
            //echo "【result】 ".$result_."</br>";
            return $result_;
        }
    
        /**
         *  作用:格式化参数,签名过程需要使用
         */
        public static function formatBizQueryParaMap($paraMap, $urlencode)
        {
            $buff = "";
            ksort($paraMap);
            foreach ($paraMap as $k => $v) {
                if ($urlencode) {
                    $v = urlencode($v);
                }
                // $buff .= strtolower($k) . "=" . $v . "&";
                $buff .= $k . "=" . $v . "&";
            }
            $reqPar;
            if (strlen($buff) > 0) {
                $reqPar = substr($buff, 0, strlen($buff) - 1);
            }
            return $reqPar;
        }
    
        /**
         *  作用:产生随机字符串,不长于32位
         */
        public static function createNoncestr($length = 32)
        {
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
            $str   = "";
            for ($i = 0; $i < $length; $i++) {
                $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            }
            return $str;
        }
    
        /**
         *  作用:array转xml
         */
        public static function arrayToXml($arr)
        {
            $xml = "<xml>";
            foreach ($arr as $key => $val) {
                if (is_numeric($val)) {
                    $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
                } else {
                    $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
                }
            }
            $xml .= "</xml>";
            return $xml;
        }
    
        /**
         *  作用:以post方式提交xml到对应的接口url
         */
        public static function postXmlCurl($xml, $url, $second = 30)
        {
            //初始化curl
            $ch   = curl_init();
            //设置超时
            curl_setopt($ch, CURLOPT_TIMEOUT, $second);
            //这里设置代理,如果有的话
            //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
            //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验2
            //设置header
            curl_setopt($ch, CURLOPT_HEADER, false);
            //要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //post提交方式
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            //运行curl
            $data = curl_exec($ch);
            //返回结果
            if ($data) {
                curl_close($ch);
                return $data;
            } else {
                $error = curl_errno($ch);
                echo "curl出错,错误码:$error" . "<br>";
                echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
                curl_close($ch);
                return false;
            }
        }
    
        /**
         *  作用:将xml转为array
         */
        public static function xmlToArray($xml)
        {
            //将XML转为array
            $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            return $array_data;
        }
    
        /**
         * 微信支付回调
         * @param Request $request
         */
        public static function wxpayNotify()
        {
            $data    = $request->all();
            $postStr = file_get_contents("php://input");
            $xmlArr  = self::xmlToArray($postStr);
            saveLog(json_encode($xmlArr), 'wxpay');
    
            if ($xmlArr['result_code'] == 'SUCCESS' && $xmlArr['return_code'] == 'SUCCESS') {
    
                $out_trade_no = $xmlArr['out_trade_no'];
                $total_feel   = $xmlArr['total_fee'] / 100;
                $trade_no     = $xmlArr['transaction_id'];
                $sign         = $xmlArr['sign'];
                unset($xmlArr['sign']);
                $tmpSign      = self::getSign($xmlArr);
                if ($out_trade_no && $total_feel && $tmpSign == $sign) {
                    Userorder::upPayOrder($out_trade_no, $total_feel, $trade_no, $xmlArr, 2);
                }
                echo "success";
            }
    
            echo "fail";
        }
    
    }
    

    相关文章

      网友评论

          本文标题:微信支付类

          本文链接:https://www.haomeiwen.com/subject/fnhiaqtx.html