public ActionResult Product()
        {
            var alertMsg = new AlertMsg();

            WxPayLog.Info(this.GetType().ToString(), "page load");
            WxPayJsApi jsApiPay = new WxPayJsApi(Request);

            try
            {
                //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                var url = jsApiPay.GetOpenidAndAccessToken();
                if (url != null)
                {
                    return(Redirect(url));
                }
                //获取收货地址js函数入口参数
                var wxEditAddrParam = jsApiPay.GetEditAddressParameters();
                TempData["openid"]      = jsApiPay.openid;
                alertMsg.IsSuccess      = true;
                ViewBag.WxEditAddrParam = wxEditAddrParam;
            }
            catch (Exception ex)
            {
                alertMsg.Message = $"页面加载出错,请重试,错误信息:{ex.Message}";
            }
            return(View(alertMsg));
        }
Пример #2
0
		public static WxPayData Report(WxPayData inputObj, PayConfig config, int timeOut = 1)
		{
			string url = "https://api.mch.weixin.qq.com/payitil/report";
			if (!inputObj.IsSet("interface_url"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "接口URL,缺少必填参数interface_url!", LogType.Report);
			}
			if (!inputObj.IsSet("return_code"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "返回状态码,缺少必填参数return_code!", LogType.Report);
			}
			if (!inputObj.IsSet("result_code"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "业务结果,缺少必填参数result_code!", LogType.Report);
			}
			if (!inputObj.IsSet("user_ip"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "访问接口IP,缺少必填参数user_ip!", LogType.Report);
			}
			if (!inputObj.IsSet("execute_time_"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "接口耗时,缺少必填参数execute_time_!", LogType.Report);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("user_ip", config.IPAddress);
			inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss"));
			inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			string xml2 = HttpService.Post(xml, url, false, config, timeOut);
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			return wxPayData;
		}
        public ActionResult JsApiPay(string openid, string total_fee)
        {
            WxPayLog.Info(this.GetType().ToString(), "JsApiPayPage load");
            //检测是否给当前页面传递了相关参数
            if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee))
            {
                WxPayLog.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit...");
                return(View(new AlertMsg("页面传参出错, 请返回重试")));
            }

            var alertMsg = new AlertMsg();
            //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数
            WxPayJsApi jsApiPay = new WxPayJsApi(Request);

            jsApiPay.openid    = openid;
            jsApiPay.total_fee = int.Parse(total_fee);

            //JSAPI支付预处理
            try
            {
                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                var       wxJsApiParam       = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数
                WxPayLog.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
                //在页面上显示订单信息
                alertMsg.IsSuccess   = true;
                alertMsg.Message     = ("订单详情:" + unifiedOrderResult.ToPrintStr());
                ViewBag.WxJsApiParam = wxJsApiParam;
            }
            catch (Exception ex)
            {
                alertMsg.Message = $"下单失败,请返回重试,错误信息:{ex.Message}";
            }
            return(View(alertMsg));
        }
Пример #4
0
        public static NameValueCollection GetOpenidAndAccessToken(Page page, string appId, string appSecret, bool appendLog = false)
        {
            if (!string.IsNullOrEmpty(page.Request.QueryString["code"]))
            {
                string code = page.Request.QueryString["code"];
                return(JsApiPay.GetOpenidAndAccessTokenFromCode(code, page, appId, appSecret, false));
            }
            string    host      = page.Request.Url.Host;
            string    path      = page.Request.Path;
            string    value     = HttpUtility.UrlEncode(page.Request.Url.ToString());
            WxPayData wxPayData = new WxPayData();

            wxPayData.SetValue("appid", appId);
            wxPayData.SetValue("redirect_uri", value);
            wxPayData.SetValue("response_type", "code");
            wxPayData.SetValue("scope", "snsapi_base");
            wxPayData.SetValue("state", "STATE#wechat_redirect");
            string text = "https://open.weixin.qq.com/connect/oauth2/authorize?" + wxPayData.ToUrl();

            if (appendLog)
            {
                WxPayLog.AppendLog(JsApiPay.paramDict, text, page.Request.Url.ToString(), "未获取到code,跳转去获取code页面", LogType.GetTokenOrOpenID);
            }
            try
            {
                page.Response.Redirect(text);
            }
            catch (ThreadAbortException)
            {
            }
            return(null);
        }
