支付宝实例:
页面跳转同步通知页面
protected void Page_Load(object sender, EventArgs e) { SortedDictionary<string, string> sPara = GetRequestGet(); if (sPara.Count > 0)//判断是否有带返回参数 { Notify aliNotify = new Notify(); string key1 = ""; string key2 = ""; string key3 = ""; bool verifyResult1 = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"], key1); bool verifyResult2 = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"], key2); bool verifyResult3 = aliNotify.Verify(sPara, Request.QueryString["notify_id"], Request.QueryString["sign"], key3); if (verifyResult1 || verifyResult2 || verifyResult3)//验证成功 { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表 //商户订单号 string out_trade_no = Request.QueryString["out_trade_no"]; //支付宝交易号 string trade_no = Request.QueryString["trade_no"]; //交易状态 string trade_status = Request.QueryString["trade_status"]; LogHelper.CreateLog("支付宝同步回调数据成功->订单号:" + out_trade_no + ",交易号:" + trade_no + ",交易状态:" + trade_status, out_trade_no); if (Request.QueryString["trade_status"] == "TRADE_FINISHED" || Request.QueryString["trade_status"] == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 LogHelper.CreateLog("支付宝同步回调数据成功->支付成功->跳转到购买成功页面!", out_trade_no); Session["orderID"] = out_trade_no; Response.Redirect("TicketSuccess.aspx"); } else { LogHelper.CreateLog("支付宝同步回调数据成功->支付失败->跳转到购买失败页面!", out_trade_no); Response.Redirect("TicketFailure.aspx"); } } else//验证失败 { LogHelper.CreateLog("支付宝同步回调数据验证失败->支付失败->跳转到购买失败页面!", "Alipay_Return_Errors"); } } else { LogHelper.CreateLog("支付宝同步回调数据验证失败->无返回参数->支付失败->跳转到购买失败页面!", "Alipay_Return_Errors"); } }
服务器异步通知页面
protected void Page_Load(object sender, EventArgs e) { ipowdb_fengleyuanDataContext ydc = ConnHelper.ipowdb_fengleyuan(); SortedDictionary<string, string> sPara = GetRequestPost(); if (sPara.Count > 0)//判断是否有带返回参数 { Notify aliNotify = new Notify(); string key1 = "key1";//支付宝账号客户的key,这里面写三个是因为有三个支付宝账号,不确定会用到哪个,所以,全部写了,然后通过验证去使用其中的一个 string key2 = "key2"; string key3 = "key3"; bool verifyResult1 = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"], key1); bool verifyResult2= aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"], key2); bool verifyResult3 = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"], key3); if (verifyResult1 || verifyResult2 || verifyResult3)//验证成功 { //商户订单号 string out_trade_no = Request.Form["out_trade_no"]; //支付宝交易号 string trade_no = Request.Form["trade_no"]; //交易状态 string trade_status = Request.Form["trade_status"]; LogHelper.CreateLog("支付宝异步回调数据成功->订单号:" + out_trade_no + ",交易号:" + trade_no + ",交易状态:" + trade_status, out_trade_no); if (trade_status == "TRADE_FINISHED") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在两种情况下出现 //1、开通了普通即时到账,买家付款成功后。 //2、开通了高级即时到账,从该笔交易成功时间算起,过了签约时的可退款时限(如:三个月以内可退款、一年以内可退款等)后。 string orderID = out_trade_no; e_TicketOrder order = ydc.e_TicketOrder.SingleOrDefault(obj => obj.OrderID == orderID); //更改支付状态 if (order.IsPay == 0) { order.IsPay = 1; order.PayType = "支付宝"; order.trade_no = trade_no; //卖家账号 if (Request.Form["seller_email"] != null) { order.seller_email = Request.Form["seller_email"]; } //买家账号 if (Request.Form["buyer_email"] != null) { order.buyer_email = Request.Form["buyer_email"]; } ydc.SubmitChanges(); LogHelper.CreateLog("TRADE_FINISHED->支付成功", orderID); } //发送短信 if (order.SmsStatus == 0) { string product_id = ""; var products = ydc.e_TicketOrderProduct.Where(obj => obj.OrderID == orderID); foreach (var item in products) { var ticket = ydc.e_Ticket.SingleOrDefault(obj => obj.TicketID == item.TicketID); product_id += ticket.ProductId + ","; } if (product_id.Length > 0) { product_id = product_id.Substring(0, product_id.Length - 1); } LogHelper.CreateLog("TRADE_FINISHED->开始发送电子凭证。" + product_id + "," + orderID + "," + order.Mobile + "," + order.CardID + "," + order.PlayDate, orderID); YNClass ync = new YNClass(); //彩信内容 string mms_content = "xx门票,您购买的门票订单号是:" + orderID + "。您也可以前往官网 http://www.xx.com.cn/ticket/ticketSea.aspx 查询您的订单状态。"; SendTicketRet result = ync.sendticket("xx@126.com", "fengleyuan" , product_id, orderID, order.Mobile, "qr", "", "sms_mms", "1", "only_one" , DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00" , Convert.ToDateTime(order.PlayDate).ToString("yyyy-MM-dd") + (" 23:59:59") , "xx门票二维码凭证", mms_content, mms_content, "无", "http://www.xx.com.cn/ticket/YN_Notify.aspx"); if (result.ErrorCode == "0") { order.SmsStatus = 1; order.SmsID = result.OrderID; ydc.SubmitChanges(); LogHelper.CreateLog("TRADE_FINISHED->开始发送电子凭证->电子凭证发送成功", orderID); } else { LogHelper.CreateLog("TRADE_FINISHED->开始发送电子凭证->凭证发送失败,原因:" + result.ErrorCode + "," + result.Description, orderID); } } } else if (trade_status == "TRADE_SUCCESS") { //判断该笔订单是否在商户网站中已经做过处理 //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序 //如果有做过处理,不执行商户的业务程序 //注意: //该种交易状态只在一种情况下出现——开通了高级即时到账,买家付款成功后。 string orderID = out_trade_no; e_TicketOrder order = ydc.e_TicketOrder.SingleOrDefault(obj => obj.OrderID == orderID); //更改支付状态 if (order.IsPay == 0) { order.IsPay = 1; order.PayType = "支付宝"; order.trade_no = trade_no; //卖家账号 if (Request.Form["seller_email"] != null) { order.seller_email = Request.Form["seller_email"]; } //买家账号 if (Request.Form["buyer_email"] != null) { order.buyer_email = Request.Form["buyer_email"]; } ydc.SubmitChanges(); LogHelper.CreateLog("TRADE_SUCCESS->支付成功", orderID); } //发送短信 if (order.SmsStatus == 0) { string product_id = ""; var products = ydc.e_TicketOrderProduct.Where(obj => obj.OrderID == orderID); foreach (var item in products) { var ticket = ydc.e_Ticket.SingleOrDefault(obj => obj.TicketID == item.TicketID); product_id += ticket.ProductId + ","; } if (product_id.Length > 0) { product_id = product_id.Substring(0, product_id.Length - 1); } LogHelper.CreateLog("TRADE_SUCCESS->开始发送电子凭证。" + product_id + "," + orderID + "," + order.Mobile + "," + order.CardID + "," + order.PlayDate, orderID); YNClass ync = new YNClass(); //彩信内容 string mms_content = "xx门票,您购买的门票订单号是:" + orderID + "。您也可以前往官网 http://www.xx.com.cn/ticket/ticketSea.aspx 查询您的订单状态。"; SendTicketRet result = ync.sendticket("xx@126.com", "fengleyuan" , product_id, orderID, order.Mobile, "qr", "", "sms_mms", "1", "only_one" , DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00" , Convert.ToDateTime(order.PlayDate).ToString("yyyy-MM-dd") + (" 23:59:59") , "xx门票二维码凭证", mms_content, mms_content, "无", "http://www.xx.com.cn/ticket/YN_Notify.aspx"); if (result.ErrorCode == "0") { order.SmsStatus = 1; order.SmsID = result.OrderID; ydc.SubmitChanges(); LogHelper.CreateLog("TRADE_SUCCESS->开始发送电子凭证->电子凭证发送成功", orderID); } else { LogHelper.CreateLog("TRADE_SUCCESS->开始发送电子凭证->凭证发送失败,原因:" + result.ErrorCode + "," + result.Description, orderID); } } } else { LogHelper.CreateLog("出现第三种状态->else", "Alipay_Notify"); } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— Response.Write("success"); //请不要修改或删除 ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { LogHelper.CreateLog("验证失败->fail", "Alipay_Notify"); Response.Write("fail"); } } else { LogHelper.CreateLog("无通知参数", "Alipay_Notify"); Response.Write("无通知参数"); } }
支付宝demo下载地址:http://pan.baidu.com/s/1hqABnYO