public bool IsSignPassed(string notifySign) { ////var notifySign = this.webParams["sign"]; this.webParams.Remove("sign"); string unSignParaString = CommonUtil.FormatBizQueryParaMap(this.webParams, false); var computeSign = MD5SignUtil.Sign(unSignParaString, wxPayConfig.PartnerKey); return(computeSign.ToUpper().Equals(notifySign.ToUpper())); }
private void SetPackageSign() { var source = _orderQueryRequest.Package.GetSignSource(); source.Add("key", wxPayConfig.PartnerKey); string unSignParaString = CommonUtil.FormatBizQueryParaMap(source, false); var computeSign = MD5SignUtil.Sign(unSignParaString, wxPayConfig.PartnerKey); _orderQueryRequest.Package.Sign = computeSign.ToUpper(); }
protected void Page_Load(object sender, EventArgs e) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Request.InputStream); xmlDoc.Save(string.Format("C:\\WXPay\\NotifyCrowdFund{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmssfff")));//写入日志 //全部参数 Dictionary <string, string> parametersAll = new Dictionary <string, string>(); foreach (XmlElement item in xmlDoc.DocumentElement.ChildNodes) { string key = item.Name; string value = item.InnerText; if ((!string.IsNullOrEmpty(key)) && (!string.IsNullOrEmpty(value))) { parametersAll.Add(key, value); } } parametersAll = (from entry in parametersAll orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);//全部参数排序 //验签参数 不包括 sign 参数 Dictionary <string, string> parametersSign = (from entry in parametersAll where !entry.Key.Equals("sign") orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value); CrowdFundRecord record = bllPay.Get <CrowdFundRecord>(string.Format(" RecordID={0}", parametersAll["out_trade_no"])); var payConfig = bllPay.GetPayConfig(); string strSign = Payment.WeiXin.CommonUtil.FormatBizQueryParaMap(parametersSign, false); if (!MD5SignUtil.VerifySignature(strSign, parametersAll["sign"], payConfig.WXPartnerKey))//验证签名 { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); return; } if (record == null) { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); return; } if (record.Status.Equals(1)) { Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>"); return; } if (record.Status.Equals(0))//只有未付款状态 { ZentCloud.ZCBLLEngine.BLLTransaction tran = new ZCBLLEngine.BLLTransaction(); try { record.Status = 1; if (!bllPay.Update(record, tran)) { tran.Rollback(); Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); } tran.Commit(); Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>"); return; } catch { tran.Rollback(); Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); } } Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>"); } catch (Exception) { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); } }
protected void Page_Load(object sender, EventArgs e) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Request.InputStream); xmlDoc.Save(string.Format("C:\\WXPay\\Notify{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmssfff")));//写入日志 //全部参数 Dictionary <string, string> parametersAll = new Dictionary <string, string>(); foreach (XmlElement item in xmlDoc.DocumentElement.ChildNodes) { string key = item.Name; string value = item.InnerText; if ((!string.IsNullOrEmpty(key)) && (!string.IsNullOrEmpty(value))) { parametersAll.Add(key, value); } } parametersAll = (from entry in parametersAll orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);//全部参数排序 //验签参数 不包括 sign 参数 Dictionary <string, string> ParametersSign = (from entry in parametersAll where !entry.Key.Equals("sign") orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value); orderInfo = bllMall.GetOrderInfo(parametersAll["out_trade_no"]); WXMallPaymentType PayMentType = bllMall.GetPaymentType(int.Parse(orderInfo.PaymentTypeAutoId)); string strSign = Payment.WeiXin.CommonUtil.FormatBizQueryParaMap(ParametersSign, false); if (!MD5SignUtil.VerifySignature(strSign, parametersAll["sign"], PayMentType.WXPartnerKey))//验证签名 { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); return; } if (orderInfo == null) { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); return; } if (orderInfo.PaymentStatus.Equals(1)) { Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>"); return; } //更新订单状态 if (parametersAll["return_code"].Equals("SUCCESS") && parametersAll["result_code"].Equals("SUCCESS")) //交易成功 { orderInfo.PaymentStatus = 1; orderInfo.Status = "待发货"; if (bllMall.GetWebsiteInfoModelFromDataBase().IsDistributionMall.Equals(1)) { orderInfo.DistributionStatus = 1; } if (bllMall.Update(orderInfo)) { Response.Write("<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>"); return; } else { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); return; } } Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); } catch (Exception) { Response.Write("<xml><return_code><![CDATA[FAIL]]></return_code></xml>"); } }
protected void Page_Load(object sender, EventArgs e) { try { #region 检查订单是否可以支付 if (Request["oid"] == null) { Response.Write("订单无效"); Response.End(); } int OrderId; if (!int.TryParse(Request["oid"], out OrderId)) { Response.Write("订单无效"); Response.End(); } Order = bllOrder.GetOrderPay(OrderId.ToString()); if (Order == null) { Response.Write("订单无效"); Response.End(); } if (!Order.Status.Equals(0)) { Response.Write("订单不是未付款状态"); Response.End(); } #endregion #region 生成预支付ID BLLJIMP.Model.PayConfig PayConfig = bllPay.GetPayConfig(); var non_str = Payment.WeiXin.CommonUtil.CreateNoncestr();//随机串 // Dictionary <string, string> Dic = new Dictionary <string, string>(); Dic.Add("appid", PayConfig.WXAppId); Dic.Add("body", "订单号" + Order.OrderId); Dic.Add("mch_id", PayConfig.WXMCH_ID); Dic.Add("nonce_str", non_str); Dic.Add("out_trade_no", Order.OrderId); Dic.Add("openid", bllPay.GetCurrentUserInfo().WXOpenId); Dic.Add("spbill_create_ip", Request.UserHostAddress); Dic.Add("total_fee", (Order.Total_Fee * 100).ToString("F0")); Dic.Add("notify_url", string.Format("http://{0}/WxPayNotify/NotifyVote.aspx", Request.Url.Host)); Dic.Add("trade_type", "JSAPI"); string strtemp = Payment.WeiXin.CommonUtil.FormatBizQueryParaMap(Dic, false); string sign = MD5SignUtil.Sign(strtemp, PayConfig.WXPartnerKey); Dic = (from entry in Dic orderby entry.Key ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value); Dic.Add("sign", sign); string postdata = Payment.WeiXin.CommonUtil.ArrayToXml(Dic); string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(postdata); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = requestBytes.Length; Stream requestStream = req.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); HttpWebResponse res = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8); string backstr = sr.ReadToEnd(); sr.Close(); res.Close(); var result = XDocument.Parse(backstr); var return_code = result.Element("xml").Element("return_code").Value; //if (!return_code.ToUpper().Equals("SUCCESS")) //{ // Response.Write(backstr); // return; //} var rusult_code = result.Element("xml").Element("result_code").Value; string prepay_id = ""; if (return_code.ToUpper().Equals("SUCCESS") && (rusult_code.ToUpper().Equals("SUCCESS"))) { prepay_id = result.Element("xml").Element("prepay_id").Value; } #endregion #region 生成支付请求 WXPayReq reqwx = new WXPayReq(); string timeStamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(); reqwx.appId = PayConfig.WXAppId; reqwx.nonceStr = non_str; reqwx.package = "prepay_id=" + prepay_id; reqwx.signType = "MD5"; reqwx.timeStamp = timeStamp; Dictionary <string, string> DicNew = new Dictionary <string, string>(); DicNew.Add("appId", reqwx.appId); DicNew.Add("timeStamp", reqwx.timeStamp); DicNew.Add("nonceStr", reqwx.nonceStr); DicNew.Add("package", reqwx.package); DicNew.Add("signType", "MD5"); string strtemp1 = Payment.WeiXin.CommonUtil.FormatQueryParaMap(DicNew); string PaySign = MD5SignUtil.Sign(strtemp1, PayConfig.WXPartnerKey); reqwx.paySign = PaySign; WxPayReq = ZentCloud.Common.JSONHelper.ObjectToJson(reqwx); #endregion } catch (Exception ex) { Response.Write(ex.Message); Response.End(); } }