Alipaynotify.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using Aop.Api.Util;
  7. using BaseModel.Models;
  8. namespace WayneCloud.PaymentProcessors.Alipay.FromSDK
  9. {
  10. /// <summary>
  11. /// 类名:Notify
  12. /// 功能:支付宝通知处理类
  13. /// 详细:处理支付宝各接口通知返回
  14. /// 版本:3.3
  15. /// 修改日期:2011-07-05
  16. /// '说明:
  17. /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
  18. /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
  19. ///
  20. /// //////////////////////注意/////////////////////////////
  21. /// 调试通知返回时,可查看或改写log日志的写入TXT里的数据,来检查通知返回是否正常
  22. /// </summary>
  23. public class Notify
  24. {
  25. #region 字段
  26. private string _partner = ""; //合作身份者ID
  27. private string _key = ""; //商户的私钥
  28. private string _charset = ""; //编码格式
  29. private string _sign_type = ""; //签名方式
  30. //支付宝消息验证地址
  31. private string Https_veryfy_url = "";
  32. #endregion
  33. /// <summary>
  34. /// 构造函数
  35. /// 从配置文件中初始化变量
  36. /// </summary>
  37. /// <param name="inputPara">通知返回参数数组</param>
  38. /// <param name="notify_id">通知验证ID</param>
  39. public Notify()
  40. {
  41. //Abby
  42. //初始化基础配置信息
  43. //_charset = AliPayConfig.Instances("").charset.Trim().ToLower();
  44. //_sign_type = AliPayConfig.Instances("").sign_type.Trim().ToUpper();
  45. //_partner = AliPayConfig.Instances("").pid;
  46. //Https_veryfy_url = AliPayConfig.Instances("").mapiUrl + "?service=notify_verify&";
  47. }
  48. /// <summary>
  49. /// 验证消息是否是支付宝发出的合法消息
  50. /// </summary>
  51. /// <param name="inputPara">通知返回参数数组</param>
  52. /// <param name="notify_id">通知验证ID</param>
  53. /// <param name="sign">支付宝生成的签名结果</param>
  54. /// <returns>验证结果</returns>
  55. public bool Verify(SortedDictionary<string, string> inputPara, string notify_id, string sign, AliPayConfig config)
  56. {
  57. //获取返回时的签名验证结果
  58. bool isSign = GetSignVeryfy(inputPara, sign, config);
  59. //获取是否是支付宝服务器发来的请求的验证结果
  60. string responseTxt = "true";
  61. if (notify_id != null && notify_id != "") { responseTxt = GetResponseTxt(notify_id); }
  62. //写日志记录(若要调试,请取消下面两行注释)
  63. //string sWord = "responseTxt=" + responseTxt + "\n isSign=" + isSign.ToString() + "\n 返回回来的参数:" + GetPreSignStr(inputPara) + "\n ";
  64. //Core.LogResult(sWord);
  65. //判断responsetTxt是否为true,isSign是否为true
  66. //responsetTxt的结果不是true,与服务器设置问题、合作身份者ID、notify_id一分钟失效有关
  67. //isSign不是true,与安全校验码、请求时的参数格式(如:带自定义参数等)、编码格式有关
  68. if (responseTxt == "true" && isSign)//验证成功
  69. {
  70. return true;
  71. }
  72. else//验证失败
  73. {
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// 获取待签名字符串(调试用)
  79. /// </summary>
  80. /// <param name="inputPara">通知返回参数数组</param>
  81. /// <returns>待签名字符串</returns>
  82. private string GetPreSignStr(SortedDictionary<string, string> inputPara)
  83. {
  84. Dictionary<string, string> sPara = new Dictionary<string, string>();
  85. //过滤空值、sign与sign_type参数
  86. sPara = Core.FilterPara(inputPara);
  87. //获取待签名字符串
  88. string preSignStr = Core.CreateLinkString(sPara);
  89. return preSignStr;
  90. }
  91. /// <summary>
  92. /// 获取返回时的签名验证结果
  93. /// </summary>
  94. /// <param name="inputPara">通知返回参数数组</param>
  95. /// <param name="sign">对比的签名结果</param>
  96. /// <returns>签名验证结果</returns>
  97. private bool GetSignVeryfy(SortedDictionary<string, string> inputPara, string sign, AliPayConfig config)
  98. {
  99. Dictionary<string, string> sPara = new Dictionary<string, string>();
  100. //过滤空值、sign与sign_type参数
  101. sPara = Core.FilterPara(inputPara);
  102. //获取待签名字符串
  103. string preSignStr = Core.CreateLinkString(sPara);
  104. //获得签名验证结果
  105. bool isSign = false;
  106. if (sign != null && sign != "")
  107. {
  108. switch (_sign_type)
  109. {
  110. //isSgin = AlipayMD5.Verify(preSignStr, sign, _key, charset);
  111. case "RSA":
  112. isSign = AlipaySignature.RSACheckContent(preSignStr, sign, config.alipay_public_key, _charset);
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. return isSign;
  119. }
  120. /// <summary>
  121. /// 获取是否是支付宝服务器发来的请求的验证结果
  122. /// </summary>
  123. /// <param name="notify_id">通知验证ID</param>
  124. /// <returns>验证结果</returns>
  125. private string GetResponseTxt(string notify_id)
  126. {
  127. string veryfy_url = Https_veryfy_url + "partner=" + _partner + "&notify_id=" + notify_id;
  128. //获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
  129. string responseTxt = Get_Http(veryfy_url, 120000);
  130. return responseTxt;
  131. }
  132. /// <summary>
  133. /// 获取远程服务器ATN结果
  134. /// </summary>
  135. /// <param name="strUrl">指定URL路径地址</param>
  136. /// <param name="timeout">超时时间设置</param>
  137. /// <returns>服务器ATN结果</returns>
  138. private string Get_Http(string strUrl, int timeout)
  139. {
  140. string strResult;
  141. try
  142. {
  143. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
  144. myReq.Timeout = timeout;
  145. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  146. Stream myStream = HttpWResp.GetResponseStream();
  147. StreamReader sr = new StreamReader(myStream, Encoding.Default);
  148. StringBuilder strBuilder = new StringBuilder();
  149. while (-1 != sr.Peek())
  150. {
  151. strBuilder.Append(sr.ReadLine());
  152. }
  153. strResult = strBuilder.ToString();
  154. }
  155. catch (Exception exp)
  156. {
  157. strResult = "错误:" + exp.Message;
  158. }
  159. return strResult;
  160. }
  161. }
  162. }