Example #1
0
        /**
         *
         * 通过code换取网页授权access_token和openid的返回数据,正确时返回的JSON数据包如下:
         * {
         *  "access_token":"ACCESS_TOKEN",
         *  "expires_in":7200,
         *  "refresh_token":"REFRESH_TOKEN",
         *  "openid":"OPENID",
         *  "scope":"SCOPE",
         *  "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
         * }
         * 其中access_token可用于获取共享收货地址
         * openid是微信支付jsapi支付接口统一下单时必须的参数
         * 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
         * @失败时抛异常WxPayException
         */
        public void GetOpenidAndAccessTokenFromCode(string code, string AdminHotelid, int Hotelid = 0)
        {
            try
            {
                //构造获取openid及access_token的url
                WxPayData data = new WxPayData();
                data.SetValue("appid", WxPayConfig.APPID(AdminHotelid, Hotelid));
                data.SetValue("secret", WxPayConfig.APPSECRET(AdminHotelid, Hotelid));
                data.SetValue("code", code);
                data.SetValue("grant_type", "authorization_code");
                string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();

                //请求url以获取数据
                string result = HttpService.Get(url);

                Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);

                //保存access_token,用于收货地址获取
                JsonData jd = JsonMapper.ToObject(result);
                access_token = (string)jd["access_token"];

                //获取用户openid
                openid = (string)jd["openid"];

                Log.Debug(this.GetType().ToString(), "Get openid : " + openid);
                Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token);
            }
            catch (Exception ex)
            {
                Log.Error(this.GetType().ToString(), ex.ToString());
                throw new WxPayException(ex.ToString());
            }
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["code"] != null)
            {
                string state = "";

                if (Request["state"] != null)
                {
                    state = Server.UrlDecode(Request["state"]);
                }

                string code    = Request["code"].ToString();
                string postUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
                postUrl = string.Format(postUrl, WxPayConfig.APPID("1", 0), WxPayConfig.APPSECRET("1", 0), code);
                string returnJason = GetJson(postUrl);
                //获取返回信息

                JavaScriptSerializer        serializer = new JavaScriptSerializer();
                Dictionary <string, object> json       = (Dictionary <string, object>)serializer.DeserializeObject(returnJason);
                object value;
                string openid = "";
                if (json.TryGetValue("openid", out value))
                {
                    openid = value.ToString();
                    Session["zdyopenid"] = openid.ToString();
                    Session.Timeout      = 60 * 60 * 24;
                }
                Log.Info("获取智订云openid:" + openid + ",URL:", state);
                Response.Redirect(state, false);
            }
        }
Example #3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["code"] != null)
                {
                    string state        = "";
                    string AdminHotelid = "1";
                    int    Hotelid      = 0;
                    if (Request["state"] != null)
                    {
                        state = Server.UrlDecode(Request["state"]);
                    }

                    string code    = Request["code"].ToString();
                    string postUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
                    postUrl = string.Format(postUrl, WxPayConfig.APPID(AdminHotelid, Hotelid), WxPayConfig.APPSECRET(AdminHotelid, Hotelid), code);

                    string returnJason = GetJson(postUrl);
                    //获取返回信息

                    JavaScriptSerializer        serializer = new JavaScriptSerializer();
                    Dictionary <string, object> json       = (Dictionary <string, object>)serializer.DeserializeObject(returnJason);
                    object value;
                    string openid;
                    if (json.TryGetValue("openid", out value))
                    {
                        openid = value.ToString();
                        Session["zdyopenid"]      = openid.ToString();
                        Session["zdyopenid_user"] = openid.ToString();
                        Session.Timeout           = 60 * 60 * 24;
                    }
                    string accessToken = "";
                    if (json.TryGetValue("access_token", out value))
                    {
                        accessToken = value.ToString();
                    }

                    string url  = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken.Trim(), Session["zdyopenid_user"].ToString().Trim());
                    string data = GetJson(url);
                    Dictionary <string, object> json1 = (Dictionary <string, object>)serializer.DeserializeObject(data);
                    Session["zdyInfo"] = json1;
                    Response.Redirect(state, false);
                }
            }
            catch (Exception ee)
            {
                Log.Info("智订云获取用户信息报错:", ee.Message);
            }
        }
