php 微信h5支付,亲测可用,非微信浏览器

贴代码了

 public function pay(){
        header("Content-type:text/html;charset=utf-8");
        $out_trade_no = I('get.out_trade_no'); //订单号、在这之前就已经下单。
        $sign = I('get.sign');
        $check_sign = sha1($out_trade_no);
        if($sign !== $check_sign){
            echo "<script>alert('不合法签名');history.go(-1);</script>";
            return false;
        }
        $list=  M('juankuan')->where(array('number'=>$out_trade_no))->find();
        if(!$list) $this->jsonReturn(0,'订单不存在','');
        $url='https://api.mch.weixin.qq.com/pay/unifiedorder';  //下单地址
        $appid='';//公众号appid
        $appsecret='';
        $mch_id='';//商户平台id
        $nonce_str='qyzf'.rand(1, 999999);//随机数
        //$out_trade_no= 'dbjz'.date('Ymd').'_'.$id.'_'.substr(time(),4).rand(1,10000);
        $ip=$this->getClientIp();
        $scene_info='{"h5_info": {"type":"Wap","app_name": "project info","package_name": "wap订单微信支付"}}';

        $total_fee=$list['money'] * 100;      //金额要*100
        $trade_type='MWEB';
        $attach='我是测试';
        $body='支付商品名称';
        $notify_url='notify'; //回调地址
        $arr=array(
            'appid'=>$appid,
            'mch_id'=>$mch_id,
            'nonce_str'=>$nonce_str,
            'out_trade_no'=>$out_trade_no,
            'spbill_create_ip'=>$ip,
            'scene_info'=>$scene_info,
//            'openid'=>$openid,
            'total_fee'=>$total_fee,
            'trade_type'=>$trade_type,
            'attach'=>$attach,
            'body'=>$body,
            'notify_url'=>$notify_url
        );
        $sign=$this->getSign($arr);
        //<openid>'.$openid.'</openid>
        $data='<xml>
                 <appid>'.$appid.'</appid>
                 <attach>'.$attach.'</attach>
                 <body>'.$body.'</body>
                 <mch_id>'.$mch_id.'</mch_id>
                 <nonce_str>'.$nonce_str.'</nonce_str>
                 <notify_url>'.$notify_url.'</notify_url>
                 <out_trade_no>'.$out_trade_no.'</out_trade_no>
                 <spbill_create_ip>'.$ip.'</spbill_create_ip>
                 <total_fee>'.$total_fee.'</total_fee>
                 <trade_type>'.$trade_type.'</trade_type>
                 <scene_info>'.$scene_info.'</scene_info>
                 <sign>'.$sign.'</sign>
                 </xml>';
        $result=$this->https_request($url,$data);
//        echo '====================';
//        var_dump($result);
//        echo '*******************';
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result_info = json_decode(json_encode(simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        //  print_r($result_info);
        $this->assign('result_info',$result_info);//$result_info['mweb_url'];//遍历到静态页面,支付跳转使用
        $this->assign('list',$list);
        $this->display();
    }
/**
 * h5 微信回调 函数
 */
public function notify()
{

    $str_Post = $GLOBALS["HTTP_RAW_POST_DATA"];
    libxml_disable_entity_loader(true);
    $postObj = simplexml_load_string($str_Post, 'SimpleXMLElement', LIBXML_NOCDATA);
    $postObj = json_encode($postObj);
    $postObj = json_decode($postObj, true);
    $out_trade_no = trim($postObj["out_trade_no"]);
    if ($postObj["result_code"] == 'SUCCESS' && $postObj["return_code"] == 'SUCCESS') {
      //这块支付完成之后更新订单状态以及其它操作。
    }
}

// curl请求

public function https_request($url, $data) {

        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }

    /**
     * @return 获取ip
     */
    private function getClientIp()
    {
        $cip='unknown';
        if ($_SERVER['REMOTE_ADDR']){
            $cip=$_SERVER['REMOTE_ADDR'];
        }elseif (getenv($_SERVER['REMOTE_ADDR'])){
            $cip=getenv($_SERVER['REMOTE_ADDR']);
        }
        return $cip;
    }




    /**
     *     作用:生成签名
     */
    public function getSign($Obj)
    {
        foreach ($Obj as $k => $v)
        {
            $Parameters[$k] = $v;
        }
        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);
        //echo '【string1】'.$String.'</br>';
        //签名步骤二:在string后加入KEY   5a02bd8ecxxxxxxxxxxxxc1aae7d199  这里的秘钥是 商户平台设置的一定要改不然报签名错误
        $String = $String."&key=zgdbjz1234567890weixin1234567890";
        //echo "【string2】".$String."</br>";
        //签名步骤三:MD5加密
        $String = md5($String);
        //echo "【string3】 ".$String."</br>";
        //签名步骤四:所有字符转为大写
        $result_ = strtoupper($String);
        //echo "【result】 ".$result_."</br>";
        return $result_;
    }



    public function formatBizQueryParaMap($paraMap, $urlencode)
    {
//        var_dump($paraMap);//die;
        $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;
    }

猜你喜欢

转载自blog.csdn.net/hua950327/article/details/81539451