public void ProcessRequest(HttpContext context) { TravelAgent.Model.WebInfo webinfo = new TravelAgent.BLL.WebInfo().loadConfig(context.Server.MapPath(ConfigurationManager.AppSettings["WebInfoConfig"].ToString())); if (webinfo != null) { appid = webinfo.AppID; mch_id = webinfo.Mchid; key = webinfo.Key; appsecret = webinfo.AppSecret; notify_url = webinfo.WebDomain + "/wxpay/notify_url.aspx"; } string action = JKRequest.GetQueryString("action"); switch (action) { case "unifysign": GetUnifySign(context); break; case "nativestatic": GetStaticPayQr(context); break; case "GetOpenId": GetOpenId(context); break; case "GetOrderStatus": GetOrderStatus(context); break; case "ver": GetVersion(context); break; } }
/// <summary> /// 获取当前用户的微信 OpenId,如果知道用户的OpenId请不要使用该函数 /// </summary> private void GetUserOpenId(string redirect_uri) { TravelAgent.Model.WebInfo webinfo = new TravelAgent.BLL.WebInfo().loadConfig(Server.MapPath(ConfigurationManager.AppSettings["WebInfoConfig"].ToString())); string code = Request.QueryString["code"]; LogUtil.WriteLog("Code:" + 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", webinfo.AppID, redirect_uri); Response.Redirect(code_url); } else { LogUtil.WriteLog(" ============ 开始 获取微信用户相关信息 ====================="); #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", webinfo.AppID, webinfo.AppSecret, code); string returnStr = HttpUtil.Send("", url); LogUtil.WriteLog("Send 页面 returnStr 第一个:" + 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}", webinfo.AppID, obj.refresh_token); returnStr = HttpUtil.Send("", url); obj = JsonConvert.DeserializeObject <OpenModel>(returnStr); LogUtil.WriteLog("Send 页面 access_token:" + obj.access_token); LogUtil.WriteLog("Send 页面 openid=" + 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); LogUtil.WriteLog("Send 页面 returnStr:" + returnStr); //this.UserOpenId = obj.openid; if (obj.openid != "") { this.hdopenid.Value = obj.openid; } LogUtil.WriteLog(" ============ 结束 获取微信用户相关信息 ====================="); #endregion } }
protected void Page_Load(object sender, EventArgs e) { try { TravelAgent.Model.WebInfo webinfo = new TravelAgent.BLL.WebInfo().loadConfig(Server.MapPath(ConfigurationManager.AppSettings["WebInfoConfig"].ToString())); if (webinfo != null) { appid = webinfo.AppID; mch_id = webinfo.Mchid; key = webinfo.Key; notify_url = webinfo.WebDomain + "/wxpay/notify_url.aspx"; } //Utils.WriteTxt(appid+"-"+mch_id+"-"+key+"-"+notify_url); var nr = new NativeReceive(); var ns = new NativeSend(); bool valid = nr.ValidSign(nr, key); //Utils.WriteTxt("验证"+valid.ToString()); if (valid) { TravelAgent.Model.Order order = orderBll.GetModelByCode(nr.product_id); if (order == null) { return; } //object r = TravelAgent.WxPay.AccessDbHelper.GetOScalar("select [order_price] from [wx_order] where [order_no]='" + nr.product_id + "'"); //if (r == null) //{ // // Response.Write("用户名或密码错误"); // // Response.End(); // return; //} float order_price; order_price = wxpay.Utils.StrToFloat(((order.orderPrice + order.attachPrice + order.subPrice - order.usePoints)).ToString(), 0) * 100;//积分1分兑换1元 UnifyEntities ue = new UnifyEntities { appid = appid, body = getOrderName(order.lineId, order.orderType), mch_id = mch_id, nonce_str = TravelAgent.WxPay.Utils.GetRandom(), notify_url = notify_url, out_trade_no = nr.product_id, product_id = nr.product_id, spbill_create_ip = JKRequest.GetIP(), trade_type = "NATIVE", total_fee = order_price.ToString() }; string url, sign; string xml = TravelAgent.WxPay.Utils.GetUnifyUrlXml <UnifyEntities>(ue, key, out url, out sign); //Utils.WriteTxt(xml); string data = Utils.HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xml); UnifyReceive unifyReceive = new UnifyReceive(data); NativeSend nc = new NativeSend() { appid = appid, mch_id = mch_id, nonce_str = TravelAgent.WxPay.Utils.GetRandom(), prepay_id = unifyReceive.prepay_id, result_code = "SUCCESS", return_code = "SUCCESS", }; string url1, sign1; string xml1 = TravelAgent.WxPay.Utils.GetUnifyUrlXml <NativeSend>(nc, key, out url1, out sign1); Utils.WriteTxt(xml1); Response.Write(xml1); } else { Utils.WriteTxt("签名验证失败"); NativeSend nc = new NativeSend() { return_code = "FAIL", return_msg = "签名验证失败" }; } } catch (Exception ee) { Utils.WriteTxt(ee.ToString()); } }