protected void Page_Load(object sender, EventArgs e) { PayConfig payConfig = new PayConfig(); string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?"; string key = payConfig.SecurityKey; //partner 的对应交易安全校验码(必须填写) string partner = payConfig.Partner; //partner合作伙伴id(必须填写) alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.Form["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的 string responseTxt = Get_Http(alipayNotifyURL, 120000000); int i; NameValueCollection coll = Request.Form; String[] requestarr = coll.AllKeys; //进行排序; string[] Sortedstr = BubbleSort(requestarr); //构造待md5摘要字符串 ; string prestr = ""; for (i = 0; i < Sortedstr.Length; i++) { if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") { if (i == Sortedstr.Length - 1) { prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]]; } else { prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]] + "&"; } } } prestr = prestr + key; //生成Md5摘要; string mysign = GetMD5(prestr); string sign = Request.Form["sign"]; //StreamWriter sw = File.AppendText(Server.MapPath("log.txt")); //sw.WriteLine(DateTime.Now.ToString() + "返回页面:" + RequestHelper.GetStringForm("out_trade_no") + ":" + Request.Form["trade_status"]); //sw.WriteLine("mysign:" + mysign + ";sign:" + sign + ";responseTxt:" + responseTxt); //sw.Flush(); //sw.Close(); if (mysign == sign && responseTxt == "true") //验证支付发过来的消息,签名是否正确 { //sw = File.AppendText(Server.MapPath("log.txt")); //sw.WriteLine(DateTime.Now.ToString() + "验证成功:" + RequestHelper.GetStringForm("out_trade_no") +":"+ Request.Form["trade_status"]); //sw.Flush(); //sw.Close(); if (Request.Form["trade_status"] == "TRADE_FINISHED" || Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS")// 判断支付状态TRADE_FINISHED(文档中有枚举表可以参考) { //更新数据库的订单语句 string orderNumber = RequestHelper.GetForm <string>("out_trade_no"); OrderInfo order = OrderBLL.ReadOrderByNumber(orderNumber, 0); if (order.ID > 0) { if (order.OrderStatus == (int)OrderStatus.WaitPay) { order.OrderStatus = (int)OrderStatus.WaitCheck; OrderBLL.UpdateOrder(order); //增加操作记录 OrderActionInfo orderAction = new OrderActionInfo(); orderAction.OrderID = order.ID; orderAction.OrderOperate = (int)OrderOperate.Pay; orderAction.StartOrderStatus = (int)OrderStatus.WaitPay; orderAction.EndOrderStatus = (int)OrderStatus.WaitCheck; orderAction.Note = "客户支付宝在线支付"; orderAction.IP = ClientHelper.IP; orderAction.Date = RequestHelper.DateNow; orderAction.AdminID = 0; orderAction.AdminName = string.Empty; OrderActionBLL.AddOrderAction(orderAction); } } else { UserRechargeInfo userRecharge = UserRechargeBLL.ReadUserRechargeByNumber(orderNumber, 0); if (userRecharge.ID > 0 && userRecharge.IsFinish == (int)BoolType.False) { userRecharge.IsFinish = (int)BoolType.True; UserRechargeBLL.UpdateUserRecharge(userRecharge); //账户记录 string note = "支付宝在线冲值:" + userRecharge.Number; UserAccountRecordBLL.AddUserAccountRecord(userRecharge.Money, 0, note, userRecharge.UserID, userRecharge.UserName); } } Response.Write("success"); } else { Response.Write("fail"); } } else { Response.Write("fail"); } }
protected void Page_Load(object sender, EventArgs e) { PayConfig payConfig = new PayConfig(); string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?"; string key = payConfig.SecurityKey; //partner 的对应交易安全校验码(必须填写) string _input_charset = "utf-8"; string partner = payConfig.Partner; //partner合作伙伴id(必须填写) alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.QueryString["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的 string responseTxt = Get_Http(alipayNotifyURL, 120000); int i; NameValueCollection coll = Request.QueryString; String[] requestarr = coll.AllKeys; //进行排序; string[] Sortedstr = BubbleSort(requestarr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]); } else { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&"); } } } prestr.Append(key); //生成Md5摘要; string mysign = GetMD5(prestr.ToString(), _input_charset); string sign = Request.QueryString["sign"]; if (mysign == sign && responseTxt == "true") //验证支付发过来的消息,签名是否正确 { //更新数据库的订单语句 message = "成功付款"; string orderNumber = RequestHelper.GetQueryString <string>("out_trade_no"); OrderInfo order = OrderBLL.ReadOrderByNumber(orderNumber, 0); if (order.ID > 0) { if (order.OrderStatus == (int)OrderStatus.WaitPay) { order.OrderStatus = (int)OrderStatus.WaitCheck; OrderBLL.UpdateOrder(order); //增加操作记录 OrderActionInfo orderAction = new OrderActionInfo(); orderAction.OrderID = order.ID; orderAction.OrderOperate = (int)OrderOperate.Pay; orderAction.StartOrderStatus = (int)OrderStatus.WaitPay; orderAction.EndOrderStatus = (int)OrderStatus.WaitCheck; orderAction.Note = "客户支付宝在线支付"; orderAction.IP = ClientHelper.IP; orderAction.Date = RequestHelper.DateNow; orderAction.AdminID = 0; orderAction.AdminName = string.Empty; OrderActionBLL.AddOrderAction(orderAction); message = "您已经成功支付订单:" + orderNumber; } } else { UserRechargeInfo userRecharge = UserRechargeBLL.ReadUserRechargeByNumber(orderNumber, 0); if (userRecharge.ID > 0 && userRecharge.IsFinish == (int)BoolType.False) { userRecharge.IsFinish = (int)BoolType.True; UserRechargeBLL.UpdateUserRecharge(userRecharge); //账户记录 string note = "支付宝在线冲值:" + userRecharge.Number; UserAccountRecordBLL.AddUserAccountRecord(userRecharge.Money, 0, note, userRecharge.UserID, userRecharge.UserName); message = "您的冲值已经成功完成"; } } } else { message = "支付订单出现问题"; } }
protected void Page_Load(object sender, EventArgs e) { PayConfig payConfig = new PayConfig(); string out_trade_no = string.Empty; //订单号 string subject = ""; //商品名称 string body = ""; //商品描述 string price = ""; //单价 string quantity = ""; //数量 string show_url = ""; //展示地址 string logistics_fee = ""; //邮费 string action = RequestHelper.GetQueryString <string>("Action"); int userID = Cookies.User.GetUserID(true); string service = payConfig.Service; //服务参数 trade_create_by_buyer 标准双接口交易 create_direct_pay_by_user 直接付款,create_partner_trade_by_buyer 担保付款 switch (action) { case "Apply": int applyID = RequestHelper.GetQueryString <int>("ApplyID"); UserRechargeInfo userRecharge = UserRechargeBLL.ReadUserRecharge(applyID, userID); out_trade_no = userRecharge.Number; subject = "在线冲值:" + userRecharge.Number; body = "在线冲值"; price = userRecharge.Money.ToString(); quantity = "1"; show_url = "http://" + Request.ServerVariables["Http_Host"]; logistics_fee = "0"; break; case "PayOrder": int orderID = RequestHelper.GetQueryString <int>("OrderID"); OrderInfo order = OrderBLL.ReadOrder(orderID, userID); out_trade_no = order.OrderNumber; subject = "在线支付:" + order.OrderNumber; body = "在线支付"; price = (order.ProductMoney - order.FavorableMoney + order.ShippingMoney + order.OtherMoney - order.Balance - order.CouponMoney).ToString(); quantity = "1"; show_url = "http://" + Request.ServerVariables["Http_Host"]; logistics_fee = "0"; break; default: break; } string gateway = "https://www.alipay.com/cooperate/gateway.do?"; //'支付接口 string partner = payConfig.Partner; // 合作伙伴ID string sign_type = "MD5"; //加密协议 string payment_type = "1"; //支付类型 string seller_email = payConfig.SellerEmail; //卖家账号 string key = payConfig.SecurityKey; //partner账户的支付宝安全校验码 string return_url = "http://" + Request.ServerVariables["Http_Host"] + "/Plugins/Pay/AliPay/Return.aspx"; //服务器返回接口 string notify_url = "http://" + Request.ServerVariables["Http_Host"] + "/Plugins/Pay/AliPay/Notify.aspx"; //服务器通知接口 string _input_charset = "utf-8"; string logistics_type = "POST"; string logistics_payment = "BUYER_PAY"; AliPay ap = new AliPay(); string aliay_url = ap.CreatUrl( gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type, price, show_url, seller_email, key, return_url, _input_charset, notify_url, logistics_type, logistics_fee, logistics_payment, quantity ); Response.Redirect(aliay_url); }