1. 程式人生 > >微信小程式之微信支付C#後臺(統一下單)

微信小程式之微信支付C#後臺(統一下單)

#region 生成簽名
/// <summary>
/// 獲取簽名資料
///</summary>
/// <param name="strParam"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetSignInfo(Dictionary<string, string> strParam, string key)
{
    int i = 0;
    string sign = string
.Empty; StringBuilder sb = new StringBuilder(); try { foreach (KeyValuePair<string, string> temp in strParam) { if (temp.Value == "" || temp.Value == null || temp.Key.ToLower() == "sign") { continue; } i
++; sb.Append(temp.Key.Trim() + "=" + temp.Value.Trim() + "&"); } sb.Append("key=" + key.Trim() + ""); sign = MD5Core.GetHashString(sb.ToString(), Encoding.UTF8).ToUpper(); } catch (Exception ex) { Utility.AddLog("PayHelper", "GetSignInfo
", ex.Message, ex); } return sign; } #endregion #region XML 處理 /// <summary> /// 獲取XML值 /// </summary> /// <param name="strXml">XML字串</param> /// <param name="strData">欄位值</param> /// <returns></returns> public static string GetXmlValue(string strXml, string strData) { string xmlValue = string.Empty; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(strXml); var selectSingleNode = xmlDocument.DocumentElement.SelectSingleNode(strData); if (selectSingleNode != null) { xmlValue = selectSingleNode.InnerText; } return xmlValue; } /// <summary> /// 集合轉換XML資料 (拼接成XML請求資料) /// </summary> /// <param name="strParam">引數</param> /// <returns></returns> public static string CreateXmlParam(Dictionary<string, string> strParam) { StringBuilder sb = new StringBuilder(); try { sb.Append("<xml>"); foreach (KeyValuePair<string, string> k in strParam) { if (k.Key == "attach" || k.Key == "body" || k.Key == "sign") { sb.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">"); } else { sb.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">"); } } sb.Append("</xml>"); } catch (Exception ex) { Utility.AddLog("PayHelper", "CreateXmlParam", ex.Message, ex); } return sb.ToString(); } /// <summary> /// XML資料轉換集合(XML資料拼接成字串) /// </summary> /// <param name="xmlString"></param> /// <returns></returns> public static Dictionary<string, string> GetFromXml(string xmlString) { Dictionary<string, string> sParams = new Dictionary<string, string>(); try { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlString); XmlElement root = doc.DocumentElement; int len = root.ChildNodes.Count; for (int i = 0; i < len; i++) { string name = root.ChildNodes[i].Name; if (!sParams.ContainsKey(name)) { sParams.Add(name.Trim(), root.ChildNodes[i].InnerText.Trim()); } } } catch (Exception ex) { Utility.AddLog("PayHelper", "GetFromXml", ex.Message, ex); } return sParams; } /// <summary> /// 返回通知 XML /// </summary> /// <param name="returnCode"></param> /// <param name="returnMsg"></param> /// <returns></returns> public static string GetReturnXml(string returnCode, string returnMsg) { StringBuilder sb = new StringBuilder(); sb.Append("<xml>"); sb.Append("<return_code><![CDATA[" + returnCode + "]]></return_code>"); sb.Append("<return_msg><![CDATA[" + returnMsg + "]]></return_msg>"); sb.Append("</xml>"); return sb.ToString(); } #endregion