using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Specialized; using System.Collections.Generic; using System.Globalization; namespace Mtxfw.shop { /// /// 功能:服务器异步通知页面 /// 版本:3.3 /// 日期:2012-07-10 /// 说明: /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 /// 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 /// /// ///////////////////页面功能说明/////////////////// /// 创建该页面文件时,请留心该页面文件中无任何HTML代码及空格。 /// 该页面不能在本机电脑测试,请到服务器上做测试。请确保外部可以访问该页面。 /// 该页面调试工具请使用写文本函数logResult。 /// 如果没有收到该页面返回的 success 信息,支付宝会在24小时内按一定的时间策略重发通知 /// public partial class notify_url : System.Web.UI.Page { Mtxfw.DAL.user_info daoUser = new Mtxfw.DAL.user_info(); Mtxfw.DAL.order_info daoorder = new Mtxfw.DAL.order_info(); Mtxfw.DAL.user_Results_jl2 daoResults2 = new Mtxfw.DAL.user_Results_jl2(); Mtxfw.DAL.order_product_info daoorderproduct = new Mtxfw.DAL.order_product_info(); protected void Page_Load(object sender, EventArgs e) { SortedDictionary sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { Mtxfw.Utility.Notify aliNotify = new Mtxfw.Utility.Notify(); bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]); if (verifyResult)//验证成功 { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //商户订单号 string out_trade_no = Request.Form["out_trade_no"]; //支付宝交易号 string trade_no = Request.Form["trade_no"]; //交易状态 string trade_status = Request.Form["trade_status"]; if (Request.Form["trade_status"] == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 Mtxfw.Model.user_Results_jl2 ReModel = daoResults2.GetModel(out_trade_no); if (ReModel != null) { if (ReModel.Seef == 0) { daoResults2.UpdateSeef("seef", 1, "seeftime", DateTime.Now, ReModel.Id); //增加买家余额 daoUser.UpdateMoney("umoney12", ReModel.Addmoney, ReModel.MemberId); Mtxfw.Model.user_info uModel = new DAL.user_info().GetModel(ReModel.MemberId); if (uModel != null) { if (ReModel.OrdersIDs != "") { Double ZSubTotal = 0.00; string[] AOrderIDs = ReModel.OrdersIDs.Split(','); for (int i = 0; i < AOrderIDs.Length; i++) { Mtxfw.Model.order_info OrderModel = daoorder.GetModel(int.Parse(AOrderIDs[i])); if (OrderModel != null) { if (OrderModel.O_Payed == "" || OrderModel.O_Payed == "0") { ZSubTotal += OrderModel.Totalprice; } } } Double umoney12 = Convert.ToDouble((uModel.umoney12 - uModel.umoney22).ToString("f2")); ZSubTotal = Convert.ToDouble(ZSubTotal.ToString("f2")); if (ZSubTotal > 0.00 && ZSubTotal <= umoney12) { for (int i = 0; i < AOrderIDs.Length; i++) { Mtxfw.Model.order_info OrderModel = daoorder.GetModel(int.Parse(AOrderIDs[i])); if (OrderModel != null) { if (OrderModel.O_Payed == "" || OrderModel.O_Payed == "0") { daoorder.UpdateqyState(0, "1", DateTime.Now, OrderModel.Id); Mtxfw.DAL.MemberStatistics.JiangLiFP(OrderModel.Id, OrderModel.orderid, OrderModel.pid.ToString(), OrderModel.UserID, OrderModel.Totalprice, OrderModel.Totalprice0, OrderModel.Totalprice1, OrderModel.Totalprice2, OrderModel.utype.ToString()); System.Data.DataSet Ds_op = new Mtxfw.DAL.order_product_info().GetList("", "utype='0' And orderid=" + OrderModel.Id + " Order By SubmitTime asc"); if (Ds_op.Tables[0].Rows.Count > 0) { for (int l = 0; l < Ds_op.Tables[0].Rows.Count; l++) { int P_pid = int.Parse(Ds_op.Tables[0].Rows[l]["pid"].ToString()); int P_Totalnum = int.Parse(Ds_op.Tables[0].Rows[l]["Totalnum"].ToString()); new Mtxfw.DAL.P_Product().UpdateInventory(P_pid, -P_Totalnum); } } Ds_op.Clear(); } } } Double money = ZSubTotal; Model.user_Results_jl2 mjl2 = new Model.user_Results_jl2(); mjl2.MemberId = ReModel.MemberId; mjl2.MemberId2 = 0; mjl2.Addmoney = 0; mjl2.Cutmoney = -money; mjl2.Totalmoney = umoney12 - money; mjl2.addtime = DateTime.Now; mjl2.BZContent = "购买商品付款"; mjl2.PayID = ""; mjl2.OrdersIDs = ""; mjl2.OrdersID = 0; mjl2.utype = 2; mjl2.utype2 = 0; mjl2.Seef = 1; mjl2.seeftime = DateTime.Now; new DAL.user_Results_jl2().Add(mjl2); daoUser.UpdateMoney("umoney22", money, ReModel.MemberId); /*try { if (System.Text.RegularExpressions.Regex.IsMatch(uModel.Phone, @"^[1]+[3,4,5,6,7,8]+\d{9}")) { string strContent = ""; strContent += "尊敬的用户(" + uModel.RealName + "),您好!您的订单已经支付成功,请登录网站查看详情!【荞宝商城】"; Mtxfw.Utility.Common.sendsms(uModel.Phone, strContent); strContent = ""; strContent += "用户(" + uModel.RealName + ")在(" + ReModel.addtime.ToString("yyyy年MM月dd日hh时mm分", DateTimeFormatInfo.InvariantInfo) + ")通过微信支付(" + money.ToString("f0") + ")元,请登录网站后台查看详情!【荞宝商城】"; Mtxfw.Utility.Common.sendsms(uModel.Phone, strContent); } } catch (Exception err) { Mtxfw.Utility.Common.WriteHtml("/weixin/weixinsentsms.txt", err.Message + err.ToString()); }*/ } else { Mtxfw.Utility.Common.WriteHtml("/weixin/payerr.txt", out_trade_no); } } } } } } else if (Request.Form["trade_status"] == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 Mtxfw.Model.user_Results_jl2 ReModel = daoResults2.GetModel(out_trade_no); if (ReModel != null) { if (ReModel.Seef == 0) { daoResults2.UpdateSeef("seef", 1, "seeftime", DateTime.Now, ReModel.Id); //增加买家余额 daoUser.UpdateMoney("umoney12", ReModel.Addmoney, ReModel.MemberId); Mtxfw.Model.user_info uModel = new DAL.user_info().GetModel(ReModel.MemberId); if (uModel != null) { if (ReModel.OrdersIDs != "") { Double ZSubTotal = 0.00; string[] AOrderIDs = ReModel.OrdersIDs.Split(','); for (int i = 0; i < AOrderIDs.Length; i++) { Mtxfw.Model.order_info OrderModel = daoorder.GetModel(int.Parse(AOrderIDs[i])); if (OrderModel != null) { if (OrderModel.O_Payed == "" || OrderModel.O_Payed == "0") { ZSubTotal += OrderModel.Totalprice; } } } Double umoney12 = Convert.ToDouble((uModel.umoney12 - uModel.umoney22).ToString("f2")); ZSubTotal = Convert.ToDouble(ZSubTotal.ToString("f2")); if (ZSubTotal > 0.00 && ZSubTotal <= umoney12) { for (int i = 0; i < AOrderIDs.Length; i++) { Mtxfw.Model.order_info OrderModel = daoorder.GetModel(int.Parse(AOrderIDs[i])); if (OrderModel != null) { if (OrderModel.O_Payed == "" || OrderModel.O_Payed == "0") { daoorder.UpdateqyState(0, "1", DateTime.Now, OrderModel.Id); Mtxfw.DAL.MemberStatistics.JiangLiFP(OrderModel.Id, OrderModel.orderid, OrderModel.pid.ToString(), OrderModel.UserID, OrderModel.Totalprice, OrderModel.Totalprice0, OrderModel.Totalprice1, OrderModel.Totalprice2, OrderModel.utype.ToString()); System.Data.DataSet Ds_op = new Mtxfw.DAL.order_product_info().GetList("", "utype='0' And orderid=" + OrderModel.Id + " Order By SubmitTime asc"); if (Ds_op.Tables[0].Rows.Count > 0) { for (int l = 0; l < Ds_op.Tables[0].Rows.Count; l++) { int P_pid = int.Parse(Ds_op.Tables[0].Rows[l]["pid"].ToString()); int P_Totalnum = int.Parse(Ds_op.Tables[0].Rows[l]["Totalnum"].ToString()); new Mtxfw.DAL.P_Product().UpdateInventory(P_pid, -P_Totalnum); } } Ds_op.Clear(); } } } Double money = ZSubTotal; Model.user_Results_jl2 mjl2 = new Model.user_Results_jl2(); mjl2.MemberId = ReModel.MemberId; mjl2.MemberId2 = 0; mjl2.Addmoney = 0; mjl2.Cutmoney = -money; mjl2.Totalmoney = umoney12 - money; mjl2.addtime = DateTime.Now; mjl2.BZContent = "购买商品付款"; mjl2.PayID = ""; mjl2.OrdersIDs = ""; mjl2.OrdersID = 0; mjl2.utype = 2; mjl2.utype2 = 0; mjl2.Seef = 1; mjl2.seeftime = DateTime.Now; new DAL.user_Results_jl2().Add(mjl2); daoUser.UpdateMoney("umoney22", money, ReModel.MemberId); /*try { if (System.Text.RegularExpressions.Regex.IsMatch(uModel.Phone, @"^[1]+[3,4,5,6,7,8]+\d{9}")) { string strContent = ""; strContent += "尊敬的用户(" + uModel.RealName + "),您好!您的订单已经支付成功,请登录网站查看详情!【荞宝商城】"; Mtxfw.Utility.Common.sendsms(uModel.Phone, strContent); strContent = ""; strContent += "用户(" + uModel.RealName + ")在(" + ReModel.addtime.ToString("yyyy年MM月dd日hh时mm分", DateTimeFormatInfo.InvariantInfo) + ")通过微信支付(" + money.ToString("f0") + ")元,请登录网站后台查看详情!【荞宝商城】"; Mtxfw.Utility.Common.sendsms(uModel.Phone, strContent); } } catch (Exception err) { Mtxfw.Utility.Common.WriteHtml("/weixin/weixinsentsms.txt", err.Message + err.ToString()); }*/ } else { Mtxfw.Utility.Common.WriteHtml("/weixin/payerr.txt", out_trade_no); } } } } } } else { } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— Response.Write("success"); //请不要修改或删除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { Response.Write("fail"); } } else { Response.Write("无通知参数"); } } /// /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组 /// /// request回来的信息组成的数组 public SortedDictionary GetRequestPost() { int i = 0; SortedDictionary sArray = new SortedDictionary(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return sArray; } } }