using Comnom.Alipay; using Itlead.Architecture.Contract; using Itlead.Architecture.Contract.DataContract; using System; using System.Collections.Generic; using System.Web; using System.Web.SessionState; namespace Itlead.Architecture.WebAppUI.tools { /// /// AlipayPayment 的摘要说明 /// public class AlipayPayment : IHttpHandler, IRequiresSessionState { const string courseService = "courseService"; const string alipayService = "alipayOrderService"; //服务器异步通知页面路径 static string notify_url = "http://m.pexue.com/tools/notify_url.ashx"; //需http://格式的完整路径,不能加?id=123这类自定义参数 //页面跳转同步通知页面路径 static string return_url = "http://m.pexue.com/tools/return_url.ashx"; //需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/ //支付类型 -必填 -不能修改 static string payment_type = "1"; //客户端的IP地址 //非局域网的外网IP地址,如:221.0.0.1 string exter_invoke_ip = "211.149.236.224"; public void ProcessRequest(HttpContext context) { if (context.Request.Cookies["user"] == null) { return; } //if (context.Session["confirm_phone"] == null || (bool)context.Session["confirm_phone"] == false) //{ // context.Response.Write(""); // return; //} context.Response.ContentType = "text/HTML"; string rId = context.Request["cid"]; bool isspecial = false; bool fg = false; if (context.Request["isspecial"] != null) { if (context.Request["isspecial"] == "true") { isspecial = true; } } if (context.Request["fg"] != null) { fg = true; } Guid courseId;//课程ID Guid uid; //用户ID if (Guid.TryParse(rId, out courseId) && Guid.TryParse(context.Request.Cookies["user"]["UserId"], out uid)) { if (!fg) { var tag = MyBasicLib.Utilities.ServiceInvoker.Invoke("alipayOrderService", s => s.Isexit(courseId, uid)); if (tag == 1) { context.Response.Write(""); //context.Response.Redirect("/PurchasingLessons/Index"); return; } if (tag == 2) { context.Response.Write(""); //context.Response.Redirect("/PurchasingLessons/Index"); return; } } AlipayOrderData order = null; //当前课程 string subject = ""; string total_fee = ""; string body = ""; if (isspecial) { SpecialCourseData specialdata = MyBasicLib.Utilities.ServiceInvoker.Invoke("specialCourseService", s => s.GetCourse(courseId)); order = new AlipayOrderData() { ID = Guid.NewGuid(), CourseID = specialdata.SpecialCourseId, CourseName = specialdata.SpecialCourseName, CreateTime = DateTime.Now, UpdateTime = DateTime.Now, StartTime = DateTime.Now, EedTime = DateTime.Now.AddYears(1), Status = 0, UserID = uid, Price = decimal.Parse(specialdata.price), isSpecial = isspecial, IsCourse = true, OpenClass = false }; subject = specialdata.SpecialCourseName; //付款金额 -必填 total_fee = specialdata.price; //订单描述 body = specialdata.SpecialCourseName; } else { CourseData coursemodel = MyBasicLib.Utilities.ServiceInvoker.Invoke(courseService, p => p.GetCourse(courseId)); order = new AlipayOrderData() { ID = Guid.NewGuid(), CourseID = coursemodel.CourseId, CourseName = coursemodel.CourseName, CreateTime = DateTime.Now, UpdateTime = DateTime.Now, StartTime = DateTime.Now, EedTime = DateTime.Now.AddYears(1), Status = 0, UserID = uid, Price = decimal.Parse(coursemodel.price), isSpecial = isspecial, IsCourse = true, OpenClass = false }; subject = coursemodel.CourseName; //付款金额 -必填 total_fee = coursemodel.price; //订单描述 body = coursemodel.CourseName; } string out_trade_no = ""; if (context.Request.UrlReferrer.AbsolutePath.IndexOf("YXSJ") > 0) { order.Favorableprice = decimal.Parse(799.ToString()); } if (!fg) { MyBasicLib.Utilities.ServiceInvoker.Invoke(alipayService, p => p.Add(order)); out_trade_no = order.ID.ToString(); context.Response.Write(""); // context.Response.Redirect("/Account/MemberCenter"); } //商户订单号 =商户网站订单系统中唯一订单号,必填 if (context.Request["orderid"] != null) { out_trade_no = context.Request["orderid"]; var ali = MyBasicLib.Utilities.ServiceInvoker.Invoke(alipayService, p => p.Get(new Guid(out_trade_no))); if (ali != null && ali.Favorableprice.HasValue) { total_fee = ali.Favorableprice.Value.ToString(); } if (context.Request.UrlReferrer.AbsolutePath.IndexOf("YXSJ") > 0) { total_fee = 799.ToString(); } } //订单名称 -必填 //商品展示地址 //需以http://开头的完整路径,例如:http://www.pexue.com/Course/Detail/ string show_url = "http://www.pexue.com/Course/Detail/" + rId; //防钓鱼时间戳 //若要使用请调用类文件submit中的query_timestamp函数 string anti_phishing_key = ""; //////////////////////////////////////////////////////////////////////////////////////////////// //把请求参数打包成数组 SortedDictionary sParaTemp = new SortedDictionary(); sParaTemp.Add("partner", Config.Partner); sParaTemp.Add("seller_email", Config.Seller_email); sParaTemp.Add("_input_charset", Config.Input_charset.ToLower()); sParaTemp.Add("service", "create_direct_pay_by_user"); sParaTemp.Add("payment_type", payment_type); sParaTemp.Add("notify_url", notify_url); sParaTemp.Add("return_url", return_url); sParaTemp.Add("out_trade_no", out_trade_no); sParaTemp.Add("subject", subject); sParaTemp.Add("total_fee", total_fee); sParaTemp.Add("body", body); sParaTemp.Add("show_url", show_url); sParaTemp.Add("anti_phishing_key", anti_phishing_key); sParaTemp.Add("exter_invoke_ip", exter_invoke_ip); //建立请求 string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认"); context.Response.Write(sHtmlText); } } public bool IsReusable { get { return false; } } } }