• “商家参数格式有误”应用切微信H5支付完美解决方案


    一、业务场景发生

    最近在跟一些合作公司作业务对接,在对方的APP中接入我们的H5支付,包括微信和支付宝。

    那就开搞,进展顺利,貌似一切都在掌握之中,给到对方一个链接即可调起支付。形如:

    https://www.batsing.com/api/h5pay?appid=1234567001&userid=10&money=0.01&paytype=weixin

    通过QQ或者浏览器打开即可付款发货,丝滑完美。

    然而。。。接入到他们APP中却报错了,赶紧去微信开发文档看看

    微信官方给的方案很简单,让APP开发的去修改webview,但这,合理吗?

    对于自家做APP的是可以。但对于像我们这种做第三方的,这显然不是一个优秀的解决途径,那怎么办呢?

    二、 问题本质

    出现这个问题的本质是什么呢?是APP中页面跳转到MWEB_URL的请求中没有带上Referer。

    (APP中的webview把一些http请求头给吃掉了,referer不会像普通浏览器那样自动识别转发出去)

    既然前端的解决方案不友好,那就换成后端来解决,强大的curl可以模拟一切http请求。

    三、Hack跟踪微信H5支付过程

    在微信下单接口解析xml得到 MWEB_URL,然后对照着微信开发文档,经过一番参数调试,伪造了请求IP和referer,请求IP要与调用下单接口时传递的客户端IP一致,referer要与微信商户后台上填写的授权域名一致。

    然后捕捉到了正常的微信支付页面代码。

    这是curl发送模拟请求的代码

    <?php
    $mweb_url = 'https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096';
    $headers = [
        "CLIENT-IP:" . $_SERVER['REMOTE_ADDR'],
        "X-FORWARDED-FOR:" . $_SERVER['REMOTE_ADDR'],
    ];
    $result = Common_Http::curl_get_content($mweb_url, [
        'CURLOPT_HTTPHEADER' => $headers,
        'CURLOPT_REFERER' => 'https://www.batsing.com'
    ]);
    echo'<pre>';print_r(htmlspecialchars($result));echo'</pre>';

    打印出来得到的内容如下,我们只看 window.onload 部分就可以了。

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta http-equiv=Content-Type content="text/html;charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="format-detection" content="telephone=no">
        <title>weixin</title>
        <style>.f10{font-size:10px}.f11{font-size:11px}.f12{font-size:12px}.f13{font-size:13px}.f14{font-size:14px}.f15{font-size:15px}.f16{font-size:16px}.f17{font-size:17px}.f18{font-size:18px}.f19{font-size:19px}.f20{font-size:20px}body{font-size:14px}h1,h2,h3,h4,h5{font-weight:400;font-style:normal}h1,.h1{font-size:20px}h2,.h2{font-size:18px}h3,.h3{font-size:16px}h4,.h4{font-size:14px}h5,.h5{font-size:12px}a,a:visited{color:#007aff}.text_color{color:#888}.title_color{color:#000}.desc{color:#b2b2b2}.warn{color:#b71414}.nickname{color:#576b95}.tips{font-size:13px;color:#b2b2b2}body{background-color:#fff}body.msg_dark{background-color:#2e3132;color:#fff}.page_msg{padding:75px 15px 0;text-align:center}.icon_area{margin-bottom:19px}.text_area{margin-bottom:25px}.text_area .title{margin-bottom:12px}.opr_area{margin-bottom:25px}.extra_area{margin-bottom:20px}@media screen and (min-height:416px){.extra_area{position:fixed;left:0;bottom:0;width:100%}}.btn{display:block;margin-left:auto;margin-right:auto;padding-left:14px;padding-right:14px;font-size:16px;text-align:center;text-decoration:none;overflow:visible;height:40px;border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;color:#fff;line-height:40px;-webkit-tap-highlight-color:rgba(255,255,255,0)}.btn.btn_inline{display:inline-block}.btn_default{background-color:#d1d1d1}.btn_default:not(.btn_disabled):visited{color:#fff}.btn_default:not(.btn_disabled):active{color:rgba(255,255,255,.4);background-color:#a7a7a7}.btn_primary{background-color:#04be02}.btn_primary:not(.btn_disabled):visited{color:#fff}.btn_primary:not(.btn_disabled):active{color:rgba(255,255,255,.4);background-color:#039702}.btn_warn{background-color:#ef4f4f}.btn_warn:not(.btn_disabled):visited{color:#fff}.btn_warn:not(.btn_disabled):active{color:rgba(255,255,255,.4);background-color:#c13e3e}.btn.btn_mini{height:25px;line-height:25px;font-size:14px}button.btn,input.btn{width:100%;border:0;outline:0;-webkit-appearance:none}button.btn:focus,input.btn:focus{outline:0}button.btn_inline,input.btn_inline{width:auto}.btn_disabled{color:rgba(255,255,255,.6)}.btn+.btn{margin-top:10px}.btn.btn_inline+.btn.btn_inline{margin-top:auto;margin-left:10px}.btn_area{margin-left:-5px;margin-right:-5px;font-size:0}.btn_area.btn_area_inline{margin-left:auto;margin-right:auto;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex}.btn_area.btn_area_inline .btn{margin-top:auto;margin-right:10px;width:100%;-webkit-box-flex:1;-webkit-flex:1;-moz-box-flex:1;-ms-flex:1;box-flex:1;flex:1;display:inline-block 9;width:48% 9;margin-left:1% 9;margin-right:1% 9}.btn_area.btn_area_inline .btn:last-child{margin-right:0}span.btn button{display:block;width:100%;height:100%;background-color:transparent;border:0;outline:0;color:#fff}span.btn button:active{color:rgba(255,255,255,.4)}span.btn.btn_loading button,span.btn.btn_disabled button{color:#fff}.icon_msg{width:100px;height:100px;vertical-align:middle;display:inline-block;border-radius:50%;-moz-border-radius:50%;-webkit-border-radius:50%}.icon_msg.warn{background-color:#f86161;color:#fff;font-size:60px;font-style:normal}html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{font:14px/1.5em "Helvetica Neue",Helvetica,Arial,sans-serif;background-color:#efeff4;line-height:1.6}body,h1,h2,h3,h4,h5,p,ul,ol,dl,dd,fieldset,textarea{margin:0}fieldset,legend,textarea,input,button{padding:0}button,input,select,textarea{font-family:inherit;font-size:100%;margin:0;*font-family:"Helvetica Neue",Helvetica,Arial,sans-serif}ul,ol{padding-left:0;list-style-type:none}a img,fieldset{border:0}a{text-decoration:none}</style>
    </head>
    <body>
    <div class="body">
        <div id="errpage" class="page_msg">
                </div>
    </div>
    <script src="//wx.gtimg.com/wxpay_h5/fingerprint2.min.1.4.1.js"></script>
    <script type="text/javascript">
    
        var is_postmsg="";
        if( 0!==0 && is_postmsg=="1" )
        {
            parent.postMessage(JSON.stringify({
                action : "send_deeplink_fail",
                data : {
                            deeplink : ""
                       },
                error : {
                    error_code : "0",
                    error_msg : "ok"
                        }
                }), "");
        }
        if( 0===0)
        {
            window.onload=function()
            {
    //        var fp=new Fingerprint2();
                //      fp.get(function(result)
                {
                    // var fingerprint="";
                    /*         if(fingerprint!=result && fingerprint)
                     {
                     document.getElementById("errpage").innerHTML='<div class="icon_area"><i class="icon_msg warn">!</i></div> 
                     <div class="text_area"> 
                     <h2 id="111" class="title"> '+result+'网络环境未能通过安全验证,请稍后再试</h2> 
                     </div>';
                     return;
                     }*/
                    var is_postmsg="";
                    if(is_postmsg=="1")
                    {
                        parent.postMessage(JSON.stringify({
                            action : "send_deeplink",
                            data : {
                                deeplink : "weixin://wap/pay?prepayid%3Dwx0917024062319287307475501524736300&package=2450968634&noncestr=1591693360&sign=07b94cb3986c14bab48ec240f805f9a5"
                            }
                        }), "");
                    }
                    else
                    {
                        var url="weixin://wap/pay?prepayid%3Dwx0917024062319287307475501524736300&package=2450968634&noncestr=1591693360&sign=07b94cb3986c14bab48ec240f805f9a5";
                        var redirect_url="";
                        top.location.href=url;
    
                        if(redirect_url)
                        {
                            setTimeout(
                                function(){
                                    top.location.href=redirect_url;
                                },
                                5000
                            );
                        }
                        else
                        {
                            setTimeout(
                                function(){
                                    window.history.back();
                                },
                                5000);
                        }
                    }
                }
                // );
            }
    
    
    
        }
    </script>
    </body>
    </html>
    View Code

    一开始还以为一直被判断“网络环境未能通过安全验证”,然后才发现那段是被注释掉的,被虚晃了一枪。

    关键部分,页面里有两段一样的 weixin://wap/pay? 开头的网址,很明显这个网址就是要拉起手机里的微信应用发起支付的。

    其他的js代码逻辑也很简单,先从顶层跳转到weixin:// 即是拉起支付,然后5秒钟后跳转到重定向地址(若无重定向地址则返回上一页)。

    我们尝试用手机直接访问这个 weixin:// 网址,确实可以拉起手机支付,支付也可以顺利完成到账。

    那可以确定,IP地址和referer校验只是在 https://wx.tenpay.com/ 这里有做,到了 weixin://wap/pay 这一步他就不再校验这些设备信息了。

     

    四、制作新的H5支付页面

    最后,我们参照微信H5支付页面,拼出一个类似的H5界面,实现跳转支付以及5秒后跳转到支付完成页;

    这样就可以实现在webview中也能顺利发起微信H5支付,并实现同样的跳转逻辑了。

  • 相关阅读:
    一个顽猴沿着一座小山的n级台阶向上跳,猴子上山一步可跳1级或3级,试求上山的n级台阶有多少种不同的爬法。
    postgres的initdb解析——从一次插件升级失败说起
    跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))
    跟我一起读postgresql源码(十三)——Executor(查询执行模块之——Join节点(上))
    跟我一起读postgresql源码(十二)——Executor(查询执行模块之——Materialization节点(下))
    跟我一起读postgresql源码(十一)——Executor(查询执行模块之——Materialization节点(上))
    跟我一起读postgresql源码(十)——Executor(查询执行模块之——Scan节点(下))
    跟我一起读postgresql源码(九)——Executor(查询执行模块之——Scan节点(上))
    跟我一起读postgresql源码(八)——Executor(查询执行模块之——可优化语句的执行)
    跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)
  • 原文地址:https://www.cnblogs.com/batsing/p/wxh5pay.html
Copyright © 2020-2023  润新知