protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (!IsPostBack) { Dictionary <string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { bool verifyResult = false; AlipayNotify aliNotify = new AlipayNotify(); verifyResult = aliNotify.VerifyNotify(sPara, Request.Form["sign"]); if (verifyResult)//验证成功 { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //解密(如果是RSA签名需要解密,如果是MD5签名则下面一行清注释掉) //sPara = aliNotify.Decrypt(sPara); //XML解析notify_data数据 try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sPara["notify_data"]); //交易状态 string trade_status = xmlDoc.SelectSingleNode("/notify/trade_status").InnerText; AliWapPayNotifyInfo info = new AliWapPayNotifyInfo(); info.trade_status = trade_status; info.subject = xmlDoc.SelectSingleNode("/notify/subject").InnerText; info.trade_no = xmlDoc.SelectSingleNode("/notify/trade_no").InnerText; info.buyer_email = xmlDoc.SelectSingleNode("/notify/buyer_email").InnerText; info.gmt_create = xmlDoc.SelectSingleNode("/notify/gmt_create").InnerText; info.quantity = xmlDoc.SelectSingleNode("/notify/quantity").InnerText; info.out_trade_no = xmlDoc.SelectSingleNode("/notify/out_trade_no").InnerText; info.notify_time = xmlDoc.SelectSingleNode("/notify/notify_time").InnerText; info.seller_id = xmlDoc.SelectSingleNode("/notify/seller_id").InnerText; info.total_fee = xmlDoc.SelectSingleNode("/notify/total_fee").InnerText; info.seller_email = xmlDoc.SelectSingleNode("/notify/seller_email").InnerText; info.price = xmlDoc.SelectSingleNode("/notify/price").InnerText; //info.gmt_payment = xmlDoc.SelectSingleNode("/notify/gmt_payment").InnerText; info.buyer_id = xmlDoc.SelectSingleNode("/notify/buyer_id").InnerText; info.notify_id = xmlDoc.SelectSingleNode("/notify/notify_id").InnerText; info.use_coupon = xmlDoc.SelectSingleNode("/notify/use_coupon").InnerText; if (xmlDoc.SelectSingleNode("/notify/gmt_payment") != null) { info.gmt_payment = xmlDoc.SelectSingleNode("/notify/gmt_payment").InnerText; } if (trade_status == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 //微商易业务逻辑 OnPaySucceed(info); Response.Write("success"); //请不要修改或删除 } else if (trade_status == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 //微商易业务逻辑 OnPaySucceed(info); Response.Write("success"); //请不要修改或删除 } else { Response.Write(trade_status); } } catch (Exception exc) { Response.Write(exc.ToString()); } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { Response.Write("fail"); } } else { Response.Write("无通知参数"); } } }
protected void Page_Load(object sender, EventArgs e) { BaseService.WriteLog("服务器异步通知页面-----------------------AlipayWapTrade2/Notify.aspx"); Dictionary <string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { BaseService.WriteLog("开始验证"); AlipayNotify aliNotify = new AlipayNotify(); bool verifyResult = aliNotify.VerifyNotify(sPara, Request.Form["sign"]); if (verifyResult)//验证成功 { BaseService.WriteLog("验证成功"); ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //解密(如果是RSA签名需要解密,如果是MD5签名则下面一行清注释掉) sPara = aliNotify.Decrypt(sPara); //XML解析notify_data数据 try { BaseService.WriteLog("notify_data:" + sPara["notify_data"]); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(sPara["notify_data"]); //商户订单号 string out_trade_no = xmlDoc.SelectSingleNode("/notify/out_trade_no").InnerText; //支付宝交易号 string trade_no = xmlDoc.SelectSingleNode("/notify/trade_no").InnerText; //交易状态 string trade_status = xmlDoc.SelectSingleNode("/notify/trade_status").InnerText; //交易成功,更新支付宝交易状态 UpdateAlipayWapTrade(xmlDoc, "2"); BaseService.WriteLog("交易状态trade_status:" + trade_status); if (trade_status == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 if (!string.IsNullOrEmpty(out_trade_no)) { try { var alipayServer = new AlipayWapTradeResponseBLL(new Utility.BasicUserInfo()); BaseService.WriteLog("out_trade_no:" + out_trade_no); BaseService.WriteLog("交易成功"); //更新交易状态 BaseService.WriteLog("更新交易状态"); alipayServer.UpdateAlipayWapTradeStatus(out_trade_no, "2"); //处理分润业务 BaseService.WriteLog("处理分润业务"); //RoyaltyBusiness(out_trade_no, trade_no); //推送交易结果集 BaseService.WriteLog("推送交易结果集"); PostResult(alipayServer, "success", out_trade_no); } catch (Exception ex) { BaseService.WriteLog("异常信息: " + ex.ToString()); } } else { BaseService.WriteLog("out_trade_no is null!!!!! "); } //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 Response.Write("success"); //请不要修改或删除 } else if (trade_status == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 if (!string.IsNullOrEmpty(out_trade_no)) { try { var alipayServer = new AlipayWapTradeResponseBLL(new Utility.BasicUserInfo()); BaseService.WriteLog("out_trade_no:" + out_trade_no); BaseService.WriteLog("交易成功"); //更新交易状态 BaseService.WriteLog("更新交易状态"); alipayServer.UpdateAlipayWapTradeStatus(out_trade_no, "2"); //处理分润业务 BaseService.WriteLog("处理分润业务"); RoyaltyBusiness(out_trade_no, trade_no); //推送交易结果集 BaseService.WriteLog("推送交易结果集"); PostResult(alipayServer, "success", out_trade_no); } catch (Exception ex) { BaseService.WriteLog("异常信息: " + ex.ToString()); } } else { BaseService.WriteLog("out_trade_no is null!!!!! "); } //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 Response.Write("success"); //请不要修改或删除 } else { Response.Write(trade_status); } } catch (Exception exc) { BaseService.WriteLog("异常信息:" + exc.ToString()); Response.Write(exc.ToString()); } } else//验证失败 { BaseService.WriteLog("验证失败"); Response.Write("fail"); } } else { BaseService.WriteLog("无通知参数"); Response.Write("无通知参数"); } }