/Common/AliPay.cs
C# | 341 lines | 216 code | 28 blank | 97 comment | 26 complexity | df0ff8bb8fe86a01e25288fa60547c66 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- using System.Xml;
- namespace YuuzleComic.Common {
- /// <summary>
- /// ??????
- /// </summary>
- public class AliPay {
- public Dictionary<string, string> Config { set; get; }
- private Dictionary<string, string> sPara = new Dictionary<string, string>();
- public string PayGetWay { set; get; }
- public string NotifyGetWay { set; get; }
- public string LogFilePath { get; set; }
- public AliPay()
- : this("") {
- }
- public AliPay(string hosturl) {
- #region ??????
- Config = new Dictionary<string, string>();
- Config["partner"] = "2088302492797137";
- Config["key"] = "ub1y7vomann13oy3rfpir7wfrplwrp6y";
- Config["seller_email"] = "admin@foosun.net";
- Config["return_url"] = hosturl + "/user/info/AlipayNotify.aspx";
- Config["notify_url"] = hosturl + "/user/info/AlipayNotify.aspx";
- Config["show_url"] = hosturl + "/user/info/history.aspx?type=2";
- Config["mainname"] = "????????????";
- #endregion
-
- #region ??????
- Config["_input_charset"] = "utf-8";
- Config["sign_type"] = "MD5";
- //??????????????
- Config["transport"] = "https";
- #endregion
-
- #region ????
- PayGetWay = "https://www.alipay.com/cooperate/gateway.do";
- NotifyGetWay = "http://notify.alipay.com/trade/notify_query.do";
- #endregion
- }
-
- #region ????
- public bool Notify(SortedDictionary<string, string> request) {
- if (Config["transport"] == "https") {
- NotifyGetWay = PayGetWay;
- }
- sPara = Para_filter(request);
- string preSignStr = DicToQueryString(sPara);
- string mysign = Build_mysign(sPara, Config["key"], Config["sign_type"], Config["_input_charset"]);
- string responseTxt = Verify(sPara["notify_id"]);
- string sWord = "responseTxt=" + responseTxt + "\n notify_url_log:sign=" + request["sign"] + "&mysign=" + mysign + "\n notify??????" + preSignStr;
- if (request["sign"] == mysign && responseTxt == "true") {
- return true;
- }
- else {
- log_result(LogFilePath, sWord);
- return false;
- }
- }
-
- /// <summary>
- /// ????????????????
- /// </summary>
- /// <returns>????</returns>
- private string Verify(string notify_id) {
- UriBuilder veryfy = new UriBuilder(NotifyGetWay);
- string query = "";
- if (Config["transport"] == "https") {
- query = "service=notify_verify&";
- }
- veryfy.Query = query + "partner=" + Config["partner"] + "¬ify_id=" + notify_id;
- return Get_Http(veryfy.Uri.ToString(), 120000);
- }
-
- /// <summary>
- /// ???????ATN??
- /// </summary>
- /// <param name="strUrl">??URL????</param>
- /// <param name="timeout">??????</param>
- /// <returns>???ATN??</returns>
- 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;
- }
-
- #endregion
-
- #region ????
- /// <summary>
- /// ??????HTML
- /// </summary>
- /// <param name="sendType">???????GET?POST?????</param>
- /// <returns>?? ????HTML??</returns>
- public string Build_Form(string sendType) {
- StringBuilder sbHtml = new StringBuilder();
- sendType = sendType.ToLower();
- if (sendType != "post") {
- sendType = "get";
- }
-
- sbHtml.Append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + PayGetWay + "_input_charset=" + Config["_input_charset"] + "\" method=\"" + sendType.ToLower() + "\">");
-
- foreach (KeyValuePair<string, string> temp in sPara) {
- sbHtml.Append("<input type=\"hidden\" name=\"" + temp.Key + "\" value=\"" + temp.Value + "\"/>");
- }
-
- sbHtml.Append("<input type=\"hidden\" name=\"sign\" value=\"" + sPara["sign"] + "\"/>");
- sbHtml.Append("<input type=\"hidden\" name=\"sign_type\" value=\"" + sPara["sign_type"] + "\"/>");
-
- //submit?????????name??
- sbHtml.Append("<input type=\"submit\" value=\"???????\"></form>");
- sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>");
- return sbHtml.ToString();
- }
-
- /// <summary>
- /// ?????????URL?????
- /// </summary>
- /// <returns>?????URL????????</returns>
- public string Build_GetUrl() {
- UriBuilder urlBuilder = new UriBuilder(PayGetWay);
- urlBuilder.Query = DicToQueryString(sPara);
- return urlBuilder.Uri.ToString();
- }
-
- /// <summary>
- /// ??????????
- /// </summary>
- /// <param name="out_trade_no">???</param>
- /// <param name="subject">??</param>
- /// <param name="body">??</param>
- /// <param name="total_fee">??</param>
- public void CreateDirectPayByUser(string out_trade_no, string subject, string body, string total_fee) {
- var para = GetConfig();
- para.Add("service", "create_direct_pay_by_user");
- para.Add("payment_type", "1");
- para.Add("out_trade_no", out_trade_no);
- para.Add("subject", subject);
- para.Add("body", body);
- para.Add("total_fee", total_fee);
- //param.Add("paymethod", paymethod);
- //param.Add("defaultbank", defaultbank);
- //param.Add("anti_phishing_key", anti_phishing_key);
- //param.Add("exter_invoke_ip", exter_invoke_ip);
- //param.Add("extra_common_param", extra_common_param);
- //param.Add("buyer_email", buyer_email);
- //param.Add("royalty_type", royalty_type);
- //param.Add("royalty_parameters", royalty_parameters);
- sPara = Para_filter(para);
- sPara.Add("sign", Build_mysign(sPara, para["key"], para["sign_type"], para["_input_charset"]));
- sPara.Add("sign_type", Config["sign_type"]);
- }
-
- /// <summary>
- /// ??????????
- /// </summary>
- /// <param name="out_trade_no">???</param>
- /// <param name="subject">??</param>
- /// <param name="body">??</param>
- /// <param name="price">??</param>
- public void CreatePartnerTradeByBuyer(string out_trade_no, string subject, string body, string price) {
- var para = GetConfig();
- para.Add("service", "create_partner_trade_by_buyer");
- para.Add("payment_type", "1");
- para.Add("out_trade_no", out_trade_no);
- para.Add("subject", subject);
- para.Add("body", body);
- para.Add("price", price);
- sPara = Para_filter(para);
- sPara.Add("sign", Build_mysign(sPara, para["key"], para["sign_type"], para["_input_charset"]));
- sPara.Add("sign_type", Config["sign_type"]);
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="out_trade_no">???</param>
- /// <param name="subject">??</param>
- /// <param name="body">??</param>
- /// <param name="price">??</param>
- public void TradeCreateByBuyer(string out_trade_no, string subject, string body, string price) {
- var para = GetConfig();
- para.Add("service", "trade_create_by_buyer");
- para.Add("payment_type", "1");
- para.Add("out_trade_no", out_trade_no);
- para.Add("subject", subject);
- para.Add("body", body);
- para.Add("price", price);
- sPara = Para_filter(para);
- sPara.Add("sign", Build_mysign(sPara, para["key"], para["sign_type"], para["_input_charset"]));
- sPara.Add("sign_type", Config["sign_type"]);
- }
- #endregion
-
- #region ????
- /// <summary>
- /// ?????????
- /// </summary>
- /// <param name="request">??</param>
- /// <returns>??????</returns>
- public SortedDictionary<string, string> RequestToDic(NameValueCollection request) {
- SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();
- String[] requestItem = request.AllKeys;
- for (int i = 0; i < requestItem.Length; i++) {
- sArray.Add(requestItem[i], request[requestItem[i]]);
- }
- return sArray;
- }
-
- /// <summary>
- /// ?????????
- /// </summary>
- /// <returns>????????</returns>
- public SortedDictionary<string, string> GetConfig() {
- SortedDictionary<string, string> param = new SortedDictionary<string, string>();
- foreach (var item in Config) {
- param.Add(item.Key, item.Value);
- }
- return param;
- }
-
- /// <summary>
- /// ????????????=???????&????????
- /// </summary>
- /// <param name="dicArray">???????</param>
- /// <returns>??????????</returns>
- public string DicToQueryString(Dictionary<string, string> dicArray) {
- StringBuilder queryBuilder = new StringBuilder();
- foreach (KeyValuePair<string, string> temp in dicArray) {
- queryBuilder.Append(temp.Key + "=" + temp.Value + "&");
- }
- string queryString = queryBuilder.ToString();
- return queryString.Substring(0, queryString.Length - 1);
- }
-
- /// <summary>
- /// ?????????????
- /// </summary>
- /// <param name="dicArrayPre">???????</param>
- /// <returns>???????</returns>
- public 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() != "key"
- && temp.Key.ToLower() != "sign_type"
- && temp.Key.ToLower() != "transport"
- && !string.IsNullOrEmpty(temp.Value)) {
- dicArray.Add(temp.Key.ToLower(), temp.Value);
- }
- }
- return dicArray;
- }
-
- /// <summary>
- /// ??????
- /// </summary>
- /// <param name="sArray">??????</param>
- /// <param name="key">?????</param>
- /// <param name="sign_type">????</param>
- /// <param name="_input_charset">????</param>
- /// <returns>???????</returns>
- public string Build_mysign(Dictionary<string, string> dicArray, string key, string sign_type, string _input_charset) {
- string prestr = DicToQueryString(dicArray); //????????????=???????&????????
- prestr = prestr + key; //?????????????????????
- string mysign = Sign(prestr, sign_type, _input_charset); //????????????????
- return mysign;
- }
-
- /// <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 void log_result(string sPath, string sWord) {
- StreamWriter fs = new StreamWriter(sPath, false, System.Text.Encoding.Default);
- fs.Write(sWord);
- fs.Close();
- }
-
- /// <summary>
- /// ??????????query_timestamp???????????
- /// ???????XML????IIS???????
- /// </summary>
- /// <returns>??????</returns>
- public string Query_timestamp() {
- string url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + Config["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;
- }
- #endregion
- }
- }