微信二维码付款

$url = $obj->paying($fee,$apiSecret,$trade_no);

<?php
class pay{

public function paying($fee,$apiSecret,$trade_no)
{
$api='https://api.mch.weixin.qq.com/pay/unifiedorder';
//构造参数
$randString=md5(uniqid());
$randOrder='000'.rand(1,1000000);
$data=[
'appid'=>'wxb0e9db1233eeb7b7',
'mch_id'=>'1435153802',
'nonce_str'=>$randString,
'body'=>'付款',
'out_trade_no'=>$trade_no,
'total_fee'=>$fee*100,//单位是分
'spbill_create_ip'=>'127.0.0.1',//终端ip
'notify_url'=>'http://t.51ao.com/wap/paynotice.php',//通知地址
//'notify_url'=>'http://t.51ao.com/wap/1.php',//通知地址
'trade_type'=>'NATIVE',
];
ksort($data);//以键名排序
$string='';
foreach($data as $k=>$v)
{
    $string.="$k=$v&";
}
//把秘钥连接到参数上
$string.='key='.$apiSecret;
//把链接的字符串全部大写再MD5加密
$sign=strtoupper(md5($string));
//把参数和签名放到一起
$data['sign']=$sign;
//参数转成xml
$xml='<xml>';
foreach($data as $k=>$v)
{
    $xml.="<$k>$v</$k>";
}
$xml.='</xml>';
//var_dump($xml);

$ret=$this->post($api,$xml);
//解析xml
$xml=simplexml_load_string($ret);
if($xml->return_code=='SUCCESS')
{
    //把code_url转化成二维码图片
    // require('./phpqrcode.php');//包含类库文件
    //import('source.class.phpqrcode');
    return $xml->code_url;
    //QRCode::png($xml->code_url);

    }else
    {

        die($xml);
    }
}
// var_dump($ret);

// 发POST请求
public function post1( $url,$xml,$second = 30)
{
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        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_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        $data = curl_exec($ch);
        curl_close($ch);

        if ($data) {
            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>';
            return false;
        }
 }

public function post($url, $data = '', $isHttps = TRUE)
{
    // 创建curl对象
    $ch = curl_init ();
    // 配置这个对象
    curl_setopt ( $ch, CURLOPT_URL, $url);  // 请求的URL地址
    if($isHttps)
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
    }
    curl_setopt ( $ch, CURLOPT_POST, 1);  // 是否是POST请求
    curl_setopt ( $ch, CURLOPT_HEADER, 0);  // 去掉HTTP协议头
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1);  // 返回接口的结果,而不是输出
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);  // 提交的数据
    // 发出请求
    $return = curl_exec ( $ch );
    // 关闭对象
    curl_close ( $ch );
    // 返回数据
    return $return;
 }
}

 

posted @ 2017-08-03 17:44  6671  阅读(192)  评论(0编辑  收藏  举报