Пример #5
0
		public static WxPayData Micropay(WxPayData inputObj, PayConfig config, int timeOut = 10)
		{
			string text = "https://api.mch.weixin.qq.com/pay/micropay";
			if (!inputObj.IsSet("body"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少必填参数body", LogType.MicroPay);
			}
			else if (!inputObj.IsSet("out_trade_no"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少必填参数out_trade_no", LogType.MicroPay);
			}
			else if (!inputObj.IsSet("total_fee"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少必填参数total_fee", LogType.MicroPay);
			}
			else if (!inputObj.IsSet("auth_code"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少必填参数auth_code", LogType.MicroPay);
			}
			inputObj.SetValue("spbill_create_ip", config.IPAddress);
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			DateTime now = DateTime.Now;
			string xml2 = HttpService.Post(xml, text, false, config, timeOut);
			DateTime now2 = DateTime.Now;
			int timeCost = (int)(now2 - now).TotalMilliseconds;
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			WxPayApi.ReportCostTime(text, timeCost, wxPayData, config);
			return wxPayData;
		}
Пример #6
0
        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());
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            WxPayLog.Info(this.GetType().ToString(), "page load");
            if (!IsPostBack)
            {
                var jsApiPay = new WxPayJsApi(this);
                try
                {
                    //调用【网页授权获取用户信息】接口获取用户的openid和access_token
                    jsApiPay.GetOpenidAndAccessToken();

                    //获取收货地址js函数入口参数
                    wxEditAddrParam     = jsApiPay.GetEditAddressParameters();
                    ViewState["openid"] = jsApiPay.openid;
                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面加载出错,请重试" + "</span>");
                    Button1.Visible = false;
                    Button2.Visible = false;
                    Label1.Visible  = false;
                    Label2.Visible  = false;
                }
            }
        }
Пример #8
0
        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);
        }
Пример #9
0
        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 ActionResult NativePay()
        {
            WxPayLog.Info(this.GetType().ToString(), "page load");

            //生成扫码支付模式一url
            ViewBag.QRCode1 = WxPayApiBiz.GetNativePrePayUrl("123456789");

            //生成扫码支付模式二url
            ViewBag.QRCode2 = WxPayApiBiz.GetNativePayUrl("123456789");

            return(View());
        }
Пример #11
0
        public PayNotify GetPayNotify(string xml)
        {
            DataTable dataTable = this.ErrorTable("Notify");
            DataRow   dataRow   = dataTable.NewRow();

            dataRow["OperTime"] = DateTime.Now;
            try
            {
                if (string.IsNullOrEmpty(xml))
                {
                    return(null);
                }
                PayNotify notifyObject = Utils.GetNotifyObject <PayNotify>(xml);
                string    sign         = "";
                if (notifyObject == null || !this.ValidPaySign(notifyObject, out sign))
                {
                    dataTable.Rows.Add(dataRow);
                    IDictionary <string, string> dictionary = new Dictionary <string, string>();
                    dictionary.Add("ErrorMsg", (notifyObject == null) ? "Notify Null" : "Valid pay Sign Error");
                    dictionary.Add("result", xml);
                    WxPayLog.AppendLog(dictionary, sign, "", "签名验证失败", LogType.PayNotify);
                    return(null);
                }
                notifyObject.PayInfo = new PayInfo
                {
                    SignType      = "MD5",
                    Sign          = notifyObject.sign,
                    TradeMode     = 0,
                    BankType      = notifyObject.bank_type,
                    BankBillNo    = "",
                    TotalFee      = (decimal)notifyObject.total_fee / 100m,
                    FeeType       = ((notifyObject.fee_type == "CNY") ? 1 : 0),
                    NotifyId      = "",
                    TransactionId = notifyObject.transaction_id,
                    OutTradeNo    = notifyObject.out_trade_no,
                    TransportFee  = 0m,
                    ProductFee    = 0m,
                    Discount      = 1m,
                    BuyerAlias    = "",
                    Attach        = notifyObject.attach
                };
                return(notifyObject);
            }
            catch (Exception ex)
            {
                IDictionary <string, string> dictionary = new Dictionary <string, string>();
                dictionary.Add("result", xml);
                WxPayLog.WriteExceptionLog(ex, dictionary, LogType.Error);
                return(null);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            WxPayLog.Info(this.GetType().ToString(), "page load");

            //生成扫码支付模式一url
            string url1 = WxPayApiBiz.GetNativePrePayUrl("123456789");

            //生成扫码支付模式二url
            string url2 = WxPayApiBiz.GetNativePayUrl("123456789");

            //将url生成二维码图片
            Image1.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url1);
            Image2.ImageUrl = "MakeQRCode.aspx?data=" + HttpUtility.UrlEncode(url2);
        }
