AlipayFunction.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. using System.Xml;
  9. namespace SiteCore.Alipay
  10. {
  11. /// <summary>
  12. /// 功能:支付宝接口公用函数类
  13. /// 详细:该类是请求、通知返回两个文件所调用的公用函数核心处理文件,不需要修改
  14. /// 版本:3.1
  15. /// 修改日期:2010-10-29
  16. /// 说明:
  17. /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
  18. /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
  19. /// </summary>
  20. public class AlipayFunction
  21. {
  22. /// <summary>
  23. /// 生成签名结果
  24. /// </summary>
  25. /// <param name="sArray">要签名的数组</param>
  26. /// <param name="key">安全校验码</param>
  27. /// <param name="sign_type">签名类型</param>
  28. /// <param name="_input_charset">编码格式</param>
  29. /// <returns>签名结果字符串</returns>
  30. public static string Build_mysign(Dictionary<string, string> dicArray, string key, string sign_type, string _input_charset)
  31. {
  32. string prestr = Create_linkstring(dicArray); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  33. //去掉最後一個&字符
  34. int nLen = prestr.Length;
  35. prestr = prestr.Substring(0, nLen - 1);
  36. prestr = prestr + key; //把拼接后的字符串再与安全校验码直接连接起来
  37. string mysign = Sign(prestr, sign_type, _input_charset); //把最终的字符串签名,获得签名结果
  38. return mysign;
  39. }
  40. /// <summary>
  41. /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  42. /// </summary>
  43. /// <param name="sArray">需要拼接的数组</param>
  44. /// <returns>拼接完成以后的字符串</returns>
  45. public static string Create_linkstring(Dictionary<string, string> dicArray)
  46. {
  47. StringBuilder prestr = new StringBuilder();
  48. foreach (KeyValuePair<string, string> temp in dicArray)
  49. {
  50. prestr.Append(temp.Key + "=" + temp.Value + "&");
  51. }
  52. return prestr.ToString();
  53. }
  54. /// <summary>
  55. /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
  56. /// </summary>
  57. /// <param name="sArray">需要拼接的数组</param>
  58. /// <param name="code">字符编码</param>
  59. /// <returns>拼接完成以后的字符串</returns>
  60. public static string CreateLinkStringUrlencode(Dictionary<string, string> dicArray, Encoding code)
  61. {
  62. StringBuilder prestr = new StringBuilder();
  63. foreach (KeyValuePair<string, string> temp in dicArray)
  64. {
  65. prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
  66. }
  67. //去掉最後一個&字符
  68. int nLen = prestr.Length;
  69. prestr.Remove(nLen - 1, 1);
  70. return prestr.ToString();
  71. }
  72. /// <summary>
  73. /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
  74. /// </summary>
  75. /// <param name="dicArrayPre">过滤前的参数组</param>
  76. /// <returns>过滤后的参数组</returns>
  77. public static Dictionary<string, string> Para_filter(SortedDictionary<string, string> dicArrayPre)
  78. {
  79. Dictionary<string, string> dicArray = new Dictionary<string, string>();
  80. foreach (KeyValuePair<string, string> temp in dicArrayPre)
  81. {
  82. if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "")
  83. {
  84. dicArray.Add(temp.Key.ToLower(), temp.Value);
  85. }
  86. }
  87. return dicArray;
  88. }
  89. /// <summary>
  90. /// 签名字符串
  91. /// </summary>
  92. /// <param name="prestr">需要签名的字符串</param>
  93. /// <param name="sign_type">签名类型</param>
  94. /// <param name="_input_charset">编码格式</param>
  95. /// <returns>签名结果</returns>
  96. public static string Sign(string prestr, string sign_type, string _input_charset)
  97. {
  98. StringBuilder sb = new StringBuilder(32);
  99. if (sign_type.ToUpper() == "MD5")
  100. {
  101. MD5 md5 = new MD5CryptoServiceProvider();
  102. byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
  103. for (int i = 0; i < t.Length; i++)
  104. {
  105. sb.Append(t[i].ToString("x").PadLeft(2, '0'));
  106. }
  107. }
  108. return sb.ToString();
  109. }
  110. /// <summary>
  111. /// 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
  112. /// </summary>
  113. /// <param name="sPath">日志的本地绝对路径</param>
  114. /// <param name="sWord">要写入日志里的文本内容</param>
  115. public static void log_result(string sPath, string sWord)
  116. {
  117. StreamWriter fs = new StreamWriter(sPath, false, Encoding.Default);
  118. fs.Write(sWord);
  119. fs.Close();
  120. }
  121. /// <summary>
  122. /// 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
  123. /// 注意:远程解析XML出错,与IIS服务器配置有关
  124. /// </summary>
  125. /// <param name="partner">合作身份者ID</param>
  126. /// <returns>时间戳字符串</returns>
  127. public static string Query_timestamp(string partner)
  128. {
  129. string url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + partner;
  130. string encrypt_key = "";
  131. XmlTextReader Reader = new XmlTextReader(url);
  132. XmlDocument xmlDoc = new XmlDocument();
  133. xmlDoc.Load(Reader);
  134. encrypt_key = xmlDoc.SelectSingleNode("/alipay/response/timestamp/encrypt_key").InnerText;
  135. return encrypt_key;
  136. }
  137. /// <summary>
  138. /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
  139. /// </summary>
  140. /// <returns>request回来的信息组成的数组</returns>
  141. public static SortedDictionary<string, string> GetRequestPost(HttpContext context)
  142. {
  143. int i = 0;
  144. SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
  145. NameValueCollection coll;
  146. //Load Form variables into NameValueCollection variable.
  147. coll = context.Request.Form;
  148. // Get names of all forms into a string array.
  149. String[] requestItem = coll.AllKeys;
  150. for (i = 0; i < requestItem.Length; i++)
  151. {
  152. sArray.Add(requestItem[i], context.Request.Form[requestItem[i]]);
  153. }
  154. return sArray;
  155. }
  156. }
  157. }