微信扫码支付

<?php
include 'phpqrcode.php';
//生成随机字符串
function getNonceNum($numLen=16){
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $numLen; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
}
//获取用户ip地址
function get_client_ip(){
    $cip = "unknown";
    if($_SERVER['REMOTE_ADDR']){
        $cip = $_SERVER['REMOTE_ADDR'];
    }elseif(getenv("REMOTE_ADDR")){
        $cip = getenv("REMOTE_ADDR");
    }
    return $cip;
}


$nonce_str = getNonceNum();
$out_trade_no = date("Ymd").getNonceNum(10);
$ip = get_client_ip();


$param['appid']             = "###";
$param['mch_id']             = "###";
$param['nonce_str']         = "$nonce_str";
$param['body']                 = "扫码支付";
$param['out_trade_no']         = "$out_trade_no";
$param['total_fee']         = 1;
$param['spbill_create_ip']     = "$ip";
$param['notify_url']         = "###url###";
$param['trade_type']         = "NATIVE";

//排序
ksort($param);

$sign_raw = "";
foreach($param as $k => $v){
    $sign_raw .= $k."=".$v."&";
}
$sign_raw .= "key=###";


//生成签名
$sign = strtoupper(md5($sign_raw));

$xmlString = "<xml>";
foreach($param as $v => $k){
    $xmlString .="<$v>$k</$v>";
}
$xmlString .= "<sign>$sign</sign>";
$xmlString .= "</xml>";

$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

$result = postXmlCurl($xmlString,$url);

//解析xml
$xml = simplexml_load_string($result);
$bigarr = array();

//循环生成数组
foreach($xml->children() as $child){
    $key =  $child->getName();
    $bigarr["$key"] = "$child";//必须加引号
}
$tiaozhuanurl = $bigarr['code_url'];

/*
生成二维码
*/
$value = "$tiaozhuanurl"; //二维码内容
$errorCorrectionLevel = 'L';//容错级别
$matrixPointSize = 6;//生成图片大小
//生成二维码图片
QRcode::png($value, 'abc.png', $errorCorrectionLevel, $matrixPointSize, 2);
/*
$logo = 'logo.png';//准备好的logo图片
*/
$QR = 'abc.png';//已经生成的原始二维码图

if ($logo !== FALSE) {
    $QR = imagecreatefromstring(file_get_contents($QR));
    $logo = imagecreatefromstring(file_get_contents($logo));
    $QR_width = imagesx($QR);//二维码图片宽度
    $QR_height = imagesy($QR);//二维码图片高度
    $logo_width = imagesx($logo);//logo图片宽度
    $logo_height = imagesy($logo);//logo图片高度
    $logo_qr_width = $QR_width / 5;
    $scale = $logo_width/$logo_qr_width;
    $logo_qr_height = $logo_height/$scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;
    //重新组合图片并调整大小
    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
        $logo_qr_height, $logo_width, $logo_height);
}
//输出图片
imagepng($QR, 'abcd.png');
echo '<img src="abcd.png">';



//post请求,带上xml数据,post
function postXmlCurl($xml, $url, $useCert = false, $second = 30){        
    $ch = curl_init();
    //设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    
    
    curl_setopt($ch,CURLOPT_URL, $url);
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }else{
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
    } 
    //设置header
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if($useCert == true){
        //设置证书
        //使用证书:cert 与 key 分别属于两个.pem文件
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT, "../cert/###.pem");
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, "../cert/###.pem");
        
        curl_setopt($ch, CURLOPT_CAINFO, '../cert/##.pem'); 
    }
    //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);
        curl_close($ch);
        echo "error";
    }
}

 

posted on 2017-11-20 15:12  summerkxy  阅读(211)  评论(0编辑  收藏  举报

导航