/// <summary> /// 请求微信支付接口 /// </summary> /// <param name="url">string 接口地址</param> /// <param name="data">Utils.Hash 接口参数</param> /// <returns>string 返回结果</returns> public static string PostPay(string url, Utils.Hash data) { Encoding encode = Encoding.GetEncoding("UTF-8"); CookieCollection cookies = new CookieCollection(); ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ProtocolVersion = HttpVersion.Version10; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; StringBuilder dataBuffer = new StringBuilder(); dataBuffer.AppendFormat(data.ToXML()); byte[] dataBytes = encode.GetBytes(dataBuffer.ToString()); using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(dataBytes, 0, dataBytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { StreamReader responseReader = new StreamReader(responseStream); return(responseReader.ReadToEnd()); } } }
/// <summary> /// 执行 T-SQL 语句,并返回数据库记录集合 [ 含分页 ] /// </summary> /// <param name="sql">string T-SQL 语句</param> /// <param name="values">params object[] 参数集合 </param> /// <param name="pageId">int 页码</param> /// <param name="pageSize">int 页尺寸</param> /// <returns>Utils.HashCollection 记录集合</returns> public Utils.Hash GetHashCollectionByPageId(string sql, int pageId, int pageSize, params object[] values) { using (MySqlConnection conn = new MySqlConnection(this._ConnectionString)) { using (MySqlCommand comd = new MySqlCommand(sql, conn)) { double recordCount = 0; double recordStart = ((pageId - 1) * pageSize); double recordEnd = recordStart + pageSize; this.AddParameters(comd, values); conn.Open(); try { using (MySqlDataReader reader = comd.ExecuteReader()) { Utils.Hash result = new Utils.Hash(); Utils.HashCollection data = new Utils.HashCollection(); while (reader.Read()) { if (recordCount >= recordStart && recordCount < recordEnd) { Utils.Hash item = new Utils.Hash(); for (int i = 0; i < reader.FieldCount; i++) { if (reader.GetValue(i) == DBNull.Value && reader.GetFieldType(i).Name == "String") { item[reader.GetName(i)] = String.Empty; } else { item[reader.GetName(i)] = reader.GetValue(i); } } data.Add(item); } recordCount++; } reader.Close(); result["data"] = data; result["pageCount"] = Math.Ceiling(recordCount / pageSize); return(result); } } catch (Exception e) { throw e; } finally { conn.Close(); } } } }
/// <summary> /// 获取调起支付的参数 /// </summary> /// <param name="appId">string APP 标识</param> /// <param name="merchantId">string 商户号</param> /// <param name="merchantKey">string 商户秘钥</param> /// <param name="prepayId">string 支付编号</param> /// <returns>Hash 操作结果</returns> public static Utils.Hash Payment(string appId, string merchantId, string merchantKey, string prepayId) { Utils.Hash queries = new Utils.Hash(); queries["appId"] = appId; queries["timeStamp"] = Utils.Genre.GetTimeStamp(DateTime.Now); queries["nonceStr"] = Utils.Genre.GetRandom(20); queries["package"] = "prepay_id=" + prepayId; queries["signType"] = "MD5"; queries["paySign"] = Signature(merchantKey, queries); return(queries); }
/// <summary> /// 从 XML 文件中获取 Hash /// </summary> /// <param name="xml">string xml 内容</param> /// <returns>Hash 详细信息</returns> public static Hash FromXML(string xml) { Utils.Hash hashResult = new Utils.Hash(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement root = doc.DocumentElement; foreach (XmlElement node in root.ChildNodes) { hashResult.Add(node.Name, node.InnerText); } return(hashResult); }
/// <summary> /// 执行 T-SQL 语句,并返回数据库记录集合 /// </summary> /// <param name="sql">string T-SQL 语句</param> /// <param name="values">params object[] 参数集合 </param> /// <returns>Utils.HashCollection 记录集合</returns> public Utils.Hash GetHashCollection(string sql, params object[] values) { using (MySqlConnection conn = new MySqlConnection(this._ConnectionString)) { using (MySqlCommand comd = new MySqlCommand(sql, conn)) { this.AddParameters(comd, values); conn.Open(); try { using (MySqlDataReader reader = comd.ExecuteReader()) { Utils.Hash result = new Utils.Hash(); Utils.HashCollection data = new Utils.HashCollection(); while (reader.Read()) { Utils.Hash item = new Utils.Hash(); for (int i = 0; i < reader.FieldCount; i++) { if (reader.GetValue(i) == DBNull.Value && reader.GetFieldType(i).Name == "String") { item[reader.GetName(i)] = String.Empty; } else { item[reader.GetName(i)] = reader.GetValue(i); } } data.Add(item); } reader.Close(); result["data"] = data; return(result); } } catch (Exception e) { throw e; } finally { conn.Close(); } } } }
/// <summary> /// 字符串签名 /// </summary> /// <param name="merchantKey">string 商户秘钥</param> /// <param name="queries">string 参数集合</param> /// <returns>string 签名结果</returns> public static string Signature(string merchantKey, Utils.Hash queries) { string queryString = String.Empty; ArrayList allKeys = new ArrayList(queries.Keys); allKeys.Sort(); foreach (string key in allKeys) { if (key != "sign" && key != "paySign") { queryString += String.IsNullOrEmpty(queryString) ? "" : "&"; queryString += key + "=" + queries.ToString(key); } } queryString += "&key=" + merchantKey; return(Secret.MD5.Encode(queryString)); }
/// <summary> /// 获取支付信息 /// </summary> /// <param name="appId">string APP 标识</param> /// <param name="appSecret">string APP 秘钥</param> /// <param name="merchantId">string 商户号</param> /// <param name="merchantKey">string 商户秘钥</param> /// <param name="openId">string 用户标识</param> /// <param name="orderId">string 订单编号</param> /// <param name="body">string 商品描述</param> /// <param name="price">int 价格</param> /// <returns>Hash 操作结果</returns> public static Utils.Hash Prepay(string appId, string appSecret, string merchantId, string merchantKey, string openId, string orderId, string body, int price) { Utils.Hash queries = new Utils.Hash(); queries["appid"] = appId; queries["mch_id"] = merchantId; queries["nonce_str"] = Utils.Genre.GetRandom(20); queries["body"] = body; queries["out_trade_no"] = orderId; queries["total_fee"] = price; queries["spbill_create_ip"] = Utils.Genre.GetAddress(); queries["notify_url"] = "https://shenxu.name/capsule/api/third/wechat/pay/callback.ashx"; queries["trade_type"] = "JSAPI"; queries["openid"] = openId; queries["sign"] = Signature(merchantKey, queries); string xmlResult = API.PostPay("https://api.mch.weixin.qq.com/pay/unifiedorder", queries); return(Utils.Hash.FromXML(xmlResult)); }
/// <summary> /// 执行 T-SQL 语句,并返回数据库记录集合 /// </summary> /// <param name="sql">string T-SQL 语句</param> /// <param name="pageID">int 页码</param> /// <param name="pageSize">int 页尺寸</param> /// <param name="values">params object[] 参数集合 </param> /// <returns>Utils.HashCollection 记录集合</returns> public Utils.Hash GetHashCollection(string sql, int pageID, int pageSize, params object[] values) { using (SqlConnection conn = new SqlConnection(this._ConnectionString)) { Match match = Regex.Match(sql, "Order By [^()]*$", RegexOptions.IgnoreCase); int rowStart = ((pageID - 1) * pageSize) + 1; int rowEnd = rowStart + pageSize - 1; int recordCount = 0; string sqlOrder = "Order By 1 Asc"; string sqlCount = "Select Count(*) As Count From ({SQL}) T"; string sqlSelect = "Select * From (Select *,ROW_NUMBER() OVER ({ORDER}) AS [RowIndex] From ({SQL}) T) TT Where [RowIndex] Between " + rowStart + " And " + rowEnd + " Order By [RowIndex] Asc"; if (match.Success) { sql = sql.Replace(match.Value, ""); sqlOrder = match.Value; } sqlCount = sqlCount.Replace("{SQL}", sql); sqlCount = sqlCount.Replace("{ORDER}", sqlOrder); sqlSelect = sqlSelect.Replace("{SQL}", sql); sqlSelect = sqlSelect.Replace("{ORDER}", sqlOrder); using (SqlCommand comd = new SqlCommand(sqlSelect, conn)) { this.AddParameters(comd, values); conn.Open(); try { using (SqlCommand _comd = new SqlCommand(sqlCount, conn)) { recordCount = Utils.Parse.ToInt(_comd.ExecuteScalar()); } using (SqlDataReader reader = comd.ExecuteReader()) { Utils.Hash result = new Utils.Hash(); Utils.HashCollection data = new Utils.HashCollection(); while (reader.Read()) { Utils.Hash item = new Utils.Hash(); for (int i = 0; i < reader.FieldCount; i++) { if (reader.GetValue(i) == DBNull.Value && reader.GetFieldType(i).Name == "String") { item[reader.GetName(i)] = String.Empty; } else { item[reader.GetName(i)] = reader.GetValue(i); } } data.Add(item); } reader.Close(); result["Data"] = data; result["Count"] = recordCount; result["PageCount"] = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(recordCount) / Convert.ToDouble(pageSize))); return(result); } } catch (Exception e) { throw e; } finally { conn.Close(); } } } }