public string ToXml() { if (0 == this.m_values.Count) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData数据为空!", LogType.Error); } StringBuilder stringBuilder = new StringBuilder("<?xml version=\"1.0\" standalone=\"true\"?>"); stringBuilder.AppendLine("<xml>"); foreach (KeyValuePair <string, object> value in this.m_values) { if (value.Value == null) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData内部含有值为null的字段!", LogType.Error); return(""); } if (value.Value.GetType() == typeof(int)) { stringBuilder.AppendLine("<" + value.Key + ">" + value.Value + "</" + value.Key + ">"); } else if (value.Value.GetType() == typeof(string)) { stringBuilder.AppendLine("<" + value.Key + "><![CDATA[" + value.Value + "]]></" + value.Key + ">"); } else { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData字段数据类型错误!", LogType.Error); } } stringBuilder.AppendLine("</xml>"); return(stringBuilder.ToString()); }
public SortedDictionary <string, object> FromXml(string xml, string key) { if (string.IsNullOrEmpty(xml)) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "将空的xml串转换为WxPayData不合法!", LogType.Error); } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.XmlResolver = null; xmlDocument.LoadXml(xml); XmlNode firstChild = xmlDocument.FirstChild; XmlNodeList childNodes = firstChild.ChildNodes; foreach (XmlNode item in childNodes) { XmlElement xmlElement = (XmlElement)item; this.m_values[xmlElement.Name] = xmlElement.InnerText; } try { this.CheckSign(key); } catch (WxPayException ex) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), ex.Message, LogType.Error); } return(this.m_values); }
internal string GetMWebUrl(PayDictionary dict, string sign, out string prepayId) { prepayId = ""; dict.Add("sign", sign); string url = SignHelper.BuildQuery(dict, false); string text = SignHelper.BuildXml(dict, false); string text2 = PayClient.PostData(PayClient.prepay_id_Url, text); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.XmlResolver = null; try { xmlDocument.LoadXml(text2); } catch (Exception ex) { WxPayLog.writeLog(dict, "加载xml文件错误:" + text2 + ",错误信息:" + ex.Message, url, text, LogType.GetPrepayID); return(""); } try { if (xmlDocument == null) { WxPayLog.writeLog(dict, "加载xml文件错误:" + text2, url, text, LogType.GetPrepayID); return(""); } XmlNode xmlNode = xmlDocument.SelectSingleNode("xml/return_code"); XmlNode xmlNode2 = xmlDocument.SelectSingleNode("xml/result_code"); if (xmlNode == null || xmlNode2 == null) { WxPayLog.writeLog(dict, "retrunnode或者resultnode为空:" + text2, url, text, LogType.GetPrepayID); return(""); } XmlNode xmlNode3 = xmlDocument.SelectSingleNode("xml/prepay_id"); if (xmlNode3 != null) { prepayId = xmlNode3.InnerText; } if (xmlNode.InnerText == "SUCCESS" && xmlNode2.InnerText == "SUCCESS") { XmlNode xmlNode4 = xmlDocument.SelectSingleNode("xml/mweb_url"); if (xmlNode4 != null) { return(xmlNode4.InnerText); } WxPayLog.writeLog(dict, "获取mweb_url结节为空:" + text2, url, text, LogType.GetPrepayID); return(""); } WxPayLog.writeLog(dict, "返回状态为不成功:" + text2, url, text, LogType.GetPrepayID); return(""); } catch (Exception ex) { WxPayLog.writeLog(dict, "加载xml结点失败:" + text2 + ",错误信息:" + ex.Message, url, text, LogType.GetPrepayID); return(""); } }
public string ToPrintStr() { string text = ""; foreach (KeyValuePair <string, object> value in this.m_values) { if (value.Value == null) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData内部含有值为null的字段!", LogType.Error); } text += $"{value.Key}={value.Value.ToString()}<br>"; } return(text); }
private bool ValidPaySign(PayNotify notify, out string servicesign) { PayDictionary payDictionary = new PayDictionary(); payDictionary = Utils.GetPayDictionary(notify); servicesign = SignHelper.SignPay(payDictionary, this._payAccount.PartnerKey); bool flag = notify.sign == servicesign; if (!flag) { WxPayLog.writeLog(payDictionary, servicesign, "", "签名验证失败", LogType.PayNotify); } servicesign = servicesign + "-" + SignHelper.BuildQuery(payDictionary, false); return(flag); }
internal string BuildPackage(PackageInfo package) { PayDictionary payDictionary = new PayDictionary(); try { payDictionary.Add("appid", this._payAccount.AppId); payDictionary.Add("mch_id", this._payAccount.PartnerId); if (!string.IsNullOrEmpty(this._payAccount.sub_mch_id) && !string.IsNullOrEmpty(this._payAccount.Sub_AppId)) { payDictionary.Add("sub_mch_id", this._payAccount.sub_mch_id); payDictionary.Add("sub_appid", this._payAccount.Sub_AppId); payDictionary.Add("sub_openid", package.sub_openid); } else { payDictionary.Add("openid", package.OpenId); } payDictionary.Add("device_info", ""); payDictionary.Add("nonce_str", Utils.CreateNoncestr()); payDictionary.Add("body", package.Body); payDictionary.Add("attach", package.Attach); payDictionary.Add("out_trade_no", package.OutTradeNo); payDictionary.Add("total_fee", (int)package.TotalFee); payDictionary.Add("spbill_create_ip", package.SpbillCreateIp); payDictionary.Add("time_start", package.TimeExpire); payDictionary.Add("time_expire", ""); payDictionary.Add("goods_tag", package.GoodsTag); payDictionary.Add("notify_url", package.NotifyUrl); payDictionary.Add("trade_type", "JSAPI"); payDictionary.Add("product_id", ""); string sign = SignHelper.SignPackage(payDictionary, this._payAccount.PartnerKey); string text = this.GetPrepay_id(payDictionary, sign); if (text.Length > 64) { text = ""; } return(string.Format("prepay_id=" + text)); } catch (Exception ex) { WxPayLog.writeLog(payDictionary, "", "", ex.Message, LogType.Error); return(""); } }
public string ToUrl() { string text = ""; foreach (KeyValuePair <string, object> value in this.m_values) { if (value.Value == null) { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData内部含有值为null的字段!", LogType.Error); } if (value.Key != "sign" && value.Value.ToString() != "") { object obj = text; text = obj + value.Key + "=" + value.Value + "&"; } } return(text.Trim('&')); }
public bool CheckSign(string key) { if (!this.IsSet("sign")) { return(true); } if (this.GetValue("sign") == null || this.GetValue("sign").ToString() == "") { WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData签名存在但不合法!", LogType.Error); } string b = this.GetValue("sign").ToString(); string a = this.MakeSign(key); if (a == b) { return(true); } WxPayLog.writeLog(this.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "WxPayData签名验证错误!", LogType.Error); return(false); }
public WxPayData GetNotifyData() { IDictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("AppID", this.config.AppId); dictionary.Add("AppSecret", this.config.AppSecret); dictionary.Add("Key", this.config.Key); dictionary.Add("MchID", this.config.MchID); Stream inputStream = this.page.Request.InputStream; int num = 0; byte[] array = new byte[1024]; StringBuilder stringBuilder = new StringBuilder(); while ((num = inputStream.Read(array, 0, 1024)) > 0) { stringBuilder.Append(Encoding.UTF8.GetString(array, 0, num)); } inputStream.Flush(); inputStream.Close(); inputStream.Dispose(); WxPayData wxPayData = new WxPayData(); try { wxPayData.FromXml(stringBuilder.ToString(), this.config.Key); } catch (WxPayException ex) { WxPayData wxPayData2 = new WxPayData(); wxPayData2.SetValue("return_code", "FAIL"); wxPayData2.SetValue("return_msg", ex.Message); dictionary.Add("return_code", "FAIL"); dictionary.Add("result", wxPayData2.ToXml()); WxPayLog.writeLog(dictionary, "", HttpContext.Current.Request.Url.ToString(), ex.Message, LogType.Error); this.page.Response.Write(wxPayData2.ToXml()); this.page.Response.End(); } return(wxPayData); }
public static string Get(string url, string PROXY_URL = "") { IDictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("PROXY_URL", PROXY_URL); GC.Collect(); string result = ""; HttpWebRequest httpWebRequest = null; HttpWebResponse httpWebResponse = null; try { ServicePointManager.DefaultConnectionLimit = 200; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = HttpService.CheckValidationResult; } httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Method = "GET"; if (!string.IsNullOrEmpty(PROXY_URL)) { WebProxy webProxy = new WebProxy(); webProxy.Address = new Uri(PROXY_URL); httpWebRequest.Proxy = webProxy; } httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); result = streamReader.ReadToEnd().Trim(); streamReader.Close(); } catch (ThreadAbortException ex) { dictionary.Add("HttpService", "Thread - caught ThreadAbortException - resetting."); dictionary.Add("Exception message: {0}", ex.Message); WxPayLog.writeLog(dictionary, "", url, "", LogType.Error); Thread.ResetAbort(); } catch (WebException ex2) { dictionary.Add("HttpService", ex2.ToString()); if (ex2.Status == WebExceptionStatus.ProtocolError) { dictionary.Add("HttpService", "StatusCode : " + ((HttpWebResponse)ex2.Response).StatusCode); dictionary.Add("HttpService", "StatusDescription : " + ((HttpWebResponse)ex2.Response).StatusDescription); } WxPayLog.writeLog(dictionary, "", url, "", LogType.Error); return(""); } catch (Exception ex3) { dictionary.Add("HttpService", ex3.ToString()); WxPayLog.writeLog(dictionary, "", url, "", LogType.Error); return(""); } finally { httpWebResponse?.Close(); httpWebRequest?.Abort(); } return(result); }
public static string SendRequest(PayInfo pay, PayConfig config) { IDictionary <string, string> dictionary = new Dictionary <string, string>(); WxPayData wxPayData = new WxPayData(); wxPayData.SetValue("auth_code", pay.Attach); wxPayData.SetValue("body", pay.OutTradeNo); WxPayData wxPayData2 = wxPayData; decimal totalFee = pay.TotalFee; wxPayData2.SetValue("total_fee", int.Parse(totalFee.ToString())); wxPayData.SetValue("out_trade_no", pay.OutTradeNo); dictionary.Add("auth_code", pay.AuthCode); dictionary.Add("body", pay.OutTradeNo); IDictionary <string, string> dictionary2 = dictionary; totalFee = pay.TotalFee; dictionary2.Add("total_fee", totalFee.ToString()); dictionary.Add("out_trade_no", pay.OutTradeNo); dictionary.Add("AppId", config.AppId); dictionary.Add("AppSecret", config.AppSecret); dictionary.Add("MchID", config.MchID); dictionary.Add("Key", config.Key); dictionary.Add("NOTIFY_URL", config.NOTIFY_URL); WxPayData wxPayData3 = WxPayApi.Micropay(wxPayData, config, 10); if (!wxPayData3.IsSet("return_code") || wxPayData3.GetValue("return_code").ToString() == "FAIL") { string msg = wxPayData3.IsSet("return_msg") ? wxPayData3.GetValue("return_msg").ToString() : ""; WxPayLog.writeLog(dictionary, "", HttpContext.Current.Request.Url.ToString(), msg, LogType.MicroPay); } wxPayData3.CheckSign(config.Key); if (wxPayData3.GetValue("return_code").ToString() == "SUCCESS" && wxPayData3.GetValue("result_code").ToString() == "SUCCESS") { return(wxPayData3.ToPrintStr()); } if (wxPayData3.GetValue("err_code").ToString() != "USERPAYING" && wxPayData3.GetValue("err_code").ToString() != "SYSTEMERROR") { return(wxPayData3.ToPrintStr()); } string out_trade_no = wxPayData.GetValue("out_trade_no").ToString(); int num = 10; while (num-- > 0) { int num3 = 0; WxPayData wxPayData4 = MicroPay.Query(out_trade_no, config, out num3); switch (num3) { case 2: break; case 1: return(wxPayData4.ToPrintStr()); default: return(wxPayData3.ToPrintStr()); } Thread.Sleep(2000); } if (!MicroPay.Cancel(out_trade_no, config, 0)) { WxPayLog.writeLog(dictionary, "", HttpContext.Current.Request.Url.ToString(), "支付失败并且撤销订单失败", LogType.MicroPay); throw new WxPayException("Reverse order failure!"); } return(wxPayData3.ToPrintStr()); }