Пример #13
0
        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);
        }
Пример #14
0
		public static WxPayData UnifiedOrder(WxPayData inputObj, PayConfig config, int timeOut = 6)
		{
			string text = "https://api.mch.weixin.qq.com/pay/unifiedorder";
			if (!inputObj.IsSet("out_trade_no"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少统一支付接口必填参数out_trade_no!", LogType.UnifiedOrder);
			}
			else if (!inputObj.IsSet("body"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少统一支付接口必填参数body!", LogType.UnifiedOrder);
			}
			else if (!inputObj.IsSet("total_fee"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少统一支付接口必填参数total_fee!", LogType.UnifiedOrder);
			}
			else if (!inputObj.IsSet("trade_type"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "缺少统一支付接口必填参数trade_type!", LogType.UnifiedOrder);
			}
			if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!", LogType.UnifiedOrder);
			}
			if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!", LogType.UnifiedOrder);
			}
			if (!inputObj.IsSet("NOTIFY_URL"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "统一支付接口中,缺少必填参数NOTIFY_URL!", LogType.UnifiedOrder);
			}
			if (!inputObj.IsSet("NOTIFY_URL"))
			{
				inputObj.SetValue("NOTIFY_URL", config.NOTIFY_URL);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("spbill_create_ip", config.IPAddress);
			inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			DateTime now = DateTime.Now;
			string xml2 = HttpService.Post(xml, text, false, config, timeOut);
			DateTime now2 = DateTime.Now;
			int timeCost = (int)(now2 - now).TotalMilliseconds;
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			WxPayApi.ReportCostTime(text, timeCost, wxPayData, config);
			return wxPayData;
		}
Пример #15
0
        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);
        }
Пример #16
0
		private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj, PayConfig config)
		{
			if (config.REPORT_LEVENL != 0 && (config.REPORT_LEVENL != 1 || !inputObj.IsSet("return_code") || !(inputObj.GetValue("return_code").ToString() == "SUCCESS") || !inputObj.IsSet("result_code") || !(inputObj.GetValue("result_code").ToString() == "SUCCESS")))
			{
				WxPayData wxPayData = new WxPayData();
				wxPayData.SetValue("interface_url", interface_url);
				wxPayData.SetValue("execute_time_", timeCost);
				if (inputObj.IsSet("return_code"))
				{
					wxPayData.SetValue("return_code", inputObj.GetValue("return_code"));
				}
				if (inputObj.IsSet("return_msg"))
				{
					wxPayData.SetValue("return_msg", inputObj.GetValue("return_msg"));
				}
				if (inputObj.IsSet("result_code"))
				{
					wxPayData.SetValue("result_code", inputObj.GetValue("result_code"));
				}
				if (inputObj.IsSet("err_code"))
				{
					wxPayData.SetValue("err_code", inputObj.GetValue("err_code"));
				}
				if (inputObj.IsSet("err_code_des"))
				{
					wxPayData.SetValue("err_code_des", inputObj.GetValue("err_code_des"));
				}
				if (inputObj.IsSet("out_trade_no"))
				{
					wxPayData.SetValue("out_trade_no", inputObj.GetValue("out_trade_no"));
				}
				if (inputObj.IsSet("device_info"))
				{
					wxPayData.SetValue("device_info", inputObj.GetValue("device_info"));
				}
				try
				{
					WxPayApi.Report(wxPayData, config, 1);
				}
				catch (WxPayException)
				{
					WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "关闭订单接口中,out_trade_no必填!", LogType.CloseOrder);
				}
			}
		}
