using Itlead.Architecture.Contract; using Itlead.Architecture.Contract.DataContract; using Itlead.Architecture.Entity.Generated; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.SessionState; using System.Xml; using WxPayAPI; namespace Itlead.Architecture.WebUI.tools { /// /// WxPay_Notify_Url 的摘要说明 /// public class WxPay_Notify_Url : IHttpHandler, IRequiresSessionState { const string alipayService = "alipayOrderService"; public void ProcessRequest(HttpContext context) { //XmlDocument doc = new XmlDocument(); //doc.XmlResolver = null; Log.Info("支付返回", "执行了这里:回调页面"); context.Response.ContentType = "text/plain"; WxPayData notifyData = GetNotifyData(); Log.Info("支付返回", "执行了这里:获取回调参数"); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { Log.Info("支付返回", "执行了这里:订单不存在"); //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml()); HttpContext.Current.Response.Write(res.ToXml()); HttpContext.Current.Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); //查询订单,判断订单真实性 if (!QueryOrder(transaction_id)) { Log.Info("支付返回", "执行了这里:订单查询失败"); //若订单查询失败,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml()); HttpContext.Current.Response.Write(res.ToXml()); HttpContext.Current.Response.End(); } //查询订单成功 else { WxPayData res = new WxPayData(); var aid = notifyData.GetValue("out_trade_no"); Log.Info("支付返回", "执行了这里:成功调用业务" + aid.ToString()); AlipayOrderData order = MyBasicLib.Utilities.ServiceInvoker.Invoke(alipayService, p => p.GetByWxOut_Order_Id(aid.ToString())); order.Status = 1; order.WxPayOederId = aid.ToString(); order.UpdateTime = DateTime.Now; order.StartTime = DateTime.Now; order.EedTime = DateTime.Now.AddYears(1); order.IsCourse = true; order.OpenClass = false; if (order.IsPrototype == true) { var Prorder = new PrototypeOrder() { ID = Guid.NewGuid().ToString(), PprototypedID = order.CourseID.ToString(), POrderID = order.ID.ToString(), PuserID = order.UserID.ToString(), CreateTime = DateTime.Now, IsDel = 0, IsPurchase = true, IsState = 0, PrDesc = null }; MyBasicLib.Utilities.ServiceInvoker.Invoke("PrototypeService", c => c.AddPrototypeOrder(Prorder)); } var s = MyBasicLib.Utilities.ServiceInvoker.Invoke(alipayService, p => p.Update(order)); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); Log.Info(this.GetType().ToString(), "order query success : " + res.ToXml()); Log.Info("支付放回", aid.ToString() + ":" + order.AlipayID.ToString() + s.ToString()); HttpContext.Current.Response.Write(res.ToXml()); if (context.Session["ConfirmPhone"] == null || (bool)context.Session["ConfirmPhone"] == false) { context.Response.Write(MsgResult("验证成功
请绑定手机", "/Account/MemberCenter/" + order.ID.ToString() + "?openConfirmPhone=1")); } else { context.Response.Write(MsgResult("支付成功", "/Account/MemberCenter/" + order.ID.ToString())); } HttpContext.Current.Response.End(); } //context.Response.Write("Hello World"); } public string MsgResult(string msg, string backUrl) { return ""; } //查询订单 private bool QueryOrder(string transaction_id) { WxPayData req = new WxPayData(); req.SetValue("transaction_id", transaction_id); WxPayData res = WxPayApi.OrderQuery(req); if (res.GetValue("return_code").ToString() == "SUCCESS" && res.GetValue("result_code").ToString() == "SUCCESS") { return true; } else { return false; } } /// /// 接收从微信支付后台发送过来的数据并验证签名 /// /// 微信支付后台返回的数据 public WxPayData GetNotifyData() { //接收从微信后台POST过来的数据 System.IO.Stream s = HttpContext.Current.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); Log.Info(this.GetType().ToString(), "Receive data from WeChat : " + builder.ToString()); //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { data.FromXml(builder.ToString()); } catch (WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); Log.Error(this.GetType().ToString(), "Sign check error : " + res.ToXml()); HttpContext.Current.Response.Write(res.ToXml()); HttpContext.Current.Response.End(); } Log.Info(this.GetType().ToString(), "Check sign success"); return data; } public bool IsReusable { get { return false; } } } }