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 = "
                       {$thesign['appid']}
                       {$thesign['body']}
                       {$thesign['mch_id']}
                       {$thesign['nonce_str']}
                       {$thesign['notify_url']}
                       {$thesign['out_trade_no']}
                       {$thesign['scene_info']}
                       {$thesign['spbill_create_ip']}
                       {$thesign['total_fee']}
                       {$thesign['trade_type']}
                       {$input['sign']}
                   ";//拼接成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 "";
                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']."¬ify_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 '';
    }


微信H5支付,亲测可用!不足之处,欢迎指点!转载请注明来源

你可能感兴趣的:(微信支付)