diff --git a/App.code/Pay/AlipayConfig.cs b/App.code/Pay/AlipayConfig.cs new file mode 100644 index 0000000..5e8f74a --- /dev/null +++ b/App.code/Pay/AlipayConfig.cs @@ -0,0 +1,63 @@ +using System.Web; +using System.Text; +using System.IO; +using System.Net; +using System; +using System.Collections.Generic; + +namespace Com.Alipay +{ + /// + /// 类名:Config + /// 功能:基础配置类 + /// 详细:设置帐户有关信息及返回路径 + /// 版本:3.3 + /// 日期:2012-07-05 + /// 说明: + /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 + /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 + /// + /// 如何获取安全校验码和合作身份者ID + /// 1.用您的签约支付宝账号登录支付宝网站(www.alipay.com) + /// 2.点击“商家服务”(https://b.alipay.com/order/myOrder.htm) + /// 3.点击“查询合作者身份(PID)”、“查询安全校验码(Key)” + /// + public class Config + { + #region 字段 + private static string input_charset = ""; + private static string sign_type = ""; + #endregion + + static Config() + { + + + //字符编码格式 目前支持 gbk 或 utf-8 + input_charset = "utf-8"; + + //签名方式,选择项:RSA、DSA、MD5 + sign_type = "MD5"; + } + + #region 属性 + + + /// + /// 获取字符编码格式 + /// + public static string Input_charset + { + get { return input_charset; } + } + + /// + /// 获取签名方式 + /// + public static string Sign_type + { + get { return sign_type; } + } + #endregion + } +} \ No newline at end of file diff --git a/App.code/Pay/AlipayCore.cs b/App.code/Pay/AlipayCore.cs new file mode 100644 index 0000000..53bc04c --- /dev/null +++ b/App.code/Pay/AlipayCore.cs @@ -0,0 +1,136 @@ +using System.Web; +using System.Text; +using System.Security.Cryptography; +using System.IO; +using System.Net; +using System; +using System.Collections.Generic; +using System.Xml; + +namespace Com.Alipay +{ + /// + /// 类名:Core + /// 功能:支付宝接口公用函数类 + /// 详细:该类是请求、通知返回两个文件所调用的公用函数核心处理文件,不需要修改 + /// 版本:3.3 + /// 修改日期:2012-07-05 + /// 说明: + /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 + /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 + /// + public class Core + { + + public Core() + { + } + + /// + /// 除去数组中的空值和签名参数并以字母a到z的顺序排序 + /// + /// 过滤前的参数组 + /// 过滤后的参数组 + public static Dictionary FilterPara(SortedDictionary dicArrayPre) + { + Dictionary dicArray = new Dictionary(); + foreach (KeyValuePair temp in dicArrayPre) + { + if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null) + { + dicArray.Add(temp.Key, temp.Value); + } + } + + return dicArray; + } + + /// + /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 + /// + /// 需要拼接的数组 + /// 拼接完成以后的字符串 + public static string CreateLinkString(Dictionary dicArray) + { + StringBuilder prestr = new StringBuilder(); + foreach (KeyValuePair temp in dicArray) + { + prestr.Append(temp.Key + "=" + temp.Value + "&"); + } + + //去掉最後一個&字符 + int nLen = prestr.Length; + prestr.Remove(nLen-1,1); + + 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(); + } + + /// + /// 写日志,方便测试(看网站需求,也可以改成把记录存入数据库) + /// + /// 要写入日志里的文本内容 + public static void LogResult(string sWord) + { + string strPath = HttpContext.Current.Server.MapPath("log"); + strPath = strPath + "\\" + DateTime.Now.ToString().Replace(":", "") + ".txt"; + StreamWriter fs = new StreamWriter(strPath, false, System.Text.Encoding.Default); + fs.Write(sWord); + fs.Close(); + } + + /// + /// 获取文件的md5摘要 + /// + /// 文件流 + /// MD5摘要结果 + public static string GetAbstractToMD5(Stream sFile) + { + MD5 md5 = new MD5CryptoServiceProvider(); + byte[] result = md5.ComputeHash(sFile); + StringBuilder sb = new StringBuilder(32); + for (int i = 0; i < result.Length; i++) + { + sb.Append(result[i].ToString("x").PadLeft(2, '0')); + } + return sb.ToString(); + } + + /// + /// 获取文件的md5摘要 + /// + /// 文件流 + /// MD5摘要结果 + public static string GetAbstractToMD5(byte[] dataFile) + { + MD5 md5 = new MD5CryptoServiceProvider(); + byte[] result = md5.ComputeHash(dataFile); + StringBuilder sb = new StringBuilder(32); + for (int i = 0; i < result.Length; i++) + { + sb.Append(result[i].ToString("x").PadLeft(2, '0')); + } + return sb.ToString(); + } + } +} \ No newline at end of file diff --git a/App.code/Pay/AlipayNotify.cs b/App.code/Pay/AlipayNotify.cs new file mode 100644 index 0000000..2a30351 --- /dev/null +++ b/App.code/Pay/AlipayNotify.cs @@ -0,0 +1,185 @@ +using System.Web; +using System.Text; +using System.IO; +using System.Net; +using System; +using System.Collections.Generic; + +namespace Com.Alipay +{ + /// + /// 类名:Notify + /// 功能:支付宝通知处理类 + /// 详细:处理支付宝各接口通知返回 + /// 版本:3.3 + /// 修改日期:2011-07-05 + /// '说明: + /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 + /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 + /// + /// //////////////////////注意///////////////////////////// + /// 调试通知返回时,可查看或改写log日志的写入TXT里的数据,来检查通知返回是否正常 + /// + public class Notify + { + #region 字段 + private string _partner = ""; //合作身份者ID + private string _key = ""; //商户的私钥 + private string _input_charset = ""; //编码格式 + private string _sign_type = ""; //签名方式 + + //支付宝消息验证地址 + private string Https_veryfy_url = "https://mapi.alipay.com/gateway.do?service=notify_verify&"; + #endregion + + + /// + /// 构造函数 + /// 从配置文件中初始化变量 + /// + /// 通知返回参数数组 + /// 通知验证ID + public Notify() + { + + Basic.BLL.siteconfig bll = new Basic.BLL.siteconfig(); + Basic.Model.siteconfig model = bll.loadConfig(Basic.Tools.Utils.GetXmlMapPath(Basic.Keys.FILE_SITE_XML_CONFING)); + + //初始化基础配置信息 + _partner = model.Alipay_partner; + _key = model.Alipay_key; + _input_charset = Config.Input_charset.Trim().ToLower(); + _sign_type = Config.Sign_type.Trim().ToUpper(); + } + + /// + /// 验证消息是否是支付宝发出的合法消息 + /// + /// 通知返回参数数组 + /// 通知验证ID + /// 支付宝生成的签名结果 + /// 验证结果 + public bool Verify(SortedDictionary inputPara, string notify_id, string sign) + { + //获取返回时的签名验证结果 + bool isSign = GetSignVeryfy(inputPara, sign); + //获取是否是支付宝服务器发来的请求的验证结果 + string responseTxt = "true"; + if (notify_id != null && notify_id != "") { responseTxt = GetResponseTxt(notify_id); } + + //写日志记录(若要调试,请取消下面两行注释) + //string sWord = "responseTxt=" + responseTxt + "\n isSign=" + isSign.ToString() + "\n 返回回来的参数:" + GetPreSignStr(inputPara) + "\n "; + //Core.LogResult(sWord); + + //判断responsetTxt是否为true,isSign是否为true + //responsetTxt的结果不是true,与服务器设置问题、合作身份者ID、notify_id一分钟失效有关 + //isSign不是true,与安全校验码、请求时的参数格式(如:带自定义参数等)、编码格式有关 + if (responseTxt == "true" && isSign)//验证成功 + { + return true; + } + else//验证失败 + { + return false; + } + } + + /// + /// 获取待签名字符串(调试用) + /// + /// 通知返回参数数组 + /// 待签名字符串 + private string GetPreSignStr(SortedDictionary inputPara) + { + Dictionary sPara = new Dictionary(); + + //过滤空值、sign与sign_type参数 + sPara = Core.FilterPara(inputPara); + + //获取待签名字符串 + string preSignStr = Core.CreateLinkString(sPara); + + return preSignStr; + } + + /// + /// 获取返回时的签名验证结果 + /// + /// 通知返回参数数组 + /// 对比的签名结果 + /// 签名验证结果 + private bool GetSignVeryfy(SortedDictionary inputPara, string sign) + { + Dictionary sPara = new Dictionary(); + + //过滤空值、sign与sign_type参数 + sPara = Core.FilterPara(inputPara); + + //获取待签名字符串 + string preSignStr = Core.CreateLinkString(sPara); + + //获得签名验证结果 + bool isSgin = false; + if (sign != null && sign != "") + { + switch (_sign_type) + { + case "MD5": + isSgin = AlipayMD5.Verify(preSignStr, sign, _key, _input_charset); + break; + default: + break; + } + } + + return isSgin; + } + + /// + /// 获取是否是支付宝服务器发来的请求的验证结果 + /// + /// 通知验证ID + /// 验证结果 + private string GetResponseTxt(string notify_id) + { + string veryfy_url = Https_veryfy_url + "partner=" + _partner + "¬ify_id=" + notify_id; + + //获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求 + string responseTxt = Get_Http(veryfy_url, 120000); + + return responseTxt; + } + + /// + /// 获取远程服务器ATN结果 + /// + /// 指定URL路径地址 + /// 超时时间设置 + /// 服务器ATN结果 + private string Get_Http(string strUrl, int timeout) + { + string strResult; + try + { + HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); + myReq.Timeout = timeout; + HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); + Stream myStream = HttpWResp.GetResponseStream(); + StreamReader sr = new StreamReader(myStream, Encoding.Default); + StringBuilder strBuilder = new StringBuilder(); + while (-1 != sr.Peek()) + { + strBuilder.Append(sr.ReadLine()); + } + + strResult = strBuilder.ToString(); + } + catch (Exception exp) + { + strResult = "错误:" + exp.Message; + } + + return strResult; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/AlipaySubmit.cs b/App.code/Pay/AlipaySubmit.cs new file mode 100644 index 0000000..f2b7211 --- /dev/null +++ b/App.code/Pay/AlipaySubmit.cs @@ -0,0 +1,309 @@ +using System.Web; +using System.Text; +using System.IO; +using System.Net; +using System; +using System.Collections.Generic; +using System.Xml; + +namespace Com.Alipay +{ + /// + /// 类名:Submit + /// 功能:支付宝各接口请求提交类 + /// 详细:构造支付宝各接口表单HTML文本,获取远程HTTP数据 + /// 版本:3.3 + /// 修改日期:2011-07-05 + /// 说明: + /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 + /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考 + /// + public class Submit + { + #region 字段 + //支付宝网关地址(新) + private static string GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?"; + //商户的私钥 + private static string _key = ""; + //编码格式 + private static string _input_charset = ""; + //签名方式 + private static string _sign_type = ""; + #endregion + + static Submit() + { + Basic.BLL.siteconfig bll = new Basic.BLL.siteconfig(); + Basic.Model.siteconfig model = bll.loadConfig(Basic.Tools.Utils.GetXmlMapPath(Basic.Keys.FILE_SITE_XML_CONFING)); + _key = model.Alipay_key; + _input_charset = Config.Input_charset.Trim().ToLower(); + _sign_type = Config.Sign_type.Trim().ToUpper(); + } + + /// + /// 生成请求时的签名 + /// + /// 请求给支付宝的参数数组 + /// 签名结果 + private static string BuildRequestMysign(Dictionary sPara) + { + //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 + string prestr = Core.CreateLinkString(sPara); + + //把最终的字符串签名,获得签名结果 + string mysign = ""; + switch (_sign_type) + { + case "MD5": + mysign = AlipayMD5.Sign(prestr, _key, _input_charset); + break; + default: + mysign = ""; + break; + } + + return mysign; + } + + /// + /// 生成要请求给支付宝的参数数组 + /// + /// 请求前的参数数组 + /// 要请求的参数数组 + private static Dictionary BuildRequestPara(SortedDictionary sParaTemp) + { + //待签名请求参数数组 + Dictionary sPara = new Dictionary(); + //签名结果 + string mysign = ""; + + //过滤签名参数数组 + sPara = Core.FilterPara(sParaTemp); + + //获得签名结果 + mysign = BuildRequestMysign(sPara); + + //签名结果与签名方式加入请求提交参数组中 + sPara.Add("sign", mysign); + sPara.Add("sign_type", _sign_type); + + return sPara; + } + + /// + /// 生成要请求给支付宝的参数数组 + /// + /// 请求前的参数数组 + /// 字符编码 + /// 要请求的参数数组字符串 + private static string BuildRequestParaToString(SortedDictionary sParaTemp, Encoding code) + { + //待签名请求参数数组 + Dictionary sPara = new Dictionary(); + sPara = BuildRequestPara(sParaTemp); + + //把参数组中所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode + string strRequestData = Core.CreateLinkStringUrlencode(sPara, code); + + return strRequestData; + } + + /// + /// 建立请求,以表单HTML形式构造(默认) + /// + /// 请求参数数组 + /// 提交方式。两个值可选:post、get + /// 确认按钮显示文字 + /// 提交表单HTML文本 + public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string strButtonValue) + { + //待请求参数数组 + Dictionary dicPara = new Dictionary(); + dicPara = BuildRequestPara(sParaTemp); + + StringBuilder sbHtml = new StringBuilder(); + + sbHtml.Append("
"); + + foreach (KeyValuePair temp in dicPara) + { + sbHtml.Append(""); + } + + //submit按钮控件请不要含有name属性 + sbHtml.Append(""); + + sbHtml.Append(""); + + return sbHtml.ToString(); + } + + + /// + /// 建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果 + /// + /// 请求参数数组 + /// 支付宝处理结果 + public static string BuildRequest(SortedDictionary sParaTemp) + { + Encoding code = Encoding.GetEncoding(_input_charset); + + //待请求参数数组字符串 + string strRequestData = BuildRequestParaToString(sParaTemp,code); + + //把数组转换成流中所需字节数组类型 + byte[] bytesRequestData = code.GetBytes(strRequestData); + + //构造请求地址 + string strUrl = GATEWAY_NEW + "_input_charset=" + _input_charset; + + //请求远程HTTP + string strResult = ""; + try + { + //设置HttpWebRequest基本信息 + HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl); + myReq.Method = "post"; + myReq.ContentType = "application/x-www-form-urlencoded"; + + //填充POST数据 + myReq.ContentLength = bytesRequestData.Length; + Stream requestStream = myReq.GetRequestStream(); + requestStream.Write(bytesRequestData, 0, bytesRequestData.Length); + requestStream.Close(); + + //发送POST数据请求服务器 + HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); + Stream myStream = HttpWResp.GetResponseStream(); + + //获取服务器返回信息 + StreamReader reader = new StreamReader(myStream, code); + StringBuilder responseData = new StringBuilder(); + String line; + while ((line = reader.ReadLine()) != null) + { + responseData.Append(line); + } + + //释放 + myStream.Close(); + + strResult = responseData.ToString(); + } + catch (Exception exp) + { + strResult = "报错:"+exp.Message; + } + + return strResult; + } + + /// + /// 建立请求,以模拟远程HTTP的POST请求方式构造并获取支付宝的处理结果,带文件上传功能 + /// + /// 请求参数数组 + /// 提交方式。两个值可选:post、get + /// 文件绝对路径 + /// 文件数据 + /// 文件内容类型 + /// 文件长度 + /// 支付宝处理结果 + public static string BuildRequest(SortedDictionary sParaTemp, string strMethod, string fileName, byte[] data, string contentType, int lengthFile) + { + + //待请求参数数组 + Dictionary dicPara = new Dictionary(); + dicPara = BuildRequestPara(sParaTemp); + + //构造请求地址 + string strUrl = GATEWAY_NEW + "_input_charset=" + _input_charset; + + //设置HttpWebRequest基本信息 + HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strUrl); + //设置请求方式:get、post + request.Method = strMethod; + //设置boundaryValue + string boundaryValue = DateTime.Now.Ticks.ToString("x"); + string boundary = "--" + boundaryValue; + request.ContentType = "\r\nmultipart/form-data; boundary=" + boundaryValue; + //设置KeepAlive + request.KeepAlive = true; + //设置请求数据,拼接成字符串 + StringBuilder sbHtml = new StringBuilder(); + foreach (KeyValuePair key in dicPara) + { + sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"" + key.Key + "\"\r\n\r\n" + key.Value + "\r\n"); + } + sbHtml.Append(boundary + "\r\nContent-Disposition: form-data; name=\"withhold_file\"; filename=\""); + sbHtml.Append(fileName); + sbHtml.Append("\"\r\nContent-Type: " + contentType + "\r\n\r\n"); + string postHeader = sbHtml.ToString(); + //将请求数据字符串类型根据编码格式转换成字节流 + Encoding code = Encoding.GetEncoding(_input_charset); + byte[] postHeaderBytes = code.GetBytes(postHeader); + byte[] boundayBytes = Encoding.ASCII.GetBytes("\r\n" + boundary + "--\r\n"); + //设置长度 + long length = postHeaderBytes.Length + lengthFile + boundayBytes.Length; + request.ContentLength = length; + + //请求远程HTTP + Stream requestStream = request.GetRequestStream(); + Stream myStream; + try + { + //发送数据请求服务器 + requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); + requestStream.Write(data, 0, lengthFile); + requestStream.Write(boundayBytes, 0, boundayBytes.Length); + HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse(); + myStream = HttpWResp.GetResponseStream(); + } + catch (WebException e) + { + return e.ToString(); + } + finally + { + if (requestStream != null) + { + requestStream.Close(); + } + } + + //读取支付宝返回处理结果 + StreamReader reader = new StreamReader(myStream, code); + StringBuilder responseData = new StringBuilder(); + + String line; + while ((line = reader.ReadLine()) != null) + { + responseData.Append(line); + } + myStream.Close(); + return responseData.ToString(); + } + + /// + /// 用于防钓鱼,调用接口query_timestamp来获取时间戳的处理函数 + /// 注意:远程解析XML出错,与IIS服务器配置有关 + /// + /// 时间戳字符串 + public static string Query_timestamp() + { + + Basic.BLL.siteconfig bll = new Basic.BLL.siteconfig(); + Basic.Model.siteconfig model = bll.loadConfig(Basic.Tools.Utils.GetXmlMapPath(Basic.Keys.FILE_SITE_XML_CONFING)); + + string url = GATEWAY_NEW + "service=query_timestamp&partner=" + model.Alipay_partner + "&_input_charset=" + Config.Input_charset; + 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; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/Config.cs b/App.code/Pay/Config.cs new file mode 100644 index 0000000..0188542 --- /dev/null +++ b/App.code/Pay/Config.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Web; + +namespace WxPayAPI +{ + /** + * 配置账号信息 + */ + public class WxPayConfig + { + //=======【基本信息设置】===================================== + /* 微信公众号信息配置 + * APPID:绑定支付的APPID(必须配置) + * MCHID:商户号(必须配置) + * KEY:商户支付密钥,参考开户邮件设置(必须配置) + * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置) + */ + + public static string APPID = Basic.BLL.siteconfig.getConfig("appid"); + public static string MCHID = Basic.BLL.siteconfig.getConfig("mchid"); + public static string KEY = Basic.BLL.siteconfig.getConfig("key"); + public static string APPSECRET = Basic.BLL.siteconfig.getConfig("appsecret"); + + //=======【证书路径设置】===================================== + /* 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要) + */ + public const string SSLCERT_PATH = "cert/apiclient_cert.p12"; + public const string SSLCERT_PASSWORD = "1233410002"; + + + + //=======【支付结果通知url】===================================== + /* 支付结果通知回调url,用于商户接收支付结果 + */ + public static string NOTIFY_URL = "http://" + HttpContext.Current.Request.Url.Host.ToString() + "/pay/wechatNative/ResultPay.aspx"; + + //=======【商户系统后台机器IP】===================================== + /* 此参数可手动配置也可在程序中自动获取 + */ + public const string IP = "8.8.8.8"; + + + //=======【代理服务器设置】=================================== + /* 默认IP和端口号分别为0.0.0.0和0,此时不开启代理(如有需要才设置) + */ + public const string PROXY_URL = "0"; + + //=======【上报信息配置】=================================== + /* 测速上报等级,0.关闭上报; 1.仅错误时上报; 2.全量上报 + */ + public const int REPORT_LEVENL = 1; + + //=======【日志级别】=================================== + /* 日志等级,0.不输出日志;1.只输出错误信息; 2.输出错误和正常信息; 3.输出错误信息、正常信息和调试信息 + */ + public const int LOG_LEVENL = 3; + + + } +} \ No newline at end of file diff --git a/App.code/Pay/Data.cs b/App.code/Pay/Data.cs new file mode 100644 index 0000000..d9cce34 --- /dev/null +++ b/App.code/Pay/Data.cs @@ -0,0 +1,267 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Xml; +using System.Security.Cryptography; +using System.Text; +using LitJson; + +namespace WxPayAPI +{ + /// + /// 微信支付协议接口数据类,所有的API接口通信都依赖这个数据结构, + /// 在调用接口之前先填充各个字段的值,然后进行接口通信, + /// 这样设计的好处是可扩展性强,用户可随意对协议进行更改而不用重新设计数据结构, + /// 还可以随意组合出不同的协议数据包,不用为每个协议设计一个数据包结构 + /// + public class WxPayData + { + public WxPayData() + { + + } + + //采用排序的Dictionary的好处是方便对数据包进行签名,不用再签名之前再做一次排序 + private SortedDictionary m_values = new SortedDictionary(); + + /** + * 设置某个字段的值 + * @param key 字段名 + * @param value 字段值 + */ + public void SetValue(string key, object value) + { + m_values[key] = value; + } + + /** + * 根据字段名获取某个字段的值 + * @param key 字段名 + * @return key对应的字段值 + */ + public object GetValue(string key) + { + object o = null; + m_values.TryGetValue(key, out o); + return o; + } + + /** + * 判断某个字段是否已设置 + * @param key 字段名 + * @return 若字段key已被设置,则返回true,否则返回false + */ + public bool IsSet(string key) + { + object o = null; + m_values.TryGetValue(key, out o); + if (null != o) + return true; + else + return false; + } + + /** + * @将Dictionary转成xml + * @return 经转换得到的xml串 + * @throws WxPayException + **/ + public string ToXml() + { + //数据为空时不能转化为xml格式 + if (0 == m_values.Count) + { + Log.Error(this.GetType().ToString(), "WxPayData数据为空!"); + throw new WxPayException("WxPayData数据为空!"); + } + + string xml = ""; + foreach (KeyValuePair pair in m_values) + { + //字段值不能为null,会影响后续流程 + if (pair.Value == null) + { + Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!"); + throw new WxPayException("WxPayData内部含有值为null的字段!"); + } + + if (pair.Value.GetType() == typeof(int)) + { + xml += "<" + pair.Key + ">" + pair.Value + ""; + } + else if (pair.Value.GetType() == typeof(string)) + { + xml += "<" + pair.Key + ">" + ""; + } + else//除了string和int类型不能含有其他数据类型 + { + Log.Error(this.GetType().ToString(), "WxPayData字段数据类型错误!"); + throw new WxPayException("WxPayData字段数据类型错误!"); + } + } + xml += ""; + return xml; + } + + /** + * @将xml转为WxPayData对象并返回对象内部的数据 + * @param string 待转换的xml串 + * @return 经转换得到的Dictionary + * @throws WxPayException + */ + public SortedDictionary FromXml(string xml) + { + if (string.IsNullOrEmpty(xml)) + { + Log.Error(this.GetType().ToString(), "将空的xml串转换为WxPayData不合法!"); + throw new WxPayException("将空的xml串转换为WxPayData不合法!"); + } + + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(xml); + XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点 + XmlNodeList nodes = xmlNode.ChildNodes; + foreach (XmlNode xn in nodes) + { + XmlElement xe = (XmlElement)xn; + m_values[xe.Name] = xe.InnerText;//获取xml的键值对到WxPayData内部的数据中 + } + + try + { + //2015-06-29 错误是没有签名 + if(m_values["return_code"] != "SUCCESS") + { + return m_values; + } + CheckSign();//验证签名,不通过会抛异常 + } + catch(WxPayException ex) + { + throw new WxPayException(ex.Message); + } + + return m_values; + } + + /** + * @Dictionary格式转化成url参数格式 + * @ return url格式串, 该串不包含sign字段值 + */ + public string ToUrl() + { + string buff = ""; + foreach (KeyValuePair pair in m_values) + { + if (pair.Value == null) + { + Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!"); + throw new WxPayException("WxPayData内部含有值为null的字段!"); + } + + if (pair.Key != "sign" && pair.Value.ToString() != "") + { + buff += pair.Key + "=" + pair.Value + "&"; + } + } + buff = buff.Trim('&'); + return buff; + } + + + /** + * @Dictionary格式化成Json + * @return json串数据 + */ + public string ToJson() + { + string jsonStr = JsonMapper.ToJson(m_values); + return jsonStr; + } + + /** + * @values格式化成能在Web页面上显示的结果(因为web页面上不能直接输出xml格式的字符串) + */ + public string ToPrintStr() + { + string str = ""; + foreach (KeyValuePair pair in m_values) + { + if (pair.Value == null) + { + Log.Error(this.GetType().ToString(), "WxPayData内部含有值为null的字段!"); + throw new WxPayException("WxPayData内部含有值为null的字段!"); + } + + str += string.Format("{0}={1}
", pair.Key, pair.Value.ToString()); + } + Log.Debug(this.GetType().ToString(), "Print in Web Page : " + str); + return str; + } + + /** + * @生成签名,详见签名生成算法 + * @return 签名, sign字段不参加签名 + */ + public string MakeSign() + { + //转url格式 + string str = ToUrl(); + //在string后加入API KEY + str += "&key=" + WxPayConfig.KEY; + //MD5加密 + var md5 = MD5.Create(); + var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); + var sb = new StringBuilder(); + foreach (byte b in bs) + { + sb.Append(b.ToString("x2")); + } + //所有字符转为大写 + return sb.ToString().ToUpper(); + } + + /** + * + * 检测签名是否正确 + * 正确返回true,错误抛异常 + */ + public bool CheckSign() + { + //如果没有设置签名,则跳过检测 + if (!IsSet("sign")) + { + Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!"); + throw new WxPayException("WxPayData签名存在但不合法!"); + } + //如果设置了签名但是签名为空,则抛异常 + else if(GetValue("sign") == null || GetValue("sign").ToString() == "") + { + Log.Error(this.GetType().ToString(), "WxPayData签名存在但不合法!"); + throw new WxPayException("WxPayData签名存在但不合法!"); + } + + //获取接收到的签名 + string return_sign = GetValue("sign").ToString(); + + //在本地计算新的签名 + string cal_sign = MakeSign(); + + if (cal_sign == return_sign) + { + return true; + } + + Log.Error(this.GetType().ToString(), "WxPayData签名验证错误!"); + throw new WxPayException("WxPayData签名验证错误!"); + } + + /** + * @获取Dictionary + */ + public SortedDictionary GetValues() + { + return m_values; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/DownloadBill.cs b/App.code/Pay/DownloadBill.cs new file mode 100644 index 0000000..7f7c2a2 --- /dev/null +++ b/App.code/Pay/DownloadBill.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Web; + +namespace WxPayAPI +{ + public class DownloadBill + { + /*** + * 下载对账单完整业务流程逻辑 + * @param bill_date 下载对账单的日期(格式:20140603,一次只能下载一天的对账单) + * @param bill_type 账单类型 + * ALL,返回当日所有订单信息,默认值 + * SUCCESS,返回当日成功支付的订单 + * REFUND,返回当日退款订单 + * REVOKED,已撤销的订单 + * @return 对账单结果(xml格式) + */ + public static string Run(string bill_date, string bill_type) + { + Log.Info("DownloadBill", "DownloadBill is processing..."); + + WxPayData data = new WxPayData(); + data.SetValue("bill_date", bill_date);//账单日期 + data.SetValue("bill_type", bill_type);//账单类型 + WxPayData result = WxPayApi.DownloadBill(data);//提交下载对账单请求给API,接收返回结果 + + Log.Info("DownloadBill", "DownloadBill process complete, result : " + result.ToXml()); + return result.ToPrintStr(); + } + } +} \ No newline at end of file diff --git a/App.code/Pay/Exception.cs b/App.code/Pay/Exception.cs new file mode 100644 index 0000000..fb23495 --- /dev/null +++ b/App.code/Pay/Exception.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Web; + +namespace WxPayAPI +{ + public class WxPayException : Exception + { + public WxPayException(string msg) : base(msg) + { + + } + } +} \ No newline at end of file diff --git a/App.code/Pay/HttpService.cs b/App.code/Pay/HttpService.cs new file mode 100644 index 0000000..8054be3 --- /dev/null +++ b/App.code/Pay/HttpService.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Net; +using System.IO; +using System.Text; +using System.Net.Security; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; + +namespace WxPayAPI +{ + /// + /// http连接基础类,负责底层的http通信 + /// + public class HttpService + { + + public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) + { + //直接确认,否则打不开 + return true; + } + + public static string Post(string xml, string url, bool isUseCert, int timeout) + { + System.GC.Collect();//垃圾回收,回收没有正常关闭的http连接 + + string result = "";//返回结果 + + HttpWebRequest request = null; + HttpWebResponse response = null; + Stream reqStream = null; + + try + { + //设置最大连接数 + ServicePointManager.DefaultConnectionLimit = 200; + //设置https验证方式 + if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + ServicePointManager.ServerCertificateValidationCallback = + new RemoteCertificateValidationCallback(CheckValidationResult); + } + + /*************************************************************** + * 下面设置HttpWebRequest的相关属性 + * ************************************************************/ + request = (HttpWebRequest)WebRequest.Create(url); + + request.Method = "POST"; + request.Timeout = timeout * 1000; + + //设置代理服务器 + //WebProxy proxy = new WebProxy(); //定义一个网关对象 + //proxy.Address = new Uri(WxPayConfig.PROXY_URL); //网关服务器端口:端口 + //request.Proxy = proxy; + + //设置POST的数据类型和长度 + request.ContentType = "text/xml"; + byte[] data = System.Text.Encoding.UTF8.GetBytes(xml); + request.ContentLength = data.Length; + + //是否使用证书 + if (isUseCert) + { + string path = HttpContext.Current.Request.PhysicalApplicationPath; + X509Certificate2 cert = new X509Certificate2(path + WxPayConfig.SSLCERT_PATH, WxPayConfig.SSLCERT_PASSWORD); + request.ClientCertificates.Add(cert); + Log.Debug("WxPayApi", "PostXml used cert"); + } + + //往服务器写入数据 + reqStream = request.GetRequestStream(); + reqStream.Write(data, 0, data.Length); + reqStream.Close(); + + //获取服务端返回 + response = (HttpWebResponse)request.GetResponse(); + + //获取服务端返回数据 + StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); + result = sr.ReadToEnd().Trim(); + sr.Close(); + } + catch (System.Threading.ThreadAbortException e) + { + Log.Error("HttpService", "Thread - caught ThreadAbortException - resetting."); + Log.Error("Exception message: {0}", e.Message); + System.Threading.Thread.ResetAbort(); + } + catch (WebException e) + { + Log.Error("HttpService", e.ToString()); + if (e.Status == WebExceptionStatus.ProtocolError) + { + Log.Error("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode); + Log.Error("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription); + } + throw new WxPayException(e.ToString()); + } + catch (Exception e) + { + Log.Error("HttpService", e.ToString()); + throw new WxPayException(e.ToString()); + } + finally + { + //关闭连接和流 + if (response != null) + { + response.Close(); + } + if(request != null) + { + request.Abort(); + } + } + return result; + } + + /// + /// 处理http GET请求,返回数据 + /// + /// 请求的url地址 + /// http GET成功后返回的数据,失败抛WebException异常 + public static string Get(string url) + { + System.GC.Collect(); + string result = ""; + + HttpWebRequest request = null; + HttpWebResponse response = null; + + //请求url以获取数据 + try + { + //设置最大连接数 + ServicePointManager.DefaultConnectionLimit = 200; + //设置https验证方式 + if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + { + ServicePointManager.ServerCertificateValidationCallback = + new RemoteCertificateValidationCallback(CheckValidationResult); + } + + /*************************************************************** + * 下面设置HttpWebRequest的相关属性 + * ************************************************************/ + request = (HttpWebRequest)WebRequest.Create(url); + + request.Method = "GET"; + + //设置代理 + //WebProxy proxy = new WebProxy(); + //proxy.Address = new Uri(WxPayConfig.PROXY_URL); + //request.Proxy = proxy; + + //获取服务器返回 + response = (HttpWebResponse)request.GetResponse(); + + //获取HTTP返回数据 + StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); + result = sr.ReadToEnd().Trim(); + sr.Close(); + } + catch (System.Threading.ThreadAbortException e) + { + Log.Error("HttpService","Thread - caught ThreadAbortException - resetting."); + Log.Error("Exception message: {0}", e.Message); + System.Threading.Thread.ResetAbort(); + } + catch (WebException e) + { + Log.Error("HttpService", e.ToString()); + if (e.Status == WebExceptionStatus.ProtocolError) + { + Log.Error("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode); + Log.Error("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription); + } + throw new WxPayException(e.ToString()); + } + catch (Exception e) + { + Log.Error("HttpService", e.ToString()); + throw new WxPayException(e.ToString()); + } + finally + { + //关闭连接和流 + if (response != null) + { + response.Close(); + } + if (request != null) + { + request.Abort(); + } + } + return result; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/JsApiPay.cs b/App.code/Pay/JsApiPay.cs new file mode 100644 index 0000000..b53274b --- /dev/null +++ b/App.code/Pay/JsApiPay.cs @@ -0,0 +1,264 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.Runtime.Serialization; +using System.IO; +using System.Text; +using System.Net; +using System.Web.Security; +using LitJson; + +namespace WxPayAPI +{ + public class JsApiPay + { + /// + /// 保存页面对象,因为要在类的方法中使用Page的Request对象 + /// + private Page page { get; set; } + + /// + /// openid用于调用统一下单接口 + /// + public string openid { get; set; } + + /// + /// access_token用于获取收货地址js函数入口参数 + /// + public string access_token { get; set; } + + /// + /// out_trade_no 商家订单号 + /// + public string out_trade_no { get; set; } + + /// + /// 商品金额,用于统一下单 + /// + public int total_fee { get; set; } + + /// + /// 统一下单接口返回结果 + /// + public WxPayData unifiedOrderResult { get; set; } + + public JsApiPay(Page page) + { + this.page = page; + } + + + /** + * + * 网页授权获取用户基本信息的全部过程 + * 详情请参看网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html + * 第一步:利用url跳转获取code + * 第二步:利用code去获取openid和access_token + * + */ + public void GetOpenidAndAccessToken() + { + if (!string.IsNullOrEmpty(page.Request.QueryString["code"])) + { + //获取code码,以获取openid和access_token + string code = page.Request.QueryString["code"]; + Log.Debug(this.GetType().ToString(), "Get code : " + code); + GetOpenidAndAccessTokenFromCode(code); + } + else + { + //构造网页授权获取code的URL + string host = page.Request.Url.Host; + string path = page.Request.Path; + string redirect_uri = page.Server.UrlEncode(HttpContext.Current.Request.Url.ToString()); + WxPayData data = new WxPayData(); + data.SetValue("appid", WxPayConfig.APPID); + data.SetValue("redirect_uri", redirect_uri); + data.SetValue("response_type", "code"); + data.SetValue("scope", "snsapi_base"); + data.SetValue("state", "STATE" + "#wechat_redirect"); + string url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + data.ToUrl(); + Log.Debug(this.GetType().ToString(), "Will Redirect to URL : " + url); + try + { + //触发微信返回code码 + page.Response.Redirect(url);//Redirect函数会抛出ThreadAbortException异常,不用处理这个异常 + } + catch (System.Threading.ThreadAbortException ex) + { + } + //page.Response.Write("Page:" + url + "
--------
" + redirect_uri + "
--------
"); + } + } + + + /** + * + * 通过code换取网页授权access_token和openid的返回数据,正确时返回的JSON数据包如下: + * { + * "access_token":"ACCESS_TOKEN", + * "expires_in":7200, + * "refresh_token":"REFRESH_TOKEN", + * "openid":"OPENID", + * "scope":"SCOPE", + * "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" + * } + * 其中access_token可用于获取共享收货地址 + * openid是微信支付jsapi支付接口统一下单时必须的参数 + * 更详细的说明请参考网页授权获取用户基本信息:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html + * @失败时抛异常WxPayException + */ + public void GetOpenidAndAccessTokenFromCode(string code) + { + try + { + //构造获取openid及access_token的url + WxPayData data = new WxPayData(); + data.SetValue("appid", WxPayConfig.APPID); + data.SetValue("secret", WxPayConfig.APPSECRET); + data.SetValue("code", code); + data.SetValue("grant_type", "authorization_code"); + string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); + + //请求url以获取数据 + string result = HttpService.Get(url); + + Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result); + + //保存access_token,用于收货地址获取 + JsonData jd = JsonMapper.ToObject(result); + access_token = (string)jd["access_token"]; + + //获取用户openid + openid = (string)jd["openid"]; + + Log.Debug(this.GetType().ToString(), "Get openid : " + openid); + Log.Debug(this.GetType().ToString(), "Get access_token : " + access_token); + } + catch (Exception ex) + { + Log.Error(this.GetType().ToString(), ex.ToString()); + throw new WxPayException(ex.ToString()); + } + } + + /** + * 调用统一下单,获得下单结果 + * @return 统一下单结果 + * @失败时抛异常WxPayException + */ + public WxPayData GetUnifiedOrderResult() + { + //统一下单 + WxPayData data = new WxPayData(); + data.SetValue("body", "百花网购物/" + out_trade_no); + data.SetValue("attach", "百花网购物"); + data.SetValue("out_trade_no", out_trade_no); + data.SetValue("total_fee", total_fee); + data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); + data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); + data.SetValue("goods_tag", "百花网购物"); + data.SetValue("trade_type", "JSAPI"); + data.SetValue("openid", openid); + + WxPayData result = WxPayApi.UnifiedOrder(data); + if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") + { + Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); + throw new WxPayException("UnifiedOrder response error!"); + } + + unifiedOrderResult = result; + return result; + } + + /** + * + * 从统一下单成功返回的数据中获取微信浏览器调起jsapi支付所需的参数, + * 微信浏览器调起JSAPI时的输入参数格式如下: + * { + * "appId" : "wx2421b1c4370ec43b", //公众号名称,由商户传入 + * "timeStamp":" 1395712654", //时间戳,自1970年以来的秒数 + * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串 + * "package" : "prepay_id=u802345jgfjsdfgsdg888", + * "signType" : "MD5", //微信签名方式: + * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 + * } + * @return string 微信浏览器调起JSAPI时的输入参数,json格式可以直接做参数用 + * 更详细的说明请参考网页端调起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 + * + */ + public string GetJsApiParameters() + { + Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing..."); + + WxPayData jsApiParam = new WxPayData(); + jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); + jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); + jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); + jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); + jsApiParam.SetValue("signType", "MD5"); + jsApiParam.SetValue("paySign", jsApiParam.MakeSign()); + + string parameters = jsApiParam.ToJson(); + + Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); + return parameters; + } + + + /** + * + * 获取收货地址js函数入口参数,详情请参考收货地址共享接口:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_9 + * @return string 共享收货地址js函数需要的参数,json格式可以直接做参数使用 + */ + public string GetEditAddressParameters() + { + string parameter = ""; + try + { + string host = page.Request.Url.Host; + string path = page.Request.Path; + string queryString = page.Request.Url.Query; + //这个地方要注意,参与签名的是网页授权获取用户信息时微信后台回传的完整url + string url = "http://" + host + path + queryString; + + //构造需要用SHA1算法加密的数据 + WxPayData signData = new WxPayData(); + signData.SetValue("appid", WxPayConfig.APPID); + signData.SetValue("url", url); + signData.SetValue("timestamp", WxPayApi.GenerateTimeStamp()); + signData.SetValue("noncestr", WxPayApi.GenerateNonceStr()); + signData.SetValue("accesstoken", access_token); + string param = signData.ToUrl(); + + Log.Debug(this.GetType().ToString(), "SHA1 encrypt param : " + param); + //SHA1加密 + string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, "SHA1"); + Log.Debug(this.GetType().ToString(), "SHA1 encrypt result : " + addrSign); + + //获取收货地址js函数入口参数 + WxPayData afterData = new WxPayData(); + afterData.SetValue("appId", WxPayConfig.APPID); + afterData.SetValue("scope", "jsapi_address"); + afterData.SetValue("signType", "sha1"); + afterData.SetValue("addrSign", addrSign); + afterData.SetValue("timeStamp", signData.GetValue("timestamp")); + afterData.SetValue("nonceStr", signData.GetValue("noncestr")); + + //转为json格式 + parameter = afterData.ToJson(); + Log.Debug(this.GetType().ToString(), "Get EditAddressParam : " + parameter); + } + catch (Exception ex) + { + Log.Error(this.GetType().ToString(), ex.ToString()); + throw new WxPayException(ex.ToString()); + } + + return parameter; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/Log.cs b/App.code/Pay/Log.cs new file mode 100644 index 0000000..c120831 --- /dev/null +++ b/App.code/Pay/Log.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.IO; + +namespace WxPayAPI +{ + public class Log + { + //在网站根目录下创建日志目录 + public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs"; + + /** + * 向日志文件写入调试信息 + * @param className 类名 + * @param content 写入内容 + */ + public static void Debug(string className, string content) + { + + } + + /** + * 向日志文件写入运行时信息 + * @param className 类名 + * @param content 写入内容 + */ + public static void Info(string className, string content) + { + + } + + /** + * 向日志文件写入出错信息 + * @param className 类名 + * @param content 写入内容 + */ + public static void Error(string className, string content) + { + + } + + /** + * 实际的写日志操作 + * @param type 日志记录类型 + * @param className 类名 + * @param content 写入内容 + */ + protected static void WriteLog(string type, string className, string content) + { + + } + } +} \ No newline at end of file diff --git a/App.code/Pay/MD5.cs b/App.code/Pay/MD5.cs new file mode 100644 index 0000000..94e65c9 --- /dev/null +++ b/App.code/Pay/MD5.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.Security.Cryptography; + +namespace Com.Alipay +{ + /// + /// 类名:MD5 + /// 功能:MD5加密 + /// 版本:3.3 + /// 修改日期:2012-07-05 + /// 说明: + /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 + /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 + /// + public sealed class AlipayMD5 + { + public AlipayMD5() + { + // + // TODO: 在此处添加构造函数逻辑 + // + } + + /// + /// 签名字符串 + /// + /// 需要签名的字符串 + /// 密钥 + /// 编码格式 + /// 签名结果 + public static string Sign(string prestr, string key, string _input_charset) + { + StringBuilder sb = new StringBuilder(32); + + prestr = prestr + key; + + 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 bool Verify(string prestr, string sign, string key, string _input_charset) + { + string mysign = Sign(prestr, key, _input_charset); + if (mysign == sign) + { + return true; + } + else + { + return false; + } + } + } +} \ No newline at end of file diff --git a/App.code/Pay/MicroPay.cs b/App.code/Pay/MicroPay.cs new file mode 100644 index 0000000..4417785 --- /dev/null +++ b/App.code/Pay/MicroPay.cs @@ -0,0 +1,183 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Threading; + +namespace WxPayAPI +{ + public class MicroPay + { + /** + * 刷卡支付完整业务流程逻辑 + * @param body 商品描述 + * @param total_fee 总金额 + * @param auth_code 支付授权码 + * @throws WxPayException + * @return 刷卡支付结果 + */ + public static string Run(string body, string total_fee, string auth_code) + { + Log.Info("MicroPay", "Micropay is processing..."); + + WxPayData data = new WxPayData(); + data.SetValue("auth_code", auth_code);//授权码 + data.SetValue("body", body);//商品描述 + data.SetValue("total_fee", int.Parse(total_fee));//总金额 + data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//产生随机的商户订单号 + + WxPayData result = WxPayApi.Micropay(data, 10); //提交被扫支付,接收返回结果 + + //如果提交被扫支付接口调用失败,则抛异常 + if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL") + { + string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : ""; + Log.Error("MicroPay", "Micropay API interface call failure, result : " + result.ToXml()); + throw new WxPayException("Micropay API interface call failure, return_msg : " + returnMsg); + } + + //签名验证 + result.CheckSign(); + Log.Debug("MicroPay", "Micropay response check sign success"); + + //刷卡支付直接成功 + if(result.GetValue("return_code").ToString() == "SUCCESS" && + result.GetValue("result_code").ToString() == "SUCCESS") + { + Log.Debug("MicroPay", "Micropay business success, result : " + result.ToXml()); + return result.ToPrintStr(); + } + + /****************************************************************** + * 剩下的都是接口调用成功,业务失败的情况 + * ****************************************************************/ + //1)业务结果明确失败 + if(result.GetValue("err_code").ToString() != "USERPAYING" && + result.GetValue("err_code").ToString() != "SYSTEMERROR") + { + Log.Error("MicroPay", "micropay API interface call success, business failure, result : " + result.ToXml()); + return result.ToPrintStr(); + } + + //2)不能确定是否失败,需查单 + //用商户订单号去查单 + string out_trade_no = data.GetValue("out_trade_no").ToString(); + + //确认支付是否成功,每隔一段时间查询一次订单,共查询10次 + int queryTimes = 10;//查询次数计数器 + while(queryTimes-- > 0) + { + int succResult = 0;//查询结果 + WxPayData queryResult = Query(out_trade_no, out succResult); + //如果需要继续查询,则等待2s后继续 + if(succResult == 2) + { + Thread.Sleep(2000); + continue; + } + //查询成功,返回订单查询接口返回的数据 + else if(succResult == 1) + { + Log.Debug("MicroPay", "Mircopay success, return order query result : " + queryResult.ToXml()); + return queryResult.ToPrintStr(); + } + //订单交易失败,直接返回刷卡支付接口返回的结果,失败原因会在err_code中描述 + else + { + Log.Error("MicroPay", "Micropay failure, return micropay result : " + result.ToXml()); + return result.ToPrintStr(); + } + } + + //确认失败,则撤销订单 + Log.Error("MicroPay", "Micropay failure, Reverse order is processing..."); + if(!Cancel(out_trade_no)) + { + Log.Error("MicroPay", "Reverse order failure"); + throw new WxPayException("Reverse order failure!"); + } + + return result.ToPrintStr(); + } + + + /** + * + * 查询订单情况 + * @param string out_trade_no 商户订单号 + * @param int succCode 查询订单结果:0表示订单不成功,1表示订单成功,2表示继续查询 + * @return 订单查询接口返回的数据,参见协议接口 + */ + public static WxPayData Query(string out_trade_no, out int succCode) + { + WxPayData queryOrderInput = new WxPayData(); + queryOrderInput.SetValue("out_trade_no",out_trade_no); + WxPayData result = WxPayApi.OrderQuery(queryOrderInput); + + if(result.GetValue("return_code").ToString() == "SUCCESS" + && result.GetValue("result_code").ToString() == "SUCCESS") + { + //支付成功 + if(result.GetValue("trade_state").ToString() == "SUCCESS") + { + succCode = 1; + return result; + } + //用户支付中,需要继续查询 + else if(result.GetValue("trade_state").ToString() == "USERPAYING") + { + succCode = 2; + return result; + } + } + + //如果返回错误码为“此交易订单号不存在”则直接认定失败 + if(result.GetValue("err_code").ToString() == "ORDERNOTEXIST") + { + succCode = 0; + } + else + { + //如果是系统错误,则后续继续 + succCode = 2; + } + return result; + } + + + /** + * + * 撤销订单,如果失败会重复调用10次 + * @param string out_trade_no 商户订单号 + * @param depth 调用次数,这里用递归深度表示 + * @return false表示撤销失败,true表示撤销成功 + */ + public static bool Cancel(string out_trade_no, int depth = 0) + { + if(depth > 10) + { + return false; + } + + WxPayData reverseInput = new WxPayData(); + reverseInput.SetValue("out_trade_no",out_trade_no); + WxPayData result = WxPayApi.Reverse(reverseInput); + + //接口调用失败 + if(result.GetValue("return_code").ToString() != "SUCCESS") + { + return false; + } + + //如果结果为success且不需要重新调用撤销,则表示撤销成功 + if(result.GetValue("result_code").ToString() != "SUCCESS" && result.GetValue("recall").ToString() == "N") + { + return true; + } + else if(result.GetValue("recall").ToString() == "Y") + { + return Cancel(out_trade_no, ++depth); + } + return false; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/NativeNotify.cs b/App.code/Pay/NativeNotify.cs new file mode 100644 index 0000000..c130348 --- /dev/null +++ b/App.code/Pay/NativeNotify.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.IO; + +namespace WxPayAPI +{ + /// + /// 扫码支付模式一回调处理类 + /// 接收微信支付后台发送的扫码结果,调用统一下单接口并将下单结果返回给微信支付后台 + /// + public class NativeNotify : Notify + { + public NativeNotify(Page page) + : base(page) + { + + } + + public override void ProcessNotify() + { + WriteLog("不错"); + WxPayData notifyData = GetNotifyData(); + + //检查openid和product_id是否返回 + if (!notifyData.IsSet("openid") || !notifyData.IsSet("product_id")) + { + WxPayData res = new WxPayData(); + res.SetValue("return_code", "FAIL"); + res.SetValue("return_msg", "回调数据异常"); + Log.Info(this.GetType().ToString(), "The data WeChat post is error : " + res.ToXml()); + page.Response.Write(res.ToXml()); + page.Response.End(); + } + + //调统一下单接口,获得下单结果 + string openid = notifyData.GetValue("openid").ToString(); + string product_id = notifyData.GetValue("product_id").ToString(); + WxPayData unifiedOrderResult = new WxPayData(); + try + { + unifiedOrderResult = UnifiedOrder(openid, product_id); + } + catch (Exception ex)//若在调统一下单接口时抛异常,立即返回结果给微信支付后台 + { + WxPayData res = new WxPayData(); + res.SetValue("return_code", "FAIL"); + res.SetValue("return_msg", "统一下单失败"); + Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml()); + page.Response.Write(res.ToXml()); + page.Response.End(); + } + + //若下单失败,则立即返回结果给微信支付后台 + if (!unifiedOrderResult.IsSet("appid") || !unifiedOrderResult.IsSet("mch_id") || !unifiedOrderResult.IsSet("prepay_id")) + { + WxPayData res = new WxPayData(); + res.SetValue("return_code", "FAIL"); + res.SetValue("return_msg", "统一下单失败"); + Log.Error(this.GetType().ToString(), "UnifiedOrder failure : " + res.ToXml()); + page.Response.Write(res.ToXml()); + page.Response.End(); + } + + //统一下单成功,则返回成功结果给微信支付后台 + WxPayData data = new WxPayData(); + data.SetValue("return_code", "SUCCESS"); + data.SetValue("return_msg", "OK"); + data.SetValue("appid", WxPayConfig.APPID); + data.SetValue("mch_id", WxPayConfig.MCHID); + data.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); + data.SetValue("prepay_id", unifiedOrderResult.GetValue("prepay_id")); + data.SetValue("result_code", "SUCCESS"); + data.SetValue("err_code_des", "OK"); + data.SetValue("sign", data.MakeSign()); + + Log.Info(this.GetType().ToString(), "UnifiedOrder success , send data to WeChat : " + data.ToXml()); + page.Response.Write(data.ToXml()); + page.Response.End(); + } + + private WxPayData UnifiedOrder(string openId, string productId) + { + //统一下单 + WxPayData req = new WxPayData(); + req.SetValue("body", "test"); + req.SetValue("attach", "test"); + req.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo()); + req.SetValue("total_fee", 1); + req.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); + req.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); + req.SetValue("goods_tag", "test"); + req.SetValue("trade_type", "NATIVE"); + req.SetValue("openid", openId); + req.SetValue("product_id", productId); + WxPayData result = WxPayApi.UnifiedOrder(req); + return result; + } + /// + /// 写入日志 + /// + /// + public void WriteLog(string readme) + { + string _Date = System.DateTime.Now.Date.ToString("yyyy-MM-dd"); + StreamWriter dout = new StreamWriter(@"D:/wwwroot/wxpay.ivhua.com/logs/" + _Date + ".txt", true); + dout.Write("事件:" + readme + " 操作时间:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")); + dout.Write(System.Environment.NewLine); //换行 + dout.Close(); + } + } +} \ No newline at end of file diff --git a/App.code/Pay/NativePay.cs b/App.code/Pay/NativePay.cs new file mode 100644 index 0000000..895389f --- /dev/null +++ b/App.code/Pay/NativePay.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.IO; +using System.Data.SqlClient; + +namespace WxPayAPI +{ + public class NativePay + { + /** + * 生成扫描支付模式一URL + * @param productId 商品ID + * @return 模式一URL + */ + public string GetPrePayUrl(string productId) + { + WxPayData data = new WxPayData(); + data.SetValue("appid", WxPayConfig.APPID);//公众帐号id + data.SetValue("mch_id", WxPayConfig.MCHID);//商户号 + data.SetValue("time_stamp", WxPayApi.GenerateTimeStamp());//时间戳 + data.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串 + data.SetValue("product_id", productId);//商品ID + data.SetValue("sign", data.MakeSign());//签名 + string str = ToUrlParams(data.GetValues());//转换为URL串 + string url = "weixin://wxpay/bizpayurl?" + str; + return url; + } + + /** + * 生成直接支付url,支付url有效期为2小时,模式二 + * @param productId 商品ID + * @return 模式二URL + */ + public string GetPayUrl(string productId, string total_fee) + { + Basic.BLL.siteconfig bll = new Basic.BLL.siteconfig(); + Basic.Model.siteconfig siteconfig = bll.loadConfig(Basic.Tools.Utils.GetXmlMapPath("Configpath")); + + string strCom = siteconfig.webname; + if (strCom.Length > 5) { strCom = strCom.Substring(0,5); } + + WxPayData data = new WxPayData(); + data.SetValue("body", strCom);//商品描述 + data.SetValue("attach", strCom);//附加数据 + data.SetValue("out_trade_no", productId);//随机字符串 + data.SetValue("total_fee", total_fee);//总金额 + data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间 + data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间 + data.SetValue("goods_tag", "");//商品标记 + data.SetValue("trade_type", "NATIVE");//交易类型 + data.SetValue("product_id", productId);//商品ID + + WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口 + string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接 + return url; + } + /** + * 参数数组转换为url格式 + * @param map 参数名与参数值的映射表 + * @return URL字符串 + */ + private string ToUrlParams(SortedDictionary map) + { + string buff = ""; + foreach (KeyValuePair pair in map) + { + buff += pair.Key + "=" + pair.Value + "&"; + } + buff = buff.Trim('&'); + return buff; + } + } +} \ No newline at end of file diff --git a/App.code/Pay/Notify.cs b/App.code/Pay/Notify.cs new file mode 100644 index 0000000..e2c1819 --- /dev/null +++ b/App.code/Pay/Notify.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.Text; + +namespace WxPayAPI +{ + /// + /// 回调处理基类 + /// 主要负责接收微信支付后台发送过来的数据,对数据进行签名验证 + /// 子类在此类基础上进行派生并重写自己的回调处理过程 + /// + public class Notify + { + public Page page {get;set;} + public Notify(Page page) + { + this.page = page; + } + + /// + /// 接收从微信支付后台发送过来的数据并验证签名 + /// + /// 微信支付后台返回的数据 + public WxPayData GetNotifyData() + { + //接收从微信后台POST过来的数据 + System.IO.Stream s = page.Request.InputStream; + int count = 0; + byte[] buffer = new byte[1024]; + StringBuilder builder = new StringBuilder(); + while ((count = s.Read(buffer, 0, 1024)) > 0) + { + builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); + } + s.Flush(); + s.Close(); + s.Dispose(); + + Log.Info(this.GetType().ToString(), "Receive data from WeChat : " + builder.ToString()); + + //转换数据格式并验证签名 + WxPayData data = new WxPayData(); + try + { + data.FromXml(builder.ToString()); + } + catch(WxPayException ex) + { + //若签名错误,则立即返回结果给微信支付后台 + WxPayData res = new WxPayData(); + res.SetValue("return_code", "FAIL"); + res.SetValue("return_msg", ex.Message); + Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml()); + page.Response.Write(res.ToXml()); + page.Response.End(); + } + + Log.Info(this.GetType().ToString(), "Check sign success"); + return data; + } + + //派生类需要重写这个方法,进行不同的回调处理 + public virtual void ProcessNotify() + { + + } + } +} \ No newline at end of file diff --git a/App.code/Pay/OrderQuery.cs b/App.code/Pay/OrderQuery.cs new file mode 100644 index 0000000..872d041 --- /dev/null +++ b/App.code/Pay/OrderQuery.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Web; + +namespace WxPayAPI +{ + public class OrderQuery + { + /*** + * 订单查询完整业务流程逻辑 + * @param transaction_id 微信订单号(优先使用) + * @param out_trade_no 商户订单号 + * @return 订单查询结果(xml格式) + */ + public static string Run(string transaction_id, string out_trade_no) + { + Log.Info("OrderQuery", "OrderQuery is processing..."); + + WxPayData data = new WxPayData(); + if(!string.IsNullOrEmpty(transaction_id))//如果微信订单号存在,则以微信订单号为准 + { + data.SetValue("transaction_id", transaction_id); + } + else//微信订单号不存在,才根据商户订单号去查单 + { + data.SetValue("out_trade_no", out_trade_no); + } + + WxPayData result = WxPayApi.OrderQuery(data);//提交订单查询请求给API,接收返回数据 + + Log.Info("OrderQuery", "OrderQuery process complete, result : " + result.ToXml()); + return result.ToPrintStr(); + } + } +} \ No newline at end of file diff --git a/App.code/Pay/Refund.cs b/App.code/Pay/Refund.cs new file mode 100644 index 0000000..1ea7556 --- /dev/null +++ b/App.code/Pay/Refund.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Web; + +namespace WxPayAPI +{ + public class Refund + { + /*** + * 申请退款完整业务流程逻辑 + * @param transaction_id 微信订单号(优先使用) + * @param out_trade_no 商户订单号 + * @param total_fee 订单总金额 + * @param refund_fee 退款金额 + * @return 退款结果(xml格式) + */ + public static string Run(string transaction_id, string out_trade_no, string total_fee, string refund_fee) + { + Log.Info("Refund", "Refund is processing..."); + + WxPayData data = new WxPayData(); + if (!string.IsNullOrEmpty(transaction_id))//微信订单号存在的条件下,则已微信订单号为准 + { + data.SetValue("transaction_id", transaction_id); + } + else//微信订单号不存在,才根据商户订单号去退款 + { + data.SetValue("out_trade_no", out_trade_no); + } + + data.SetValue("total_fee", int.Parse(total_fee));//订单总金额 + data.SetValue("refund_fee", int.Parse(refund_fee));//退款金额 + data.SetValue("out_refund_no", WxPayApi.GenerateOutTradeNo());//随机生成商户退款单号 + data.SetValue("op_user_id", WxPayConfig.MCHID);//操作员,默认为商户号 + + WxPayData result = WxPayApi.Refund(data);//提交退款申请给API,接收返回数据 + + Log.Info("Refund", "Refund process complete, result : " + result.ToXml()); + return result.ToPrintStr(); + } + } +} \ No newline at end of file diff --git a/App.code/Pay/ReturnBean.cs b/App.code/Pay/ReturnBean.cs new file mode 100644 index 0000000..394afbb --- /dev/null +++ b/App.code/Pay/ReturnBean.cs @@ -0,0 +1,114 @@ +using System; + + +/// +/// ReturnBean 的摘要说明 +/// +public class ReturnBean +{ + private String return_code = null; + private String return_msg = null; + private String appid = null; + private String mch_id = null; + private String nonce_str = null; + private String sign = null; + private String result_code = null; + private String prepay_id = null; + private String trade_type = null; + private String mweb_url = null; + private string str; + + public ReturnBean(string str) + { + this.return_code = WX_Util.extract(str, "return_code"); + this.return_msg = WX_Util.extract(str, "return_msg"); + this.appid = WX_Util.extract(str, "appid"); + this.mch_id = WX_Util.extract(str, "mch_id"); + this.nonce_str = WX_Util.extract(str, "nonce_str"); + this.sign = WX_Util.extract(str, "sign"); + this.result_code = WX_Util.extract(str, "result_code"); + this.prepay_id = WX_Util.extract(str, "prepay_id"); + this.trade_type = WX_Util.extract(str, "trade_type"); + this.mweb_url = WX_Util.extract(str, "mweb_url"); + } + public String getReturn_code() + { + return return_code; + } + public void setReturn_code(String return_code) + { + this.return_code = return_code; + } + public String getReturn_msg() + { + return return_msg; + } + public void setReturn_msg(String return_msg) + { + this.return_msg = return_msg; + } + public String getAppid() + { + return appid; + } + public void setAppid(String appid) + { + this.appid = appid; + } + public String getMch_id() + { + return mch_id; + } + public void setMch_id(String mch_id) + { + this.mch_id = mch_id; + } + public String getNonce_str() + { + return nonce_str; + } + public void setNonce_str(String nonce_str) + { + this.nonce_str = nonce_str; + } + public String getSign() + { + return sign; + } + public void setSign(String sign) + { + this.sign = sign; + } + public String getResult_code() + { + return result_code; + } + public void setResult_code(String result_code) + { + this.result_code = result_code; + } + public String getPrepay_id() + { + return prepay_id; + } + public void setPrepay_id(String prepay_id) + { + this.prepay_id = prepay_id; + } + public String getTrade_type() + { + return trade_type; + } + public void setTrade_type(String trade_type) + { + this.trade_type = trade_type; + } + public String getMweb_url() + { + return mweb_url; + } + public void setMweb_url(String mweb_url) + { + this.mweb_url = mweb_url; + } +} \ No newline at end of file diff --git a/App.code/Pay/WX_Util.cs b/App.code/Pay/WX_Util.cs new file mode 100644 index 0000000..8ef3e20 --- /dev/null +++ b/App.code/Pay/WX_Util.cs @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography; +using System.Text; +using System.Web; +using System.Xml; + +/// +/// WX_Util 的摘要说明 +/// +public class WX_Util +{ + public static string getRandomStr() + { + return Guid.NewGuid().ToString().Replace("-", ""); + } + public static string extract(string str, string key) + { + SortedDictionary map = new SortedDictionary(); + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(str); + XmlNode xmlNode = xmlDoc.FirstChild;//获取到根节点 + XmlNodeList nodes = xmlNode.ChildNodes; + foreach (XmlNode xn in nodes) + { + XmlElement xe = (XmlElement)xn; + if (key.Equals(xe.Name)) + { + return xe.InnerText; + } + } + return ""; + } + + public static ReturnBean submit(SortedDictionary map, string key) + { + string str = null; + HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.mch.weixin.qq.com/pay/unifiedorder"); + request.Method = "POST"; + Stream requestStream = request.GetRequestStream(); + StreamWriter streamWriter = new StreamWriter(requestStream); + streamWriter.Write(getXML(map, key)); + streamWriter.Flush(); + + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + Stream responseStream = response.GetResponseStream(); + StreamReader streamReader = new StreamReader(responseStream); + str = streamReader.ReadToEnd(); + + streamWriter.Close(); + streamReader.Close(); + WX_Util.log(str); + return new ReturnBean(str); + } + + public static void log(string str) + { + System.IO.FileStream file = new System.IO.FileStream(HttpContext.Current.Server.MapPath("/upload/log.txt"), System.IO.FileMode.Append); + System.IO.StreamWriter writer = new System.IO.StreamWriter(file); + writer.WriteLine(str + "\n\r"); + writer.Close(); + } + + public static string getXML(SortedDictionary map, string key) + { + string xml = ""; + xml += "" + sign(map, key) + ""; + foreach (KeyValuePair pair in map) + { + if (pair.Value != null) + { + if (pair.Value.GetType() == typeof(int)) + { + xml += "<" + pair.Key + ">" + pair.Value + ""; + } + else if (pair.Value.GetType() == typeof(string)) + { + xml += "<" + pair.Key + ">" + ""; + } + } + } + xml += ""; + return xml; + } + + public static string sign(SortedDictionary map, string key) + { + string strb = ""; + foreach (KeyValuePair pair in map) + { + if (pair.Key != "sign" && pair.Value != "") + { + strb += pair.Key + "=" + pair.Value + "&"; + } + } + strb += "key=" + key; + + return md5(strb); + } + + public static string md5(string str) + { + var md5 = MD5.Create(); + var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); + var sb = new StringBuilder(); + foreach (byte b in bs) + { + sb.Append(b.ToString("x2")); + } + //所有字符转为大写 + return sb.ToString().ToUpper(); + } + public static string getRequestStr(HttpRequest request) + { + System.IO.Stream s = request.InputStream; + int count = 0; + byte[] buffer = new byte[1024]; + StringBuilder builder = new StringBuilder(); + while ((count = s.Read(buffer, 0, 1024)) > 0) + { + builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); + } + s.Flush(); + s.Close(); + s.Dispose(); + + return builder.ToString(); + } + public WX_Util() + { + // + // TODO: 在此处添加构造函数逻辑 + // + } +} \ No newline at end of file