微信公众号H5支付步骤

微信公众平台:https://mp.weixin.qq.com/

进入 微信支付 管理》开通支付功能。

微信支付|商户平台:

设置安全目录:https://pay.weixin.qq.com/index.php/extend/pay_setting,意思是只有该目录下的页面才能够发起支付请求。

注意保存对应的appid和Appsecret。

接下来是微信的授权js,Authorize.cshtml

<script type="text/javascript">
        var code = GetQueryString("code");
        var state = GetQueryString("state");
        if (code == null || code == undefined || code == "") {
            //步骤1:第一次进入本页面,先获取微信的授权code,然后再跳转会本页面,这时候微信会自动加上code参数
            var REDIRECT_URI = encodeURI("http://www.xxx.com/Home/Authorize");
            var url2 = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxx&redirect_uri=" + REDIRECT_URI + "&response_type=code&scope=snsapi_userinfo&state=" + state + "#wechat_redirect";
            window.location.href = url2;
        }
        else {
            //步骤2:获取openid
            $.ajax({
                type: "GET",
                url: "/WX/GetToken",
                data: { code: code },
                success: function (result) {
                    var data = $.parseJSON(result);
                    if (data.openid != null && data.openid != "" && data.openid != undefined) {
                        if (data.IsFirst) {
                            window.location.href = "/UInfo/UData?state=" + state;
                        }
                        else {
                            if (state == 1) {
                                window.location.href = "/BInfo/Index";
                            }
                            else if (state == 2) {
                                window.location.href = "/CInfo/Index";
                            }
                            else {
                                window.location.href = "/UInfo/Index";
                            }
                        }
                    }
                    else {
                        alert(data.errmsg);
                    }
                }
            });
        }
    </script>

猜你喜欢

转载自www.cnblogs.com/xsj1989/p/9034654.html