PHP微信H5支付

public function _initialize()
    {
        Vendor('Wxpay.example.lib.WxPay.Api');
        Vendor('Wxpay.example.Wxpay.Config');
        Vendor('Wxpay.example.Wxpay.JsApiPay');
    }
    public function wachat_pay(){
        $config = config('Wechat');
        $order_id = intval(input("param.order_id/d",0));
        if(empty($order_id)){
            $this->error("订单有误");
        }
        $order = Db::name('order')->where(array('id'=>$order_id))->find();
        if(empty($order)){
            $this->error("订单有误");
        }
        $theurl="https://api.mch.weixin.qq.com/pay/unifiedorder";
        $input['appid']='wx***********';
        $input['mch_id']='15*********';
        $input['nonce_str']=$this->getNonceStr();
        $input['device_info']='WEB';

        $input['body']='购物';
        $input['out_trade_no']=$order['order_sn'];;
        $input['total_fee']=($order['total_price']+$order['express_fee'])*100;
//        $input['total_fee']=1;
        $input['spbill_create_ip']=$this->get_ip();
        $input['notify_url']='http://'.$_SERVER['HTTP_HOST'].'/index/Wxpay/notify_url';
        $input['trade_type']='MWEB';
        $input['scene_info']='{"h5_info":{"type":"Wap","wap_url":"http://www.*******.com","wap_name":"商城"}}';

        $thesign['appid']=$input['appid'];
        $thesign['mch_id']=$input['mch_id'];
        $thesign['device_info']=$input['device_info'];
        $thesign['nonce_str']=$input['nonce_str'];
        $thesign['body']=$input['body'];
        $thesign['notify_url']=$input['notify_url'];
        $thesign['trade_type']=$input['trade_type'];
        $thesign['scene_info']=$input['scene_info'];
        $thesign['total_fee']=$input['total_fee'];
        $thesign['spbill_create_ip']=$input['spbill_create_ip'];
        $thesign['out_trade_no']=$input['out_trade_no'];

        $input['sign']=$this->MakeSign($thesign);
        $post_data = "<xml>
                       <appid>{$thesign['appid']}</appid>
                       <body>{$thesign['body']}</body>
                       <mch_id>{$thesign['mch_id']}</mch_id>
                       <nonce_str>{$thesign['nonce_str']}</nonce_str>
                       <notify_url>{$thesign['notify_url']}</notify_url>
                       <out_trade_no>{$thesign['out_trade_no']}</out_trade_no>
                       <scene_info>{$thesign['scene_info']}</scene_info>
                       <spbill_create_ip>{$thesign['spbill_create_ip']}</spbill_create_ip>
                       <total_fee>{$thesign['total_fee']}</total_fee>
                       <trade_type>{$thesign['trade_type']}</trade_type>
                       <sign>{$input['sign']}</sign>
                   </xml>";//拼接成XML 格式

        $content=$this->http_post($theurl,$post_data);
        $objectxml = (array)simplexml_load_string($content,'SimpleXMLElement',LIBXML_NOCDATA);

        if($objectxml['return_code'] == 'SUCCESS')  {
            if($objectxml['result_code'] == 'SUCCESS'){//如果这两个都为此状态则返回mweb_url,详情看‘统一下单’接口文档

                $url_encode_redirect_url = urlencode($config['RETURN_URL']);//支付成功回调路径,在Home/config.php文件中可配置
                $url                     = $objectxml['mweb_url'] . '&redirect_url=' . $url_encode_redirect_url;//拼接支付链接
                echo "<script>window.location.href='" . $url ."';</script>";
                exit();
            }
            if($objectxml['result_code'] == 'FAIL') {
                return $err_code_des = $objectxml['err_code_des'];
            }
        }
    }
    
    //获取用户ip
    public function get_ip(){
        $ip='unknown';
        if($_SERVER['REMOTE_ADDR']){
            $ip=$_SERVER['REMOTE_ADDR'];
        }elseif(getenv('REMOTE_ADDR')){
            $ip=getenv('REMOTE_ADDR');
        }
        return $ip;
    }
    /*随机字符串*/
    public  function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
    //签名生成
    public function MakeSign($config)
    {
        $stringA = "appid=".$config['appid']."&body=".$config['body']."&mch_id=".$config['mch_id']."&nonce_str=".$config['nonce_str']."&notify_url=".$config['notify_url']."&out_trade_no=".$config['out_trade_no']."&scene_info=".$config['scene_info']."&spbill_create_ip=".$config['spbill_create_ip']."&total_fee=".$config['total_fee']."&trade_type=".$config['trade_type'];

        $string=md5($stringA."&key=*************************************");
        $result = strtoupper($string);
        return $result;

    }
    public function http_post($url='',$post_data=array()) {

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

商城异步跳转

    public function notify_url(){
        $testxml  = file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);//转成数组,
        if($result){
            //如果成功返回了
            $out_trade_no = $result['out_trade_no'];
            $order = Db::name('order')->where('order_sn',$out_trade_no)->where('status',5)->find();
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
                //执行业务逻辑  改变订单状态等
            }
        }
        return '<xml><return_code><![CDATA[SUCCESS]></return_code><return_msg><![CDATA]></return_msg></xml>';
    }

在这里插入图片描述
微信H5支付,亲测可用!不足之处,欢迎指点!转载请注明来源

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值