zoukankan      html  css  js  c++  java
  • php微信h5支付

    近期开发用到了微信h5支付,大概分享一下代码和常见问题

    注:支付调试需要在线上调试,本地调试微信回调是找不到的

    1.预下单

     public function getCode($info)
        {
            $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
            //1.获取调用统一下单接口所需必备参数
            $appid = $this->appid;//微信公众号appid
            $mch_id = $this->mchid;//微信支付商户号
            $key = $this->key;//自己设置的微信商家key
            $out_trade_no = $info['order_id'];//平台内部订单号
            $nonce_str = MD5($out_trade_no);//随机字符串
            $body = $info['goods_name'];//付款内容
            $total_fee = $info['amount'] * 100;//付款金额,单位为分
            $spbill_create_ip = getIP(); //获得用户设备IP
            $attach = 'weixinh5';//附加数据(自定义,在支付通知中原样返回)
            $notify_url = $this->notify_url; //微信支付成功数据回调地址,这里处理支付成功后数据库对应操作
            $trade_type = 'MWEB';//交易类型,微信H5支付时固定为MWEB
            $scene_info = $this->scene_info;//场景信息
            //2.将参数按照key=value的格式,并按照参数名ASCII字典序排序生成字符串
            $signA = "appid=$appid&attach=$attach&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
            //3.拼接字符串
            $strSignTmp = $signA . "&key=$key";
            //4.MD5加密后转换成大写
            $sign = strtoupper(MD5($strSignTmp));
            //5.拼接成所需XML格式
            $post_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>$spbill_create_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>";
    
            //6.以POST方式向微信传参,并取得微信返回的支付参数
            $dataxml = httpRequest($url, 'POST', $post_data);
            $result  = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA); //将微信返回的XML转换成数组
     
    //5.调用支付类中封装的支付方法并对应传参
                    $result = $this->getCode($info);
                    //6.当return_code和result_code均为SUCCESS,代表下单成功,将支付参数返回
                    if ($result['return_code'] == 'SUCCESS') {
                        if ($result['result_code'] == 'SUCCESS') {
                            $return_url = "自定义你的支付成功返回地址?orderid=" . $order_id;//带id方便查询是否支付成功
                            $return_url = urlencode($return_url);
                            $url = $result['mweb_url'] . '&redirect_url=' . $return_url;
                            $return_wx = [
                                'amount' => $info['amount'],//你的支付金额
                                'url' => $url,//微信返回的拉起支付地址
                            ];
                            file_put_contents('url.txt', $url);//方便调试
                return view('wxpay', $return_wx);//到自定义付款页面
                        } elseif ($result['result_code'] == 'FAIL') {
                            return $this->ApiErrorNo($result['err_code_des'], '400');
                        }
                    } else {
                        return $this->ApiErrorNo('订单不存在,请核实后再提交!', '400');
                    }
        }

    2.前端付款页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>微信支付</title>
        <style type="text/css">
            body{
                font-family: "Microsoft YaHei";
            }
            .pay-box{
                position: absolute;
                top: 50%;
                margin-top: -516px;
                left: 50%;
                margin-left: -320px;
            }
            .ico{
                width: 240px;
                height: 240px;
                border-radius: 120px;
                background: #3FB837;
                color: #fff;
                display: inline-block;
                font-size: 160px;
                line-height: 240px;
            }
            .txt{
                font-size: 42px;
                padding-top: 30px;
                color: #333;
            }
            .val{
                font-size: 80px;
                font-weight: bold;
            }
            .pay{
                width: 640px;
                height: 100px;
                margin-top: 100px;
                padding: 20px;
                border-radius: 10px;
                font-size:42px;
                color: #fff;
                background: #07BF05;
                border: 0px;
                text-align: center;
            }
            a{
                color: #fff;
                background: transparent !important;
            }
        </style>
    </head>
    <body>
    <div class="pay-box" style="text-align: center;">
        <div class="ico"></div>
        <div class="txt">
            支付金额
        </div>
        <div class="val"><span>{{$amount}}</span>
            <!-- 支付价格 -->
        </div>
        <a class="pay" href="{{$url}}"><button class="pay">确认支付</button></a>
        <!-- 这里点击调起微信支付页面 mweb_url  -->
    
    </div>
    </body>
    <script>
    
    </script>
    </html>

    3.支付成功微信数据回调接口操作对应数据库(例如订单支付状态改变等)

    4.支付成功页面返回接口地址上面已设置,逻辑自行处理  

    大概这样就结束啦  有用的麻烦点个赞

  • 相关阅读:
    ElasticSearch 查询语法
    自定义的带tab的可左右滑动的viewpager之二viewpager与fragment不兼容
    QT5 串口收发实例代码
    Communications link failure报错的处理
    mac 环境下mysql 不能删除schema问题的解决办法
    [置顶] How to dump redo log entry?
    pjsip视频通信开发(上层应用)之拨号界面整体界面功能实现
    windows command ftp 中文文件名乱码解决方法
    (字符串的模式匹配4.7.18)POJ 2406 Power Strings(求一个字符串的最小重复串)
    通过程序 VB.Net 或 C# 读取文本文件行数
  • 原文地址:https://www.cnblogs.com/zhu-hong/p/11772652.html
Copyright © 2011-2022 走看看