| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- using System.Xml;
- namespace SiteCore.Alipay
- {
- /// <summary>
- /// 功能:支付宝接口公用函数类
- /// 详细:该类是请求、通知返回两个文件所调用的公用函数核心处理文件,不需要修改
- /// 版本:3.1
- /// 修改日期:2010-10-29
- /// 说明:
- /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
- /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
- /// </summary>
- public class AlipayFunction
- {
- /// <summary>
- /// 生成签名结果
- /// </summary>
- /// <param name="sArray">要签名的数组</param>
- /// <param name="key">安全校验码</param>
- /// <param name="sign_type">签名类型</param>
- /// <param name="_input_charset">编码格式</param>
- /// <returns>签名结果字符串</returns>
- public static string Build_mysign(Dictionary<string, string> dicArray, string key, string sign_type, string _input_charset)
- {
- string prestr = Create_linkstring(dicArray); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
- //去掉最後一個&字符
- int nLen = prestr.Length;
- prestr = prestr.Substring(0, nLen - 1);
- prestr = prestr + key; //把拼接后的字符串再与安全校验码直接连接起来
- string mysign = Sign(prestr, sign_type, _input_charset); //把最终的字符串签名,获得签名结果
- return mysign;
- }
- /// <summary>
- /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
- /// </summary>
- /// <param name="sArray">需要拼接的数组</param>
- /// <returns>拼接完成以后的字符串</returns>
- public static string Create_linkstring(Dictionary<string, string> dicArray)
- {
- StringBuilder prestr = new StringBuilder();
- foreach (KeyValuePair<string, string> temp in dicArray)
- {
- prestr.Append(temp.Key + "=" + temp.Value + "&");
- }
- return prestr.ToString();
- }
- /// <summary>
- /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
- /// </summary>
- /// <param name="sArray">需要拼接的数组</param>
- /// <param name="code">字符编码</param>
- /// <returns>拼接完成以后的字符串</returns>
- public static string CreateLinkStringUrlencode(Dictionary<string, string> dicArray, Encoding code)
- {
- StringBuilder prestr = new StringBuilder();
- foreach (KeyValuePair<string, string> temp in dicArray)
- {
- prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
- }
- //去掉最後一個&字符
- int nLen = prestr.Length;
- prestr.Remove(nLen - 1, 1);
- return prestr.ToString();
- }
- /// <summary>
- /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
- /// </summary>
- /// <param name="dicArrayPre">过滤前的参数组</param>
- /// <returns>过滤后的参数组</returns>
- public static Dictionary<string, string> Para_filter(SortedDictionary<string, string> dicArrayPre)
- {
- Dictionary<string, string> dicArray = new Dictionary<string, string>();
- foreach (KeyValuePair<string, string> temp in dicArrayPre)
- {
- if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "")
- {
- dicArray.Add(temp.Key.ToLower(), temp.Value);
- }
- }
- return dicArray;
- }
- /// <summary>
- /// 签名字符串
- /// </summary>
- /// <param name="prestr">需要签名的字符串</param>
- /// <param name="sign_type">签名类型</param>
- /// <param name="_input_charset">编码格式</param>
- /// <returns>签名结果</returns>
- public static string Sign(string prestr, string sign_type, string _input_charset)
- {
- StringBuilder sb = new StringBuilder(32);
- if (sign_type.ToUpper() == "MD5")
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
- for (int i = 0; i < t.Length; i++)
- {
- sb.Append(t[i].ToString("x").PadLeft(2, '0'));
- }
- }
- return sb.ToString();
- }
- /// <summary>
- /// 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
- /// </summary>
- /// <param name="sPath">日志的本地绝对路径</param>
- /// <param name="sWord">要写入日志里的文本内容</param>
- public static void log_result(string sPath, string sWord)
- {
- StreamWriter fs = new StreamWriter(sPath, false, Encoding.Default);
- fs.Write(sWord);
- fs.Close();
- }
- /// <summary>
- /// 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
- /// 注意:远程解析XML出错,与IIS服务器配置有关
- /// </summary>
- /// <param name="partner">合作身份者ID</param>
- /// <returns>时间戳字符串</returns>
- public static string Query_timestamp(string partner)
- {
- string url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + partner;
- string encrypt_key = "";
- XmlTextReader Reader = new XmlTextReader(url);
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(Reader);
- encrypt_key = xmlDoc.SelectSingleNode("/alipay/response/timestamp/encrypt_key").InnerText;
- return encrypt_key;
- }
- /// <summary>
- /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
- /// </summary>
- /// <returns>request回来的信息组成的数组</returns>
- public static SortedDictionary<string, string> GetRequestPost(HttpContext context)
- {
- int i = 0;
- SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
- NameValueCollection coll;
- //Load Form variables into NameValueCollection variable.
- coll = context.Request.Form;
- // Get names of all forms into a string array.
- String[] requestItem = coll.AllKeys;
- for (i = 0; i < requestItem.Length; i++)
- {
- sArray.Add(requestItem[i], context.Request.Form[requestItem[i]]);
- }
- return sArray;
- }
-
- }
- }
|