Пример #17
0
        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("");
            }
        }
Пример #18
0
        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('&'));
        }
Пример #19
0
        public static void GetCode(Page page, string appId, bool appendLog = false)
        {
            string    text      = HttpUtility.UrlEncode(page.Request.Url.ToString());
            WxPayData wxPayData = new WxPayData();

            wxPayData.SetValue("appid", appId);
            wxPayData.SetValue("redirect_uri", text);
            wxPayData.SetValue("response_type", "code");
            wxPayData.SetValue("scope", "snsapi_base");
            wxPayData.SetValue("state", "STATE#wechat_redirect");
            string text2 = "https://open.weixin.qq.com/connect/oauth2/authorize?" + wxPayData.ToUrl();

            if (appendLog)
            {
                WxPayLog.AppendLog(JsApiPay.paramDict, text2, text, "跳转去获取code页面", LogType.GetTokenOrOpenID);
            }
            page.Response.Redirect(text2);
        }
Пример #20
0
        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);
        }
Пример #21
0
		public static WxPayData Reverse(WxPayData inputObj, PayConfig config, int timeOut = 6)
		{
			string text = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
			if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "撤销订单API接口中缺少必填参数out_trade_no 或者 transaction_id", LogType.CloseOrder);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			DateTime now = DateTime.Now;
			string xml2 = HttpService.Post(xml, text, true, config, timeOut);
			DateTime now2 = DateTime.Now;
			int timeCost = (int)(now2 - now).TotalMilliseconds;
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			WxPayApi.ReportCostTime(text, timeCost, wxPayData, config);
			return wxPayData;
		}
Пример #22
0
		public static WxPayData ShortUrl(WxPayData inputObj, PayConfig config, int timeOut = 6)
		{
			string text = "https://api.mch.weixin.qq.com/tools/shorturl";
			if (!inputObj.IsSet("long_url"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "需要转换的URL,签名用原串,传输需URL encode!", LogType.ShortUrl);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			DateTime now = DateTime.Now;
			string xml2 = HttpService.Post(xml, text, false, config, timeOut);
			DateTime now2 = DateTime.Now;
			int timeCost = (int)(now2 - now).TotalMilliseconds;
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			WxPayApi.ReportCostTime(text, timeCost, wxPayData, config);
			return wxPayData;
		}
Пример #23
0
        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);
        }
Пример #24
0
        public static string GetEditAddressParameters(string appId, string access_token, bool appendLog = false)
        {
            string text = "";

            try
            {
                string    host      = JsApiPay.page.Request.Url.Host;
                string    path      = JsApiPay.page.Request.Path;
                string    query     = JsApiPay.page.Request.Url.Query;
                string    value     = "http://" + host + path + query;
                WxPayData wxPayData = new WxPayData();
                wxPayData.SetValue("appid", appId);
                wxPayData.SetValue("url", value);
                wxPayData.SetValue("timestamp", WxPayApi.GenerateTimeStamp());
                wxPayData.SetValue("noncestr", WxPayApi.GenerateNonceStr());
                wxPayData.SetValue("accesstoken", access_token);
                string    password   = wxPayData.ToUrl();
                string    text2      = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
                WxPayData wxPayData2 = new WxPayData();
                wxPayData2.SetValue("appId", appId);
                wxPayData2.SetValue("scope", "jsapi_address");
                wxPayData2.SetValue("signType", "sha1");
                wxPayData2.SetValue("addrSign", text2);
                wxPayData2.SetValue("timeStamp", wxPayData.GetValue("timestamp"));
                wxPayData2.SetValue("nonceStr", wxPayData.GetValue("noncestr"));
                text = wxPayData2.ToJson();
                JsApiPay.paramDict.Add("paramJson", text);
                if (appendLog)
                {
                    WxPayLog.AppendLog(JsApiPay.paramDict, text2, JsApiPay.page.Request.Url.ToString(), "获取收货地址js函数入口参数", LogType.GetOrEditAddress);
                }
            }
            catch (Exception ex)
            {
                WxPayLog.AppendLog(JsApiPay.paramDict, "", JsApiPay.page.Request.Url.ToString(), "获取收货地址js函数入口参数:" + ex.Message, LogType.GetOrEditAddress);
                throw new WxPayException(ex.ToString());
            }
            return(text);
        }
