public void ProcessRequest(HttpContext context) { //在此处写入您的处理程序实现。 HttpRequest request = context.Request; HttpResponse response = context.Response; PayModel payModel = JsonConvert.DeserializeObject <PayModel>(request["payinfo"]); if (string.IsNullOrEmpty(payModel.OpenId)) { string redirect_uri = HttpUtility.UrlEncode(request.Url.ToString()); GetUserOpenId(payModel, redirect_uri); payModel.OpenId = UserOpenId; response.Redirect(payModel.ToString()); } }
/// <summary> /// 获取当前用户的微信 OpenId,如果知道用户的OpenId请不要使用该函数 /// </summary> private void GetUserOpenId(PayModel payModel, string redirect_uri) { if (!string.IsNullOrEmpty(payModel.AppId) && !string.IsNullOrEmpty(payModel.AppKey) && !string.IsNullOrEmpty(payModel.AppSecret)) { HttpRequest Request = HttpContext.Current.Request; HttpResponse Response = HttpContext.Current.Response; string code = Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { string code_url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=lk#wechat_redirect", payModel.AppId, redirect_uri); Response.Redirect(code_url); } else { #region =======获取支付用户 OpenID========= string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", payModel.AppId, payModel.AppSecret, code); string returnStr = PayUtil.Send("", url); PayUtil.SaveLog("获取token返回信息:", returnStr); var obj = JsonConvert.DeserializeObject <OpenModel>(returnStr); url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", payModel.AppId, obj.refresh_token); returnStr = PayUtil.Send("", url); obj = JsonConvert.DeserializeObject <OpenModel>(returnStr); PayUtil.SaveLog("用户access_token/openid:", obj.access_token + "/" + obj.openid); //url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", obj.access_token, obj.openid); //returnStr = HttpUtil.Send("", url); //PayUtil.SaveLog("用户信息:" , returnStr); UserOpenId = obj.openid; #endregion } } else { PayUtil.SaveLog("获取用户的微信用户OpenId失败", "参数->AppId:" + payModel.AppId + ";AppKey:" + payModel.AppKey + ";AppSecret:" + payModel.AppSecret); } }
/// <summary> /// 获取配置信息 /// </summary> /// <returns></returns> public PayModel GetPayModel() { //如果有多个商家,可从数据库取商家配置信息 //string appId=GetRequest("AppId",""); //DataTable dt = new DataTable(); //DataRow dr = dt.Rows[0]; //PayModel p = new PayModel(); //p.AppId = appId; //p.AppKey = dr["AppKey"].ToString(); //本示例使用配置信息(单商家) if (!string.IsNullOrEmpty(PayConfig.AppId) && !string.IsNullOrEmpty(PayConfig.AppKey)) { PayModel p = new PayModel(); p.AppId = PayConfig.AppId; p.AppKey = PayConfig.AppKey; return p; } else { PayUtil.SaveLog("Notify页面->获取配置信息失败", "AppId:" + PayConfig.AppId + ";AppKey:" + PayConfig.AppKey); return null; } }
/// <summary> /// 获取当前用户的微信 OpenId,如果知道用户的OpenId请不要使用该函数 /// </summary> private void GetUserOpenId(PayModel payModel, string redirect_uri) { if (!string.IsNullOrEmpty(payModel.AppId) && !string.IsNullOrEmpty(payModel.AppKey) && !string.IsNullOrEmpty(payModel.AppSecret)) { HttpRequest Request = HttpContext.Current.Request; HttpResponse Response = HttpContext.Current.Response; string code = Request.QueryString["code"]; if (string.IsNullOrEmpty(code)) { string code_url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=lk#wechat_redirect", payModel.AppId, redirect_uri); Response.Redirect(code_url); } else { #region =======获取支付用户 OpenID========= string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", payModel.AppId, payModel.AppSecret, code); string returnStr = PayUtil.Send("", url); PayUtil.SaveLog("获取token返回信息:", returnStr); var obj = JsonConvert.DeserializeObject<OpenModel>(returnStr); url = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type=refresh_token&refresh_token={1}", payModel.AppId, obj.refresh_token); returnStr = PayUtil.Send("", url); obj = JsonConvert.DeserializeObject<OpenModel>(returnStr); PayUtil.SaveLog("用户access_token/openid:", obj.access_token + "/" + obj.openid); //url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}", obj.access_token, obj.openid); //returnStr = HttpUtil.Send("", url); //PayUtil.SaveLog("用户信息:" , returnStr); UserOpenId = obj.openid; #endregion } } else { PayUtil.SaveLog("获取用户的微信用户OpenId失败", "参数->AppId:" + payModel.AppId + ";AppKey:" + payModel.AppKey + ";AppSecret:" + payModel.AppSecret); } }