protected void Page_Load(object sender, EventArgs e) { try { BLLJIMP.BLLMall bllMall = new BLLJIMP.BLLMall(); BLLJIMP.BllPay bllPay = new BLLJIMP.BllPay(); Dictionary <string, string> dicAll = bllPay.GetRequestParameter(); //商户订单号 //string out_trade_no = Request.QueryString["out_trade_no"]; //支付宝交易号 // string trade_no = Request.QueryString["trade_no"]; //交易状态 // string result = Request.QueryString["result"]; var order = bllMall.GetOrderInfo(dicAll["out_trade_no"]); WXMallPaymentType paymentType = bllMall.GetPaymentType(int.Parse(order.PaymentTypeAutoId)); if (dicAll.Count > 0)//判断是否有带返回参数 { //Notify aliNotify = new Notify(); //bool verifyResult = aliNotify.VerifyReturnMall(DicAll, Request.QueryString["sign"], PaymentType.AlipayPartnerKey); if (bllPay.VerifySignatureAlipay(dicAll, paymentType.AlipayPartner, paymentType.AlipayPartnerKey))//验证成功 { if (order.PaymentStatus.Equals(1)) { payresult = "true"; } else { order.PaymentStatus = 1; order.Status = "待发货"; if (bllMall.GetWebsiteInfoModelFromDataBase().IsDistributionMall.Equals(1)) { order.DistributionStatus = 1; } if (bllMall.Update(order)) { payresult = "true"; } else { msg.InnerHtml = "更新支付状态失败!</br>等待异步通知更新.."; } } } else//验证失败 { msg.InnerHtml = "验证失败"; } } else { msg.InnerHtml = "无返回参数"; } } catch (Exception ex) { msg.InnerHtml = ex.Message; } }
protected void Page_Load(object sender, EventArgs e) { webAction = Request["Action"]; if (webAction == "edit") { autoid = Convert.ToInt32(Request["id"]); model = bll.GetPaymentType(autoid); if (model == null) { Response.End(); } else { HeadTitle = string.Format("{0}", model.PaymentTypeName); } } }
protected void Page_Load(object sender, EventArgs e) { try { BLLJIMP.BLLMall bllMall = new BLLJIMP.BLLMall(); Dictionary <string, string> sPara = GetRequestPost(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sPara["notify_data"]); xmlDoc.Save(string.Format("C:\\Alipay\\mallnotify{0}.xml", DateTime.Now.ToString("yyyyMMddHHmmssfff"))); //商户订单号 string outTradeNo = xmlDoc.SelectSingleNode("/notify/out_trade_no").InnerText; //支付宝交易号 string trade_no = xmlDoc.SelectSingleNode("/notify/trade_no").InnerText; //交易状态 string tradeStatus = xmlDoc.SelectSingleNode("/notify/trade_status").InnerText; var order = bllMall.GetOrderInfo(outTradeNo); WXMallPaymentType paymentType = bllMall.GetPaymentType(int.Parse(order.PaymentTypeAutoId)); if (sPara.Count > 0)//判断是否有带返回参数 { Notify aliNotify = new Notify(); bool verifyResult = aliNotify.VerifyNotifyMall(sPara, Request.Form["sign"], paymentType.AlipayPartner, paymentType.AlipayPartnerKey); if (verifyResult)//验证成功 { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //解密(如果是RSA签名需要解密,如果是MD5签名则下面一行清注释掉) //sPara = aliNotify.Decrypt(sPara); //XML解析notify_data数据 if (tradeStatus == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 if (order.PaymentStatus.Equals(0))//只有未付款状态 { order.PaymentStatus = 1; order.Status = "待发货"; if (bllMall.GetWebsiteInfoModelFromDataBase().IsDistributionMall.Equals(1)) { order.DistributionStatus = 1; } if (bllMall.Update(order)) { Response.Write("success"); } else { Response.Write("fail"); } } else { Response.Write("success"); //请不要修改或删除 } } else if (tradeStatus == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 if (order.PaymentStatus.Equals(0))//只有未付款状态 { order.PaymentStatus = 1; order.Status = "待发货"; if (bllMall.GetWebsiteInfoModelFromDataBase().IsDistributionMall.Equals(1)) { order.DistributionStatus = 1; } if (bllMall.Update(order)) { Response.Write("success"); } else { Response.Write("fail"); } } else { Response.Write("success"); //请不要修改或删除 } } else { Response.Write(tradeStatus); } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { Response.Write("fail"); } } else { Response.Write("无通知参数"); } } catch (Exception) { Response.Write("fail"); } }
protected void Page_Load(object sender, EventArgs e) { try { if (Request["oid"] == null) { Response.Write("订单无效"); Response.End(); } int OrderId; if (!int.TryParse(Request["oid"], out OrderId)) { Response.Write("订单无效"); Response.End(); } Order = bllMall.GetOrderInfo(Request["oid"]); if (Order == null) { Response.Write("订单无效"); Response.End(); } if (!Order.PaymentStatus.Equals(0)) { Response.Write("订单不是未付款状态"); Response.End(); } if (!Order.PaymentType.Equals(1)) { Response.Write("订单不属于支付宝支付"); Response.End(); } if (string.IsNullOrEmpty(Order.PaymentTypeAutoId)) { Response.Write("无效支付方式"); Response.End(); } WXMallPaymentType paymentType = bllMall.GetPaymentType(int.Parse(Order.PaymentTypeAutoId)); if (paymentType == null) { Response.Write("无效支付方式"); Response.End(); } //支付宝网关地址 string GATEWAY_NEW = "http://wappaygw.alipay.com/service/rest.htm?"; ////////////////////////////////////////////调用授权接口alipay.wap.trade.create.direct获取授权码token//////////////////////////////////////////// //返回格式 string format = "xml"; //必填,不需要修改 //返回格式 string v = "2.0"; //必填,不需要修改 //请求号 string req_id = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(0, 99999); //必填,须保证每次请求都是唯一 //req_data详细信息 //服务器异步通知页面路径 string notify_url = string.Format("http://{0}/alipay/mallnotifyurl.aspx", Request.Url.Host); //需http://格式的完整路径,不允许加?id=123这类自定义参数 //页面跳转同步通知页面路径 string call_back_url = string.Format("http://{0}/alipay/mallcall_back_url.aspx", Request.Url.Host); //需http://格式的完整路径,不允许加?id=123这类自定义参数 //操作中断返回地址 string merchant_url = string.Format("http://{0}/alipay/merchanturl.aspx", Request.Url.Host); //用户付款中途退出返回商户的地址。需http://格式的完整路径,不允许加?id=123这类自定义参数 //卖家支付宝帐户 //string seller_email = "*****@*****.**"; string seller_email = paymentType.AlipaySeller_Account_Name; //必填 //商户订单号 string out_trade_no = Order.OrderID; //商户网站订单系统中唯一订单号,必填 //订单名称 string subject = string.Format("订单{0}", Order.OrderID); //必填 //付款金额 string total_fee = Order.TotalAmount.ToString(); //必填 //请求业务参数详细 string req_dataToken = "<direct_trade_create_req><notify_url>" + notify_url + "</notify_url><call_back_url>" + call_back_url + "</call_back_url><seller_account_name>" + seller_email + "</seller_account_name><out_trade_no>" + out_trade_no + "</out_trade_no><subject>" + subject + "</subject><total_fee>" + total_fee + "</total_fee><merchant_url>" + merchant_url + "</merchant_url></direct_trade_create_req>"; //必填 //把请求参数打包成数组 Dictionary <string, string> sParaTempToken = new Dictionary <string, string>(); sParaTempToken.Add("partner", paymentType.AlipayPartner); sParaTempToken.Add("_input_charset", Config.Input_charset.ToLower()); sParaTempToken.Add("sec_id", Config.Sign_type.ToUpper()); sParaTempToken.Add("service", "alipay.wap.trade.create.direct"); sParaTempToken.Add("format", format); sParaTempToken.Add("v", v); sParaTempToken.Add("req_id", req_id); sParaTempToken.Add("req_data", req_dataToken); //建立请求 string sHtmlTextToken = Submit.BuildRequestMall(GATEWAY_NEW, sParaTempToken, paymentType.AlipayPartnerKey); //URLDECODE返回的信息 Encoding code = Encoding.GetEncoding("utf-8"); sHtmlTextToken = HttpUtility.UrlDecode(sHtmlTextToken, code); //using (System.IO.StreamWriter sr = new StreamWriter("D:\\MonitorHandlerException.txt")) //{ // sr.Write(sHtmlTextToken); //} //解析远程模拟提交后返回的信息 Dictionary <string, string> dicHtmlTextToken = Submit.ParseResponse(sHtmlTextToken); //获取token string request_token = dicHtmlTextToken["request_token"]; ////////////////////////////////////////////根据授权码token调用交易接口alipay.wap.auth.authAndExecute//////////////////////////////////////////// //业务详细 string req_data = "<auth_and_execute_req><request_token>" + request_token + "</request_token></auth_and_execute_req>"; //必填 //把请求参数打包成数组 Dictionary <string, string> sParaTemp = new Dictionary <string, string>(); sParaTemp.Add("partner", paymentType.AlipayPartner); sParaTemp.Add("_input_charset", Config.Input_charset.ToLower()); sParaTemp.Add("sec_id", Config.Sign_type.ToUpper()); sParaTemp.Add("service", "alipay.wap.auth.authAndExecute"); sParaTemp.Add("format", format); sParaTemp.Add("v", v); sParaTemp.Add("req_data", req_data); //建立请求 string sHtmlText = Submit.BuildRequestMall(GATEWAY_NEW, sParaTemp, "get", "确认", paymentType.AlipayPartnerKey); Response.Write(sHtmlText); } catch (Exception ex) { Response.Write(ex.Message); Response.End(); } }
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(); } model = bllMall.GetOrderInfo(OrderId.ToString()); if (model == null) { Response.Write("订单无效"); Response.End(); } if (!model.PaymentStatus.Equals(0)) { Response.Write("订单不是未付款状态"); Response.End(); } if (!model.PaymentType.Equals(2)) { Response.Write("订单不属于微信支付"); Response.End(); } if (string.IsNullOrEmpty(model.PaymentTypeAutoId)) { Response.Write("无效支付方式"); Response.End(); } WXMallPaymentType paymentType = bllMall.GetPaymentType(int.Parse(model.PaymentTypeAutoId)); if (paymentType == null) { Response.Write("无效支付方式"); Response.End(); } #endregion var non_str = Payment.WeiXin.CommonUtil.CreateNoncestr();//随机串 #region 获取微信支付预支付ID if (string.IsNullOrEmpty(model.WXPrepay_Id))//还没有预支付ID { // Dictionary <string, string> Dic = new Dictionary <string, string>(); Dic.Add("appid", paymentType.WXAppId); Dic.Add("body", "订单号" + model.OrderID); Dic.Add("mch_id", paymentType.WXPartner); Dic.Add("nonce_str", non_str); Dic.Add("out_trade_no", model.OrderID); Dic.Add("openid", bllMall.GetCurrentUserInfo().WXOpenId); Dic.Add("spbill_create_ip", Request.UserHostAddress); Dic.Add("total_fee", (model.TotalAmount * 100).ToString("F0")); Dic.Add("notify_url", string.Format("http://{0}/WxPayNotify/Notify.aspx", Request.Url.Host)); Dic.Add("trade_type", "JSAPI"); string strtemp = Payment.WeiXin.CommonUtil.FormatBizQueryParaMap(Dic, false); string sign = MD5SignUtil.Sign(strtemp, paymentType.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; if (return_code.ToUpper().Equals("SUCCESS") && (rusult_code.ToUpper().Equals("SUCCESS"))) { var prepay_id = result.Element("xml").Element("prepay_id").Value; model.WXPrepay_Id = prepay_id; bllMall.Update(model);//更新订单 微信预支付ID } // } #endregion #region 生成微信支付请求 WXPayReq reqwx = new WXPayReq(); string timeStamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(); reqwx.appId = paymentType.WXAppId; reqwx.nonceStr = non_str; reqwx.package = "prepay_id=" + model.WXPrepay_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, paymentType.WXPartnerKey); reqwx.paySign = PaySign; WxPayReq = ZentCloud.Common.JSONHelper.ObjectToJson(reqwx); #endregion } catch (Exception ex) { Response.Write(ex.Message); Response.End(); } }