微信支付H5完整版代码

在贴代码之前,说明一下:我用的是php7.1

第一步:在前端html里面,设置一个iframe。<iframe src="" id="payw" width="0" height="0" sandbox="allow-scripts allow-top-navigation allow-same-origin"></iframe>

第二步,当点击支付按钮的时候,调用js代码。

$("#btnsure").click(function(e){
               $.ajax({
                    type: "post",
                    url: "/member/index/pay.html",
                    data: { je:$('input:radio[name=fee]:checked').val() },//je代表金额
                    dataType: 'JSON',
                    success: function (datak) {
                        layui.use(['layer'], function () {
                            var layer = layui.layer;
                            layer.ready(function () {
                                switch(datak.code){
                                    case 1:
                                        $("#payw").attr('src',datak.url);//url代表服务器返回的微信支付吊起地址。把这个地址放到iframe里面。
                                    break;
                                    case -1:
                                        layer.msg(datak.msg, { 'time': 2000 });
                                        window.location.href = datak.url;
                                    break;
                                    default:
                                        layer.msg(datak.msg, { 'time': 2000 });
                                    break;
                                }
                            });
                        });
                    },
                    error: function () {
                        alert('编辑失败');
                    },
                    complete: function () {
                    }
                });
});

第三步,服务器代码(PHP)

$payconfig = config('self');//获取微信支付配置
$param = array();
$param['body']              = $this->request->_member['itemid'];//当前登录会员的ID
$param['out_trade_no']      = date('YmndHis',time()).rand(1111,9999); //订单号
$param['total'] = floatval($this->request->post('je'))*100;//支付金额
$param['spbill_create_ip']  = $this->request->ip(); //ip地址
$param['trade_type']        = "MWEB"; //H5 支付类型
$param['scene_info'] = '{"h5_info": {"type":"Wap","wap_url": "https://XXXX","wap_name": "XXXXXXX"}}';   //场景信息,h5固定
$param['attach'] = $this->request->_member['itemid']; //当前登录会员的ID
$wechatpay = new Weixin($param, $payconfig['weixin'], array(), '');//实例化微信支付类对象
$result = $wechatpay->payh5();//调用h5
if($result['return_code'] == 'SUCCESS'){
                $this->success('支付成功',$result['mweb_url'].'&redirect_url=https://www.yingmai360.com/member/index/index','',$result);//把支付地址返回给客户端。
}else{
                $this->error('支付失败');
}

第四步:支付回调的代码(PHP)

//获取接口数据,如果$_REQUEST拿不到数据,则使用file_get_contents函数获取
        $post = file_get_contents('php://input');
        if (empty($post) || $post == null || $post == '') {
            //阻止微信接口反复回调接口  文档地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7,下面这句非常重要!!!
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';  
            echo $str;
            exit('Notify 非法回调');
        }
        libxml_disable_entity_loader(true); //禁止引用外部xml实体
        $xml = simplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);//XML转数组  
        $post_data = (array)$xml;
        /** 解析出来的数组
        *Array
        * (
        * [appid] => wx1c870c0145984d30
        * [bank_type] => CFT
        * [cash_fee] => 100
        * [fee_type] => CNY
        * [is_subscribe] => N
        * [mch_id] => 1297210301
        * [nonce_str] => gkq1x5fxejqo5lz5eua50gg4c4la18vy
        * [openid] => olSGW5BBvfep9UhlU40VFIQlcvZ0
        * [out_trade_no] => fangchan_588796
        * [result_code] => SUCCESS
        * [return_code] => SUCCESS
        * [sign] => F6890323B0A6A3765510D152D9420EAC
        * [time_end] => 20180626170839
        * [total_fee] => 100
        * [trade_type] => JSAPI
        * [transaction_id] => 4200000134201806265483331660
        * )
        **/
        $myfile = fopen("return_log.txt", "a") or die("Unable to open file!");
        fwrite($myfile, json_encode($post_data)."\r\n");
        fclose($myfile); //把获取到的数组信息保存到文件里面。

//剩下的逻辑代码,自己根据需求来写。

微信支付底层的类weixin 和 h5支付的函数payh5(),代码单独找我要。

Guess you like

Origin blog.csdn.net/u010261924/article/details/108686112