最详细的ASP.NET微信JS(2)

public partial class JsPay : System.Web.UI.Page { public string openid = ""; protected void Page_Load(object sender, EventArgs e) { string code = Request["code"]; if (string.IsNullOrEmpty(code)) { //如果code没获取成功,重新拉取一遍 GetAuthUrl("wxxxxxxxxxxxxxxxxxxxxxxx", "http://www.china101.net/JsPay.aspx"); } openid = GetOpenID("wxxxxxxxxxxxxxxxxxxxxxxx", "dsdssdsdsdsdsdsdsdsdsd", JKRequest.GetQueryString("code"), () => { }); } public string GetOpenID(string appid, string secret, string code, Action CallBack) { try { string retdata = Utils.HttpGet(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code)); .LogHelper.WriteFile(retdata); JObject jobj = (JObject)JsonConvert.DeserializeObject(retdata); string openid = jobj.Value<string>("openid"); return openid; } catch (Exception) { CallBack(); return ""; } // return GetUserInfo(access_token, openid); } /// <summary> /// 获取鉴权地址 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> public void GetAuthUrl(string appid, string redirect_url) { Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=123#wechat_redirect", appid, Utils.UrlEncode(redirect_url))); } }

WxPay.ashx代码:

/// <summary> /// WxPay 的摘要说明 /// </summary> public class WxPay : IHttpHandler { /// <summary> /// 公众账号ID /// </summary> private string appid = "wxxxxxxxxxxxxxxxx"; /// <summary> /// 商户号 /// </summary> private string mch_id = "12333333333"; /// <summary> /// 通知url /// </summary> private string notify_url = "http://www.china101.net/Notify2.aspx"; /// <summary> /// 密钥 /// </summary> private string key = "chinapagexxxxxxxxxxxxx"; public void ProcessRequest(HttpContext context) { string action = JKRequest.GetQueryString("action"); switch (action) { case "unifysign": GetUnifySign(context); break; case "jspayparam": GetJsPayParam(context); break; case "nativedynamic": GetPayQr(context); break; } } #region 获取js支付参数 void GetJsPayParam(HttpContext context) { JsEntities jsEntities = new JsEntities() { appId = appid, nonceStr = .Utils.GetRandom(), package = string.Format("prepay_id={0}", GetPrepayId(context)), signType = "MD5", timeStamp = .Utils.ConvertDateTimeInt(DateTime.Now).ToString() }; string url, sign; string xmlStr = .Utils.GetUnifyRequestXml<JsEntities>(jsEntities, key, out url, out sign); LogHelper.WriteFile(xmlStr); jsEntities.paySign = sign; context.Response.Write(JsonConvert.SerializeObject(jsEntities)); } #endregion #region 获取预支付ID //-------------------------------------------------------------------------- string GetPrepayId(HttpContext context) { string xml; GetUnifySign(context, out xml); LogHelper.WriteFile("GetPrepayId---71--" + xml); UnifyReceive unifyReceive = new UnifyReceive(.Utils.HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xml)); LogHelper.WriteFile("unifyReceive---73--" + unifyReceive.prepay_id); return unifyReceive.prepay_id; } #endregion #region 获取统一签名 void GetUnifySign(HttpContext context) { string xml; context.Response.Write(GetUnifySign(context, out xml)); } #endregion #region 获取统一签名 string GetUnifySign(HttpContext context, out string xml) { string url, sign; xml = WxPayHelper.Utils.GetUnifyUrlXml<UnifyEntities>(GetUnifyEntities(context), key, out url, out sign); return sign; } #endregion #region 获取二维码 void GetPayQr(HttpContext context) { string url = GetPayUrl(context); WxPayHelper.Utils.GetQrCode(url); } #endregion #region 获取二维码链接 string GetPayUrl(HttpContext context) { string xml; GetUnifySign(context, out xml); WxPayHelper.Utils.WriteTxt(xml); UnifyReceive unifyReceive = new UnifyReceive(WxPayHelper.Utils.HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xml)); return unifyReceive.code_url; } #endregion #region 获取统一支付接口参数对象 UnifyEntities GetUnifyEntities(HttpContext context) { string msgid = JKRequest.GetQueryString("msgid"); LogHelper.WriteFile("115---------"+msgid); UnifyEntities unify = new UnifyEntities { appid = appid, body = JKRequest.GetQueryString("body"), mch_id = mch_id, nonce_str = WxPayHelper.Utils.GetRandom(), out_trade_no = JKRequest.GetQueryString("out_trade_no"), notify_url = notify_url, spbill_create_ip = JKRequest.GetIP(), trade_type = JKRequest.GetQueryString("trade_type"), total_fee = JKRequest.GetQueryString("total_fee") }; if (unify.trade_type == "NATIVE") { unify.product_id = msgid; } else { unify.openid = msgid; } return unify; } #endregion public bool IsReusable { get { return false; } } }

Utils.cs代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyzdy.html