美文网首页
php微信扫码支付

php微信扫码支付

作者: 七百年前 | 来源:发表于2017-06-08 15:44 被阅读159次

1.扫码支付

 /**
     * @author chantrans
     * 本页面的作用是生成商品二维码链接
     */
    echo '![模式一扫码支付](http://paysdk.weixin.qq.com/example/qrcode.php?data=' . createUrl()';

    /**
     * 二维码扫码链接构造方式:
     * 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。
     */
    function createUrl($productid)
    {

        $app_id     = "wx426b3015555a46be"; //公众号appid
        $app_key    = "8934e7d15453e97507ef794cf7b0519d"; //公众号支付请求中用于加密的密钥Key,可验证商户唯一身份,PaySignKey对应于支付场景中的appKey值。
        $mch_id     = "1900009851";
        $nonce_str  = createNoncestr();
        $time_stamp = time();
        //访问微信获得信息
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        $pp['appid']        = "wx426b3015555a46be"; //公众号appid
        $pp['mch_id']       = $mch_id;
        $pp['device_info']  = $productid;
        $pp['body']         = $productid;
        $pp['nonce_str']    = $nonce_str;
        $pp['out_trade_no'] = $productid;
        $pp['total_fee']    = 100;
        $pp['notify_url']   = "http://paysdk.weixin.qq.com/example/notify.php";
        $pp['trade_type']   = "NATIVE";
        $pp['time_stamp']   = $time_stamp;
        $pp['sign']         = getSign($pp, $app_key);

        $xml           = arrayToXml($pp);
        $xml_res       = postXmlCurl($xml, $url);
        $return_result = xmlToArray($xml_res); //把xml转为数组
        echo "<pre>";
        var_dump($return_result);
        return urlencode($return_result['code_url']);
        $wx['appid']     = $app_id;
$wx['timeStamp'] = time();
$wx['nonceStr']  = $nonce_str;
$wx['package']   = "prepay_id=" . $return_result['prepay_id'];
$wx['signType']  = "MD5";
$wx['paySign']   = getSign($pp, $app_key);
    }

    /**
     *  作用:生成签名
     */
    function getSign($Obj, $key)
    {
        foreach ($Obj as $k => $v) {
            $Parameters[$k] = $v;
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = 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_;
    }

    /**
     *  作用:格式化参数,签名过程需要使用
     */
    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位
     */
    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
     */
    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
     */
    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
     */
    function xmlToArray($xml)
    {
        //将XML转为array
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }

相关文章

网友评论

      本文标题:php微信扫码支付

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