Example #4
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["code"] != null)
            {
                string state        = "";
                string AdminHotelid = "1";
                int    Hotelid      = 0;
                if (Request["state"] != null)
                {
                    state = Server.UrlDecode(Request["state"]);
                    string[] urlid = state.Split('?');    //dh.sewa-power.com/Reservation/HotelDetails.aspx?AdminHotelid=SEWA006637&hotelid=36
                    string[] a     = urlid[1].Split('&'); //AdminHotelid=SEWA006637&hotelid=36
                    string[] b     = a[0].Split('=');     //AdminHotelid=SEWA006637
                    string[] c     = a[1].Split('=');     //hotelid=36
                    AdminHotelid = b[1];
                    Hotelid      = Convert.ToInt32(c[1]);
                }


                string code    = Request["code"].ToString();
                string postUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
                postUrl = string.Format(postUrl, WxPayConfig.APPID(AdminHotelid, Hotelid), WxPayConfig.APPSECRET(AdminHotelid, Hotelid), code);
                string returnJason = GetJson(postUrl);
                //获取返回信息

                JavaScriptSerializer        serializer = new JavaScriptSerializer();
                Dictionary <string, object> json       = (Dictionary <string, object>)serializer.DeserializeObject(returnJason);
                object value;
                string openid = "";
                if (json.TryGetValue("openid", out value))
                {
                    openid = value.ToString();
                    Session["zfzdyopenid" + AdminHotelid + Hotelid] = openid.ToString();
                    Session.Timeout = 60 * 60 * 24;
                }
                Log.Info("获取支付到店zfzdyopenid:" + openid + ",URL:", state);
                Response.Redirect(state, false);
            }
        }
Example #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["code"] != null)
            {
                string state        = "";
                string AdminHotelid = "";
                if (Request["state"] != null)
                {
                    state = Server.UrlDecode(Request["state"]);
                    string[] urlid = state.Split('?');    //dh.sewa-power.com/Reservation/HotelDetails.aspx?AdminHotelid=SEWA006637&hotelid=36
                    string[] a     = urlid[1].Split('&'); //AdminHotelid=SEWA006637&hotelid=36
                    string[] b     = a[0].Split('=');     //AdminHotelid=SEWA006637
                    //string[] c = a[1].Split('='); //hotelid=36
                    AdminHotelid = b[1];
                    //Hotelid = Convert.ToInt32(c[1]);
                }

                string session_zhi     = "openid" + AdminHotelid;
                string session_unionid = "unionid" + AdminHotelid;

                string code    = Request["code"].ToString();
                string postUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code";
                postUrl = string.Format(postUrl, WxPayConfig.APPID(AdminHotelid, Hotelid), WxPayConfig.APPSECRET(AdminHotelid, Hotelid), code);

                string returnJason = GetJson(postUrl);
                //获取返回信息

                JavaScriptSerializer        serializer = new JavaScriptSerializer();
                Dictionary <string, object> json       = (Dictionary <string, object>)serializer.DeserializeObject(returnJason);
                object value;
                string openid = "";
                if (json.TryGetValue("openid", out value))
                {
                    openid = value.ToString();
                    Session[session_zhi]   = openid.ToString();
                    Session["openid_user"] = openid.ToString();
                    Session.Timeout        = 60 * 60 * 24;
                }
                string accessToken = "";
                if (json.TryGetValue("access_token", out value))
                {
                    accessToken = value.ToString();
                }

                string url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken.Trim(), openid);
                //string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", accessToken.Trim(), openid);
                string data = GetJson(url);
                Dictionary <string, object> json1 = (Dictionary <string, object>)serializer.DeserializeObject(data);

                string unionid = "0";
                if (json1.TryGetValue("unionid", out value))
                {
                    unionid = value.ToString();
                }

                Session[session_unionid] = unionid;


                Session["Info"] = json1;

                Response.Redirect(state, false);
            }
        }