using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; namespace WX.CRM.Common.WxPay { public class CheckPayApi { public CheckPayApi(string username, string secret, string identityKey) { UserName = username; Secret = secret; IdentityKey = identityKey; } /// /// 微信支付状态 /// /// /// public ApiResult WechatPayStatus(Dictionary param) { try { string url = $"{apiPath}wechat/queryTradeStatusByChannel"; //默认参数 //拼接验证头 Dictionary header = new Dictionary(); var timestamps = GetTimeStamp(); var sign = GetSign(param, timestamps); var authorization = $"{UserName}:{sign}"; header.Add("authorization", authorization); header.Add("timestamps", timestamps); //请求 var req = Post(url, param, header); LogHelper.Info("wechat:" + req); //返回 var reqInfo = JsonConvert.DeserializeObject>(req); if (reqInfo == null) { return new ApiResult { code = "501", message = "数据为空" }; } return reqInfo; } catch (Exception ex) { return new ApiResult { code = "501", message = ex.Message }; } } /// /// 支付宝支付状态 /// /// /// public ApiResult AliPayStatus(Dictionary param) { try { string url = $"{apiPath}alipay/queryTradeStatusByChannel"; //拼接验证头 Dictionary header = new Dictionary(); var timestamps = GetTimeStamp(); var sign = GetSign(param, timestamps); var authorization = $"{UserName}:{sign}"; header.Add("authorization", authorization); header.Add("timestamps", timestamps); //请求 var req = Post(url, param, header); LogHelper.Info("alipay:" + req); //返回 var reqInfo = JsonConvert.DeserializeObject>(req); if (reqInfo == null) { return new ApiResult { code = "501", message = "数据为空" }; } return reqInfo; } catch (Exception ex) { return new ApiResult { code = "501", message = ex.Message }; } } #region 内部参数 // https://uppay.soft.dn8188.com/payServer/ 正式 // https://uppay.soft.dn8188.com/payServerTest/ 测试 // "https://uppay.soft.dn8188.com/unifiedpaytest/"; 本地 private string apiPath = Utility.GetSettingByKey("DGpayApi"); private string UserName { get; set; } private string Secret { get; set; } //DG_SOFTWARE private string IdentityKey { get; set; } #endregion #region 内部方法 /// /// 内置请求 /// /// /// private string Post(string url, Dictionary param, Dictionary header) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //请求设置 request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.Timeout = 3000;//设置超时时间 //拼接请求头 foreach (var item in header) { request.Headers.Add(item.Key, item.Value); } //拼接参数 if (param.Any()) { var pStr = JsonConvert.SerializeObject(param); byte[] data = Encoding.UTF8.GetBytes(pStr); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } //提交请求 HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } //解析请求结果 using (Stream myResponseStream = response.GetResponseStream()) { StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); //记录请求情况 LogHelper.Info($"url:{url},param:{JsonConvert.SerializeObject(param)},header:{JsonConvert.SerializeObject(header)},req:{retString}"); return retString; } } /// /// 计算签名 /// /// /// /// private string GetSign(Dictionary param, string timestamps) { //一次排序 var newP = param.OrderBy(m => m.Key).ToDictionary(m => m.Key, n => n.Value); var pJosn = JsonConvert.SerializeObject(newP); //二次排序 var enStrList = new string[] { UserName, pJosn, Secret, timestamps }; Array.Sort(enStrList, string.CompareOrdinal); //拼接 var enStr = string.Join("", enStrList); //md5 加密 return _md5(enStr); } /// /// 计算 md5 /// /// /// private string _md5(string enCode) { string res = ""; byte[] data = Encoding.GetEncoding("utf-8").GetBytes(enCode); MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes = md5.ComputeHash(data); for (int i = 0; i < bytes.Length; i++) { res += bytes[i].ToString("x2"); } return res; } /// /// 获取时间戳 /// /// private string GetTimeStamp() { TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } #endregion } /// /// 返回格式 /// /// public class ApiResult where T : new() { public string code { get; set; } public T data { get; set; } public string message { get; set; } } public class PayStatus { /// /// 三方订单号(微信/支付宝) /// public string tradeNo { get; set; } /// /// 商户订单号 /// public string outTradeNo { get; set; } /// /// 金额 /// public int totalAmount { get; set; } /// /// 状态 0 未支付 1 已支付 /// public int tradeStatus { get; set; } /// /// 付款时间 /// public DateTime? payTime { get; set; } } }