<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
最近挺忙的,沒時間寫東西。然後在弄微信APP支付,網上的搜尋一趟,都比較凌亂,我也遇到一些坑,不過也算弄好了,記錄分享一下。
1、準備各種呼叫介面需要的引數,設定app.config。
<!--AppID--> <add key="AppID" value="" /> <!--AppSecret--> <add key="AppSecret" value="" /> <!--商戶號--> <add key="MchID" value="" /> <!--APIKey--> <add key="APIKey" value="" />
2、呼叫統一下單API,直接上程式碼了。
/// <summary> /// Builds the request. /// </summary> /// <returns></returns> public static string BuildRequest() { var dicParam = CreateParam(); var signString = CreateURLParamString(dicParam); var preString = signString + "&key=" + ConfigHelper.APIKey; var sign = Sign(preString, "utf-8").ToUpper(); dicParam.Add("sign", sign); return BuildForm(dicParam); } /// <summary> /// Generates the out trade no. /// </summary> /// <returns></returns> private static string GenerateOutTradeNo() { var ran = new Random(); return $"{ConfigHelper.MchID}{DateTime.Now:yyyyMMddHHmmss}{ran.Next(999)}"; } /// <summary> /// Signs the specified prestr. /// </summary> /// <param name="prestr">The prestr.</param> /// <param name="_input_charset">The input charset.</param> /// <returns></returns> private static string Sign(string prestr, string _input_charset) { var sb = new StringBuilder(32); MD5 md5 = new MD5CryptoServiceProvider(); var t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr)); foreach (var t1 in t) { sb.Append(t1.ToString("x").PadLeft(2, '0')); } return sb.ToString(); } /// <summary> /// Creates the parameter. /// </summary> /// <returns></returns> private static SortedDictionary<string, string> CreateParam() { const string amount = "1"; double dubamount; double.TryParse(amount, out dubamount); var notify_url = ConfigHelper.WebSiteUrl + "/api/v1/testWeiXin"; //支付完成後的回撥處理頁面 const string detail = "xxxx"; var dic = new SortedDictionary<string, string> { {"appid", ConfigHelper.AppID},//賬號ID {"mch_id", ConfigHelper.MchID},//商戶號 {"nonce_str", Guid.NewGuid().ToString().Replace("-", "")},//隨機字串 {"body", detail}, //商品描述 {"out_trade_no", GenerateOutTradeNo()},//商戶訂單號 {"total_fee", (dubamount * 100).ToString(CultureInfo.InvariantCulture)},//總金額 {"spbill_create_ip", GeneralHelper.GetIP()},//終端IP {"notify_url", notify_url},//通知地址 {"trade_type", "APP"}//交易型別 }; return dic; } /// <summary> /// Creates the URL parameter string. /// </summary> /// <param name="dicArray">The dic array.</param> /// <returns></returns> private static string CreateURLParamString(SortedDictionary<string, string> dicArray) { var prestr = new StringBuilder(); foreach (var temp in dicArray.OrderBy(o => o.Key)) { prestr.Append(temp.Key + "=" + temp.Value + "&"); } var nLen = prestr.Length; prestr.Remove(nLen - 1, 1); return prestr.ToString(); } /// <summary> /// Builds the form. /// </summary> /// <param name="dicParam">The dic parameter.</param> /// <returns></returns> private static string BuildForm(SortedDictionary<string, string> dicParam) { var sbXML = new StringBuilder(); sbXML.Append("<xml>"); foreach (var temp in dicParam) { sbXML.Append("<" + temp.Key + ">" + temp.Value + "</" + temp.Key + ">"); } sbXML.Append("</xml>"); return sbXML.ToString(); } /// <summary> /// Froms the XML. /// </summary> /// <param name="xml">The XML.</param> /// <returns></returns> /// <exception cref="Exception">將空的xml串轉換為WxPayData不合法!</exception> public static SortedDictionary<string, string> FromXml(string xml) { var sortDic = new SortedDictionary<string, string>(); if (string.IsNullOrEmpty(xml)) { throw new Exception("將空的xml串轉換為WxPayData不合法!"); } var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); var xmlNode = xmlDoc.FirstChild;//獲取到根節點<xml> var nodes = xmlNode.ChildNodes; foreach (XmlNode xn in nodes) { var xe = (XmlElement)xn; if (!sortDic.ContainsKey(xe.Name)) sortDic.Add(xe.Name, xe.InnerText); } return sortDic; } /// <summary> /// Posts the specified URL. /// </summary> /// <param name="url">The URL.</param> /// <param name="content">The content.</param> /// <param name="contentType">Type of the content.</param> /// <returns></returns> /// <exception cref="Exception">POST請求錯誤" + e</exception> public static string Post(string url, string content, string contentType = "application/x-www-form-urlencoded") { string result; try { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType)); var stringContent = new StringContent(content, Encoding.UTF8); var response = client.PostAsync(url, stringContent).Result; result = response.Content.ReadAsStringAsync().Result; } } catch (Exception e) { throw new Exception("POST請求錯誤" + e); } return result; }
3、生成預付訂單,獲取prepay_id。
/// <summary> /// Gets the value from dic. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dic">The dic.</param> /// <param name="key">The key.</param> /// <returns></returns> public static T GetValueFromDic<T>(IDictionary<string, string> dic, string key) { string val; dic.TryGetValue(key, out val); var returnVal = default(T); if (val != null) returnVal = (T)Convert.ChangeType(val, typeof(T)); return returnVal; } /// <summary> /// Builds the application pay. /// </summary> /// <param name="prepayid">The prepayid.</param> /// <returns></returns> public static string BuildAppPay(string prepayid) { var dicParam = CreateWapAndAppPayParam(prepayid); var signString = CreateURLParamString(dicParam); var preString = signString + "&key=" + ConfigHelper.APIKey; var sign = Sign(preString, "utf-8").ToUpper(); dicParam.Add("sign", sign); return JsonConvert.SerializeObject( new { appid = dicParam["appid"], partnerid = dicParam["partnerid"], prepayid = dicParam["prepayid"], package = dicParam["package"], noncestr = dicParam["noncestr"], timestamp = dicParam["timestamp"], sign = dicParam["sign"] }); } /// <summary> /// Creates the wap and application pay parameter. /// </summary> /// <param name="prepayId">The prepay identifier.</param> /// <returns></returns> private static SortedDictionary<string, string> CreateWapAndAppPayParam(string prepayId) { var dic = new SortedDictionary<string, string> { {"appid", ConfigHelper.AppID},//公眾賬號ID {"partnerid", ConfigHelper.MchID},//商戶號 {"prepayid", prepayId},//預支付交易對談ID {"package", "Sign=WXPay"},//擴充套件欄位 {"noncestr", Guid.NewGuid().ToString().Replace("-", "")},//隨機字串 { "timestamp", (Convert.ToInt32((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds)).ToString() }//時間戳 }; return dic; } /// <summary> /// Validatons the query result. /// </summary> /// <param name="dic">The dic.</param> /// <returns></returns> public static bool ValidatonQueryResult(SortedDictionary<string, string> dic) { var result = false; if (dic.ContainsKey("return_code") && dic.ContainsKey("return_code")) { if (dic["return_code"] == "SUCCESS" && dic["result_code"] == "SUCCESS") result = true; } if (result) return true; var sb = new StringBuilder(); foreach (var item in dic.Keys) { sb.Append(item + ":" + dic[item] + "|"); } return false; }
4、呼叫獲取支付資訊,給到APP發起支付操作。
var requestXml = WeiXinUtil.BuildRequest(); var resultXml = WeiXinUtil.Post("https://api.mch.weixin.qq.com/pay/unifiedorder", requestXml); var dic = WeiXinUtil.FromXml(resultXml); string returnCode; dic.TryGetValue("return_code", out returnCode); if (returnCode == "SUCCESS") { var prepay_id = WeiXinUtil.GetValueFromDic<string>(dic, "prepay_id"); if (!string.IsNullOrEmpty(prepay_id)) { var payInfo = JsonConvert.DeserializeObject<WeiXinUtil.WxPayModel>(WeiXinUtil.BuildAppPay(prepay_id)); json.Add(new JProperty("appid", payInfo.appid)); json.Add(new JProperty("partnerid", payInfo.partnerid)); json.Add(new JProperty("prepayid", payInfo.prepayid)); json.Add(new JProperty("package", payInfo.package)); json.Add(new JProperty("noncestr", payInfo.noncestr)); json.Add(new JProperty("timestamp", payInfo.timestamp)); json.Add(new JProperty("sign", payInfo.sign)); json.Add(new JProperty("code", 0)); json.Add(new JProperty("msg", "成功")); return this.Jsonp(json.ToString()); } else { json.Add(new JProperty("code", 40028)); json.Add(new JProperty("msg", "支付錯誤:" + WeiXinUtil.GetValueFromDic<string>(dic, "err_code_des"))); return this.Jsonp(json.ToString()); } } else { return this.Jsonp(ApiException.OrderFailed()); }
5、APP支付完成,獲得回撥資訊,就OK了。
var context = this.HttpContext; var request = context.Request; var verifyResult = false; var requestXml = WeiXinUtil.GetRequestXmlData(request); var dic = WeiXinUtil.FromXml(requestXml); var returnCode = WeiXinUtil.GetValueFromDic<string>(dic, "return_code"); if (!string.IsNullOrEmpty(returnCode) && returnCode == "SUCCESS")//通訊成功 { var result = WeiXinUtil.WePayNotifyValidation(dic); if (result) { var transactionid = WeiXinUtil.GetValueFromDic<string>(dic, "transaction_id"); if (!string.IsNullOrEmpty(transactionid)) { var queryXml = WeiXinUtil.BuildQueryRequest(transactionid, dic); var queryResult = WeiXinUtil.Post("https://api.mch.weixin.qq.com/pay/orderquery", queryXml); var queryReturnDic = WeiXinUtil.FromXml(queryResult); if (WeiXinUtil.ValidatonQueryResult(queryReturnDic))//查詢成功 { verifyResult = true; var status = WeiXinUtil.GetValueFromDic<string>(dic, "result_code"); if (!string.IsNullOrEmpty(status) && status == "SUCCESS") { var order = new Order() { OrderNumber = WeiXinUtil.GetValueFromDic<string>(dic, "out_trade_no"), TransactionId = transactionid, ProductPrice = WeiXinUtil.GetValueFromDic<decimal>(dic, "total_fee") / 100, TradeType = WeiXinUtil.GetValueFromDic<string>(dic, "trade_type"), BankType = WeiXinUtil.GetValueFromDic<string>(dic, "bank_type"), PayDate = DateTime.Parse(WeiXinUtil.GetValueFromDic<string>(dic, "time_end")), StatusId = 1, IsPresent = false, AddDate = DateTime.Now, IsDelete = false }; CURD.Add(order, ConfigHelper.WriteDB); WeiXinUtil.BuildReturnXml("OK", "成功"); } } else WeiXinUtil.BuildReturnXml("FAIL", "訂單查詢失敗"); } else WeiXinUtil.BuildReturnXml("FAIL", "支付結果中微信訂單號不存在"); } else WeiXinUtil.BuildReturnXml("FAIL", "簽名失敗"); } else { string returnmsg; dic.TryGetValue("return_msg", out returnmsg); throw new Exception("非同步通知錯誤:" + returnmsg); } return verifyResult; /// <summary> /// Gets the request XML data. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> public static string GetRequestXmlData(HttpRequestBase request) { var stream = request.InputStream; int count; var buffer = new byte[1024]; var builder = new StringBuilder(); while ((count = stream.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } stream.Flush(); stream.Close(); return builder.ToString(); } /// <summary> /// Wes the pay notify validation. /// </summary> /// <param name="dic">The dic.</param> /// <returns></returns> public static bool WePayNotifyValidation(SortedDictionary<string, string> dic) { var sign = GetValueFromDic<string>(dic, "sign"); if (dic.ContainsKey("sign")) { dic.Remove("sign"); } var tradeType = GetValueFromDic<string>(dic, "trade_type"); var preString = CreateURLParamString(dic); if (string.IsNullOrEmpty(tradeType)) { var preSignString = preString + "&key=" + ConfigHelper.APIKey; var signString = Sign(preSignString, "utf-8").ToUpper(); return signString == sign; } else return false; } /// <summary> /// Builds the query request. /// </summary> /// <param name="transactionId">The transaction identifier.</param> /// <param name="dic">The dic.</param> /// <returns></returns> public static string BuildQueryRequest(string transactionId, SortedDictionary<string, string> dic) { var dicParam = CreateQueryParam(transactionId); var signString = CreateURLParamString(dicParam); var key = ConfigHelper.APIKey; var preString = signString + "&key=" + key; var sign = Sign(preString, "utf-8").ToUpper(); dicParam.Add("sign", sign); return BuildForm(dicParam); } /// <summary> /// Creates the query parameter. /// </summary> /// <param name="transactionId">The transaction identifier.</param> /// <returns></returns> private static SortedDictionary<string, string> CreateQueryParam(string transactionId) { var dic = new SortedDictionary<string, string> { {"appid", ConfigHelper.AppID},//公眾賬號ID {"mch_id", ConfigHelper.MchID},//商戶號 {"nonce_str", Guid.NewGuid().ToString().Replace("-", "")},//隨機字串 {"transaction_id", transactionId}//微信訂單號 }; return dic; } /// <summary> /// Builds the return XML. /// </summary> /// <param name="code">The code.</param> /// <param name="returnMsg">The return MSG.</param> /// <returns></returns> public static string BuildReturnXml(string code, string returnMsg) { return $"<xml><return_code><![CDATA[[code]]]></return_code><return_msg><![CDATA[{returnMsg}]]></return_msg></xml>"; }
6、總結:這個可以直接拿來用了,反反覆覆測試了很多遍,遇到的問題有關於錢,還有簽名的問題,偵錯都解決了。繼續解決問題,積累經驗。
到此這篇關於ASP.Net專案中實現微信APP支付功能的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45