AlipayNotify.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. namespace SiteCore.Alipay
  7. {
  8. /// <summary>
  9. /// 类名:alipay_notify
  10. /// 功能:付款过程中服务器通知类
  11. /// 详细:该页面是通知返回核心处理文件,不需要修改
  12. /// 版本:3.1
  13. /// 修改日期:2010-10-29
  14. /// '说明:
  15. /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
  16. /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
  17. ///
  18. /// //////////////////////注意/////////////////////////////
  19. /// 调试通知返回时,可查看或改写log日志的写入TXT里的数据,来检查通知返回是否正常
  20. /// </summary>
  21. public class AlipayNotify
  22. {
  23. private readonly string gateway = ""; //网关地址
  24. private readonly string _transport = ""; //访问模式
  25. private readonly string _partner = ""; //合作身份者ID
  26. private readonly string _key = ""; //交易安全校验码
  27. private readonly string _input_charset = ""; //编码格式
  28. private readonly string _sign_type = ""; //签名方式
  29. private readonly string mysign = ""; //签名结果
  30. private readonly string responseTxt = ""; //服务器ATN结果
  31. private readonly Dictionary<string, string> sPara = new Dictionary<string, string>();//要签名的参数组
  32. private readonly string preSignStr = ""; //待签名的字符串
  33. /// <summary>
  34. /// 获取通知返回后计算后(验证)的签名结果
  35. /// </summary>
  36. public string Mysign
  37. {
  38. get { return mysign; }
  39. }
  40. /// <summary>
  41. /// 获取验证是否是支付宝服务器发来的请求结果
  42. /// </summary>
  43. public string ResponseTxt
  44. {
  45. get { return responseTxt; }
  46. }
  47. /// <summary>
  48. /// 获取待签名的字符串(调试用)
  49. /// </summary>
  50. public string PreSignStr
  51. {
  52. get { return preSignStr; }
  53. }
  54. /// <summary>
  55. /// 构造函数
  56. /// 从配置文件中初始化变量
  57. /// </summary>
  58. /// <param name="inputPara">通知返回来的参数数组</param>
  59. /// <param name="notify_id">验证通知ID</param>
  60. /// <param name="partner">合作身份者ID</param>
  61. /// <param name="key">安全校验码</param>
  62. /// <param name="input_charset">编码格式</param>
  63. /// <param name="sign_type">签名类型</param>
  64. /// <param name="transport">访问模式</param>
  65. public AlipayNotify(SortedDictionary<string, string> inputPara, string notify_id, string partner, string key, string input_charset, string sign_type, string transport)
  66. {
  67. _transport = transport;
  68. if (_transport == "https")
  69. {
  70. gateway = "https://www.alipay.com/cooperate/gateway.do?";
  71. }
  72. else
  73. {
  74. gateway = "http://notify.alipay.com/trade/notify_query.do?";
  75. }
  76. _partner = partner.Trim();
  77. _key = key.Trim();
  78. _input_charset = input_charset;
  79. _sign_type = sign_type.ToUpper();
  80. sPara = AlipayFunction.Para_filter(inputPara); //过滤空值、sign与sign_type参数
  81. preSignStr = AlipayFunction.Create_linkstring(sPara); //获取待签名字符串(调试用)
  82. //获得签名结果
  83. mysign = AlipayFunction.Build_mysign(sPara, _key, _sign_type, _input_charset);
  84. //获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
  85. responseTxt = Verify(notify_id);
  86. }
  87. /// <summary>
  88. /// 验证是否是支付宝服务器发来的请求
  89. /// </summary>
  90. /// <returns>验证结果</returns>
  91. private string Verify(string notify_id)
  92. {
  93. string veryfy_url = "";
  94. if (_transport == "https")
  95. {
  96. veryfy_url = gateway + "service=notify_verify&partner=" + _partner + "&notify_id=" + notify_id;
  97. }
  98. else
  99. {
  100. veryfy_url = gateway + "partner=" + _partner + "&notify_id=" + notify_id;
  101. }
  102. return Get_Http(veryfy_url, 120000);
  103. }
  104. /// <summary>
  105. /// 获取远程服务器ATN结果
  106. /// </summary>
  107. /// <param name="strUrl">指定URL路径地址</param>
  108. /// <param name="timeout">超时时间设置</param>
  109. /// <returns>服务器ATN结果</returns>
  110. private string Get_Http(string strUrl, int timeout)
  111. {
  112. string strResult;
  113. try
  114. {
  115. HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
  116. myReq.Timeout = timeout;
  117. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  118. Stream myStream = HttpWResp.GetResponseStream();
  119. StreamReader sr = new StreamReader(myStream, Encoding.Default);
  120. StringBuilder strBuilder = new StringBuilder();
  121. while (-1 != sr.Peek())
  122. {
  123. strBuilder.Append(sr.ReadLine());
  124. }
  125. strResult = strBuilder.ToString();
  126. }
  127. catch (Exception exp)
  128. {
  129. strResult = "错误:" + exp.Message;
  130. }
  131. return strResult;
  132. }
  133. }
  134. }