微信支付pc端掃碼支付 demo

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

// 定義時區
ini_set('date.timezone', 'Asia/Shanghai');

class Weixinpay
{
    // 定義配置項
    private $config = array(
        'APPID'      => '', // 微信支付APPID
        'MCHID'      => '', // 微信支付MCHID 商戶收款賬號
        'KEY'        => '', // 微信支付KEY
        'APPSECRET'  => '', //公衆帳號secert
        'NOTIFY_URL' => 'http://abc.com/Api/WeixPay/notify/order_number/', // 接收支付狀態的連接  改成自己的域名
    );

    // 構造函數
    public function __construct()
    {
        // 如果是在thinkphp中 那麼需要補全/Application/Common/Conf/config.php中的配置
        // 如果不是在thinkphp框架中使用;那麼註釋掉下面一行代碼;直接補全 private $config 即可
        $this->config = C('WEIXINPAY_CONFIG');
    }

    /**
     *
     * 產生隨機字符串,不長於32位
     * @param int $length
     * @return 產生的隨機字符串
     */
    public function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str   = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
    /**
     * 統一下單
     * @param  array $order 訂單 必須包含支付所需要的參數 body(產品描述)、total_fee(訂單金額)、out_trade_no(訂單號)、product_id(產品id)、trade_type(類型:JSAPI,NATIVE,APP)
     */
    public function unifiedOrder($order)
    {
        // 獲取配置項
        $weixinpay_config = $this->config;
        $config           = array(
            'appid'            => $weixinpay_config['APPID'],
            'mch_id'           => $weixinpay_config['MCHID'],
            // 'nonce_str'        => 'test',
            'nonce_str'        => $this->getNonceStr(),
            'spbill_create_ip' => get_client_ip(0, true),
            // 'spbill_create_ip' => '192.168.0.1',

            'notify_url'       => $weixinpay_config['NOTIFY_URL'],
        );

        // 合併配置數據和訂單數據
        $data = array_merge($order, $config);
        // 生成簽名
        $sign         = $this->makeSign($data);
        $data['sign'] = $sign;
        $xml          = $this->toXml($data);
        $url          = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //接收xml數據的文件
        $header[]     = "Content-type: text/xml"; //定義content-type爲xml,注意是數組
        $ch           = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 兼容本地沒有指定curl.cainfo路徑的錯誤
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            // 顯示報錯信息;終止繼續執行
            die(curl_error($ch));
        }
        curl_close($ch);
        $result = $this->toArray($response);
        // print_R($result);exit;
        // 顯示錯誤信息
        if ($result['return_code'] == 'FAIL') {
            die($result['return_msg']);
        }
        $result['sign'] = $sign;
        // $result['nonce_str'] = 'test';
        return $result;
    }

    /**
     * 驗證
     * @return array 返回數組格式的notify數據
     */
    public function notify()
    {
        // 獲取xml
        $xml = file_get_contents('php://input', 'r');
        // 轉成php數組
        $data = $this->toArray($xml);
        // 保存原sign
        $data_sign = $data['sign'];
        // sign不參與簽名
        unset($data['sign']);
        $sign = $this->makeSign($data);
        // 判斷簽名是否正確  判斷支付狀態
        if ($sign === $data_sign && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
            $result = $data;
        } else {
            $result = false;
        }
        // 返回狀態給微信服務器
        if ($result) {
            $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
        } else {
            $str = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[簽名失敗]]></return_msg></xml>';
        }
        echo $str;
        return $result;
    }

    /**
     * 輸出xml字符
     * @throws WxPayException
     **/
    public function toXml($data)
    {
        if (!is_array($data) || count($data) <= 0) {
            throw new WxPayException("數組數據異常!");
        }
        $xml = "<xml>";
        foreach ($data as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else {
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }

    /**
     * 生成簽名
     * @return 簽名,本函數不覆蓋sign成員變量,如要設置簽名需要調用SetSign方法賦值
     */
    public function makeSign($data)
    {
        // 去空
        $data = array_filter($data);
        //簽名步驟一:按字典序排序參數
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        //簽名步驟二:在string後加入KEY
        $config           = $this->config;
        $string_sign_temp = $string_a . "&key=" . $config['KEY'];
        //簽名步驟三:MD5加密
        $sign = md5($string_sign_temp);
        // 簽名步驟四:所有字符轉爲大寫
        $result = strtoupper($sign);
        return $result;
    }

    /**
     * 將xml轉爲array
     * @param  string $xml xml字符串
     * @return array       轉換得到的數組
     */
    public function toArray($xml)
    {
        //禁止引用外部xml實體
        libxml_disable_entity_loader(true);
        $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }

    public function GetOpenid()
    {
        // 獲取配置項
        $config = $this->config;
        // 如果沒有get參數沒有code;則重定向去獲取openid;
        if (!isset($_GET['code'])) {
            // 獲取訂單號
            $orderno = I('orderno');
            // 返回的url
            //觸發微信返回code碼
            $redirect_uri = urlencode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); //.$_SERVER['QUERY_STRING']
            // $redirect_uri = urlencode($redirect_uri);
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $config['APPID'] . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
            redirect($url);
        } else {
            // 如果有code參數;則表示獲取到openid
            $code = I('get.code');
            // 取出訂單號
            $out_trade_no = I('get.state', 0, 'intval');
            // 組合獲取prepay_id的url
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $config['APPID'] . '&secret=' . $config['APPSECRET'] . '&code=' . $code . '&grant_type=authorization_code';

            $ch = curl_init();
            //設置超時
            curl_setopt($ch, CURLOPT_TIMEOUT, 360000);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            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);
            $openid = $data['openid'];
            return $openid;
        }
    }
    /**
     * 獲取jssdk需要用到的數據
     * @return array jssdk需要用到的數據
     */
    public function getParameters()
    {
        // 獲取配置項
        $config = $this->config;
        // 如果沒有get參數沒有code;則重定向去獲取openid;
        if (!isset($_GET['code'])) {
            // 獲取訂單號
            $out_trade_no = I('get.out_trade_no', 1, 'intval');
            // 返回的url
            $redirect_uri = U('Api/Weixinpay/pay', '', '', true);
            $redirect_uri = urlencode($redirect_uri);
            $url          = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $config['APPID'] . '&redirect_uri=' . $redirect_uri . '&response_type=code&scope=snsapi_base&state=' . $out_trade_no . '#wechat_redirect';
            redirect($url);
        } else {
            // 如果有code參數;則表示獲取到openid
            $code = I('get.code');
            // 取出訂單號
            $out_trade_no = I('get.state', 0, 'intval');
            // 組合獲取prepay_id的url
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $config['APPID'] . '&secret=' . $config['APPSECRET'] . '&code=' . $code . '&grant_type=authorization_code';
            // curl獲取prepay_id
            $result = $this->curl_get_contents($url);
            $result = json_decode($result, true);
            $openid = $result['openid'];
            // 訂單數據  請根據訂單號out_trade_no 從數據庫中查出實際的body、total_fee、out_trade_no、product_id
            $order = array(
                'body'         => 'test', // 商品描述(需要根據自己的業務修改)
                'total_fee'    => 1, // 訂單金額  以(分)爲單位(需要根據自己的業務修改)
                'out_trade_no' => $out_trade_no, // 訂單號(需要根據自己的業務修改)
                'product_id'   => '1', // 商品id(需要根據自己的業務修改)
                'trade_type'   => 'JSAPI', // JSAPI公衆號支付
                'openid'       => $openid, // 獲取到的openid
            );
            // 統一下單 獲取prepay_id
            $unified_order = $this->unifiedOrder($order);
            // 獲取當前時間戳
            $time = time();
            // 組合jssdk需要用到的數據
            $data = array(
                'appId'     => $config['APPID'], //appid
                'timeStamp' => strval($time), //時間戳
                'nonceStr'  => $unified_order['nonce_str'], // 隨機字符串
                'package'   => 'prepay_id=' . $unified_order['prepay_id'], // 預支付交易會話標識
                'signType'  => 'MD5', //加密方式
            );
            // 生成簽名
            $data['paySign'] = $this->makeSign($data);
            return $data;
        }
    }

    /**
     * 生成支付二維碼
     * @param  array $order 訂單 必須包含支付所需要的參數 body(產品描述)、total_fee(訂單金額)、out_trade_no(訂單號)、product_id(產品id)、trade_type(類型:JSAPI,NATIVE,APP)
     */
    public function pay($order)
    {
        $result    = $this->unifiedOrder($order);
        $decodeurl = urldecode($result['code_url']);
        qrcode($decodeurl);
    }
    public function mweb($order)
    {
        $result       = $this->unifiedOrder($order);
        $redirect_uri = U('order/readx', '', '', true);
        $redirect_uri .= '?orderno=' . $order['out_trade_no'];
        $url = $result['mweb_url'];
        $url .= '&redirect_url=' . urlencode($redirect_uri);
        echo '<script>window.location.href="' . $url . '"</script>';
    }

    /**
     *
     * 獲取jsapi支付的參數
     * @param array $UnifiedOrderResult 統一支付接口返回的數據
     * @throws WxPayException
     *
     * @return json數據,可直接填入js函數作爲參數
     */
    public function GetJsApiParameters($OrderResult)
    {
        $config             = $this->config;
        $jsapi              = [];
        $jsapi['appId']     = $config['appid'];
        $timeStamp          = time();
        $jsapi['timeStamp'] = "$timeStamp";
        $jsapi['nonceStr']  = $this->getNonceStr();
        $jsapi['package']   = $OrderResult['prepay_id'];
        $jsapi['signType']  = 'MD5';
        $jsapi['paySign']   = $OrderResult['paySign'];
        $parameters         = json_encode($jsapi);
        return $parameters;
    }
    public function jspay($order)
    {
        $config = $this->config;
        $openId = $this->GetOpenid();
        $openId or exit('登錄失敗!');
        // 統一下單 獲取prepay_id
        $order['openid'] = $openId;
        $unified_order   = $this->unifiedOrder($order);
        // 獲取當前時間戳
        $time = time();
        // 組合jssdk需要用到的數據
        $data = array(
            'appId'     => $config['APPID'], //appid
            'timeStamp' => strval($time), //時間戳
            'nonceStr'  => $unified_order['nonce_str'], // 隨機字符串
            'package'   => 'prepay_id=' . $unified_order['prepay_id'], // 預支付交易會話標識
            'signType'  => 'MD5', //加密方式
        );
        // 生成簽名
        $data['paySign'] = $this->makeSign($data);
        $data            = json_encode($data);
        return $data;
    }

    /**
     * curl 請求http
     */
    public function curl_get_contents($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); //設置訪問的url地址
        // curl_setopt($ch,CURLOPT_HEADER,1);               //是否顯示頭部信息
        curl_setopt($ch, CURLOPT_TIMEOUT, 5); //設置超時
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); //用戶訪問代理 User-Agent
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']); //設置 referer
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //跟蹤301
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回結果
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章