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
{
///
/// 功能:支付宝接口公用函数类
/// 详细:该类是请求、通知返回两个文件所调用的公用函数核心处理文件,不需要修改
/// 版本:3.1
/// 修改日期:2010-10-29
/// 说明:
/// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
/// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
///
public class AlipayFunction
{
///
/// 生成签名结果
///
/// 要签名的数组
/// 安全校验码
/// 签名类型
/// 编码格式
/// 签名结果字符串
public static string Build_mysign(Dictionary 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;
}
///
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
///
/// 需要拼接的数组
/// 拼接完成以后的字符串
public static string Create_linkstring(Dictionary dicArray)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair temp in dicArray)
{
prestr.Append(temp.Key + "=" + temp.Value + "&");
}
return prestr.ToString();
}
///
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
///
/// 需要拼接的数组
/// 字符编码
/// 拼接完成以后的字符串
public static string CreateLinkStringUrlencode(Dictionary dicArray, Encoding code)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair temp in dicArray)
{
prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
}
//去掉最後一個&字符
int nLen = prestr.Length;
prestr.Remove(nLen - 1, 1);
return prestr.ToString();
}
///
/// 除去数组中的空值和签名参数并以字母a到z的顺序排序
///
/// 过滤前的参数组
/// 过滤后的参数组
public static Dictionary Para_filter(SortedDictionary dicArrayPre)
{
Dictionary dicArray = new Dictionary();
foreach (KeyValuePair temp in dicArrayPre)
{
if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "")
{
dicArray.Add(temp.Key.ToLower(), temp.Value);
}
}
return dicArray;
}
///
/// 签名字符串
///
/// 需要签名的字符串
/// 签名类型
/// 编码格式
/// 签名结果
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();
}
///
/// 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
///
/// 日志的本地绝对路径
/// 要写入日志里的文本内容
public static void log_result(string sPath, string sWord)
{
StreamWriter fs = new StreamWriter(sPath, false, Encoding.Default);
fs.Write(sWord);
fs.Close();
}
///
/// 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数
/// 注意:远程解析XML出错,与IIS服务器配置有关
///
/// 合作身份者ID
/// 时间戳字符串
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;
}
///
/// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组
///
/// request回来的信息组成的数组
public static SortedDictionary GetRequestPost(HttpContext context)
{
int i = 0;
SortedDictionary sArray = new SortedDictionary();
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;
}
}
}