Пример #25
0
        }                                            //H5调起JS API参数
        protected void Page_Load(object sender, EventArgs e)
        {
            WxPayLog.Info(this.GetType().ToString(), "page load");
            if (!IsPostBack)
            {
                string openid    = Request.QueryString["openid"];
                string total_fee = Request.QueryString["total_fee"];
                //检测是否给当前页面传递了相关参数
                if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee))
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>");
                    WxPayLog.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit...");
                    submit.Visible = false;
                    return;
                }

                //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数
                var jsApiPay = new WxPayJsApi(this);
                jsApiPay.openid    = openid;
                jsApiPay.total_fee = int.Parse(total_fee);

                //JSAPI支付预处理
                try
                {
                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                    wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数
                    WxPayLog.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam);
                    //在页面上显示订单信息
                    Response.Write("<span style='color:#00CD00;font-size:20px'>订单详情:</span><br/>");
                    Response.Write("<span style='color:#00CD00;font-size:20px'>" + unifiedOrderResult.ToPrintStr() + "</span>");
                }
                catch (Exception ex)
                {
                    Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试" + "</span>");
                    submit.Visible = false;
                }
            }
        }
Пример #26
0
		public static WxPayData Refund(WxPayData inputObj, PayConfig config, int timeOut = 60)
		{
			string text = "https://api.mch.weixin.qq.com/secapi/pay/refund";
			if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "退款订单API接口中缺少必填参数out_trade_no 或者 transaction_id", LogType.Refund);
			}
			else if (!inputObj.IsSet("out_refund_no"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "退款订单API接口中缺少必填参数out_refund_no", LogType.Refund);
			}
			else if (!inputObj.IsSet("total_fee"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "退款订单API接口中缺少必填参数total_fee", LogType.Refund);
			}
			else if (!inputObj.IsSet("refund_fee"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "退款订单API接口中缺少必填参数refund_fee", LogType.Refund);
			}
			else if (!inputObj.IsSet("op_user_id"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "退款订单API接口中缺少必填参数op_user_id", LogType.Refund);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			DateTime now = DateTime.Now;
			string xml2 = HttpService.Post(xml, text, true, config, timeOut);
			DateTime now2 = DateTime.Now;
			int timeCost = (int)(now2 - now).TotalMilliseconds;
			WxPayData wxPayData = new WxPayData();
			wxPayData.FromXml(xml2, config.Key);
			WxPayApi.ReportCostTime(text, timeCost, wxPayData, config);
			return wxPayData;
		}
Пример #27
0
		public static WxPayData DownloadBill(WxPayData inputObj, PayConfig config, int timeOut = 6)
		{
			string url = "https://api.mch.weixin.qq.com/pay/downloadbill";
			if (!inputObj.IsSet("bill_date"))
			{
				WxPayLog.AppendLog(inputObj.GetParam(), "", HttpContext.Current.Request.Url.ToString(), "对账单接口中,缺少必填参数bill_date", LogType.DownLoadBill);
			}
			inputObj.SetValue("appid", config.AppId);
			inputObj.SetValue("mch_id", config.MchID);
			inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());
			inputObj.SetValue("sign", inputObj.MakeSign(config.Key));
			string xml = inputObj.ToXml();
			string text = HttpService.Post(xml, url, false, config, timeOut);
			WxPayData wxPayData = new WxPayData();
			if (text.Substring(0, 5) == "<xml>")
			{
				wxPayData.FromXml(text, config.Key);
			}
			else
			{
				wxPayData.SetValue("result", text);
			}
			return wxPayData;
		}
 public ActionResult RefundQuery()
 {
     WxPayLog.Debug(this.GetType().ToString(), "page load");
     return(View());
 }
 protected void Page_Load(object sender, EventArgs e)
 {
     WxPayLog.Debug(this.GetType().ToString(), "page load");
 }
 public ActionResult DownloadBill()
 {
     WxPayLog.Debug(this.GetType().ToString(), "DownloadBillPage load");
     return(View());
 }