微信h5支付

<?php



    const APPID = ‘‘;
    //受理商ID,身份标识
    const MCHID = ‘‘;
    //商户支付密钥Key。审核通过后,在微信发送的邮件中查看
    const KEY = ‘‘;


    // echo get_client_ip();
    // die;

    $data[‘appid‘]=APPID;                               //微信分配的公众账号ID
    $data[‘mch_id‘]=MCHID;                              //微信支付分配的商户号
    $data[‘time_stamp‘]=time();                         //系统当前时间,定义规则详见时间戳
    $data[‘nonce_str‘]=uniqid();                        //随机字符串,不长于32位。推荐随机数生成算法
    $data[‘out_trade_no‘]=time().rand(‘1111‘,‘9999‘);     //商户定义的商品id 或者订单号
    $data[‘body‘]=‘龙测试‘;                             //商品信息
    $data[‘trade_type‘]=‘MWEB‘;                       //交易类型
    $data[‘total_fee‘]=1;                               //支付金额          单位分
    $data[‘spbill_create_ip‘]=‘117.23.83.120‘;
    $data[‘notify_url‘]=‘www.guomeng.group/index/way_login/pay‘;        //外网地址
    $data[‘scene_info‘]=‘{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "腾讯充值"}}‘;
    
    
          
    $sign=createSign($data);               
    $data[‘sign‘]=$sign;                                 //签名
    $data=dataToXml($data);                               //转xml
    $request=request($url=‘https://api.mch.weixin.qq.com/pay/unifiedorder‘,$https=true,$method=‘post‘,$data);               //将数据发送到微信服务器

    $obj=simplexml_load_string($request);

    $code_url=$obj->mweb_url;
    $add_url=urlencode(‘http://baidu.com‘);
    $code_url=$code_url.‘&‘.$add_url;

    echo ‘<a href="‘.$code_url.‘">跳转</a>‘;
    die;

    //header( "Location: $code_url" ); 



    //生成签名
    function createSign($data){
        $string=‘‘;
        ksort($data);
        foreach($data as $key=>$value){
            $string.="{$key}={$value}&";
        }
        $string.=‘key=‘.KEY;
        return strToUpper(md5($string));
    }

    //转xml
    function dataToXml($data){
        $xml=‘<xml>‘;
        foreach($data as $key=>$value){
            $xml.="<{$key}>{$value}</{$key}>";
        }
        $xml.=‘</xml>‘;
        return $xml;
    }

    //curl发送请求
    function request($url,$https,$method,$data){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($https==true){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        if($method==‘post‘){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }


    function get_client_ip() {
        if(getenv(‘HTTP_CLIENT_IP‘) && strcasecmp(getenv(‘HTTP_CLIENT_IP‘), ‘unknown‘)) {
            $ip = getenv(‘HTTP_CLIENT_IP‘);
        } elseif(getenv(‘HTTP_X_FORWARDED_FOR‘) && strcasecmp(getenv(‘HTTP_X_FORWARDED_FOR‘), ‘unknown‘)) {
            $ip = getenv(‘HTTP_X_FORWARDED_FOR‘);
        } elseif(getenv(‘REMOTE_ADDR‘) && strcasecmp(getenv(‘REMOTE_ADDR‘), ‘unknown‘)) {
            $ip = getenv(‘REMOTE_ADDR‘);
        } elseif(isset($_SERVER[‘REMOTE_ADDR‘]) && $_SERVER[‘REMOTE_ADDR‘] && strcasecmp($_SERVER[‘REMOTE_ADDR‘], ‘unknown‘)) {
            $ip = $_SERVER[‘REMOTE_ADDR‘];
        }
        return preg_match ( ‘/[\d\.]{7,15}/‘, $ip, $matches ) ? $matches [0] : ‘‘;
    }

 

微信h5支付

上一篇:微信小程序swiper禁止用户手动滑动


下一篇:css3中-moz、-ms、-webkit,-o分别代表的意思,以及微信浏览器内核分析