//[AllowUser] //public ActionResult TestAliPay() //{ // var receipt_amount = "0.01"; // var trade_no = "20181226220013......."; // var return_code = "TRADE_SUCCESS"; // var out_trade_no = "20181226103124129"; // var gmt_payment = "2018-12-26 10:31:29"; // string msg = ""; // bool res = OrderBll.Value.CompleteAliPay(receipt_amount, trade_no, return_code, out_trade_no, gmt_payment, out msg); // return Json(res); //} /// <summary> /// 获取支付宝Get过来的通知消息,并以“参数名=参数值”的形式组成数组 /// </summary> /// <returns></returns> public IDictionary <string, string> GetRequestGet() { Pay.Log Log = new Pay.Log(Pay.AliPay.AliPayConfig.LogPath); int i = 0; IDictionary <string, string> sArry = new Dictionary <string, string>(); NameValueCollection coll; coll = Request.QueryString; String[] requstItem = coll.AllKeys; for (i = 0; i < requstItem.Length; i++) { Log.Info("GetRequestGet", requstItem[i] + ":" + Request.QueryString[requstItem[i]]); sArry.Add(requstItem[i], Request.QueryString[requstItem[i]]); } return(sArry); }
/// <summary> /// 页面跳转同步通知页面 /// </summary> /// <returns></returns> public ActionResult ReturnUrl() { Pay.Log Log = new Pay.Log(Pay.AliPay.AliPayConfig.LogPath); Log.Info("ReturnUrl", "支付页面同步回调"); //将同步通知中收到的所有参数都存放到map中 IDictionary <string, string> map = GetRequestGet(); if (map.Count > 0) //判断是否有带返回参数 { try { //支付宝的公钥 string alipayPublicKey = AliPayConfig.payKey; string signType = AliPayConfig.signType; string charset = AliPayConfig.charset; bool keyFromFile = false; // 获取支付宝GET过来反馈信息 bool verify_result = AlipaySignature.RSACheckV1(map, alipayPublicKey, charset, signType, keyFromFile); if (verify_result) { // 验证成功 return(Json(new { Result = true, msg = "验证成功" })); } else { Log.Error("AliPayNotifyUrl", "支付验证失败"); return(Json(new { Result = false, msg = "验证失败" })); } } catch (Exception e) { //throw new Exception(e.Message); return(Json(new { Result = false, msg = "验证失败" })); Log.Error("AliPayNotifyUrl", "支付验证失败"); } } else { return(Json(new { Result = false, msg = "无返回参数" })); } }
/// <summary> /// 获取支付宝POST过来通知消息,并以“参数名=参数值”的形式组成数组 /// </summary> /// <returns>request回来的信息组成的数组</returns> public IDictionary <string, string> GetRequestPost() { Pay.Log Log = new Pay.Log(Pay.AliPay.AliPayConfig.LogPath); int i = 0; IDictionary <string, string> sArray = new Dictionary <string, string>(); NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestItem = coll.AllKeys; for (i = 0; i < requestItem.Length; i++) { Log.Info("GetRequestPost", requestItem[i] + ":" + Request.Form[requestItem[i]]); sArray.Add(requestItem[i], Request.Form[requestItem[i]]); } return(sArray); }
/// <summary> /// 服务器异步通知页面 /// </summary> public void AliPayNotifyUrl() { Pay.Log Log = new Pay.Log(AliPayConfig.LogPath); Log.Info("AliPayNotifyUrl", "支付页面异步回调"); IDictionary <string, string> map = GetRequestPost(); if (map.Count > 0) { try { string alipayPublicKey = AliPayConfig.payKey; string signType = AliPayConfig.signType; string charset = AliPayConfig.charset; bool keyFromFile = false; bool verify_result = AlipaySignature.RSACheckV1(map, alipayPublicKey, charset, signType, keyFromFile); Log.Info("AliPayNotifyUrl验签", verify_result + ""); //验签成功后,按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,校验成功后再response中返回success并继续商户自身业务处理,校验失败返回false if (verify_result) { //商户订单号 string out_trade_no = map["out_trade_no"]; //支付宝交易号 string trade_no = map["trade_no"]; //交易创建时间 string gmt_create = map["gmt_create"]; //交易付款时间 string gmt_payment = map["gmt_payment"]; //通知时间 string notify_time = map["notify_time"]; //通知类型 trade_status_sync string notify_type = map["notify_type"]; //通知校验ID string notify_id = map["notify_id"]; //开发者的app_id string app_id = map["app_id"]; //卖家支付宝用户号 string seller_id = map["seller_id"]; //买家支付宝用户号 string buyer_id = map["buyer_id"]; //实收金额 string receipt_amount = map["receipt_amount"]; //交易状态 string return_code = map["trade_status"]; //交易状态TRADE_FINISHED的通知触发条件是商户签约的产品不支持退款功能的前提下,买家付款成功; //或者,商户签约的产品支持退款功能的前提下,交易已经成功并且已经超过可退款期限 //状态TRADE_SUCCESS的通知触发条件是商户签约的产品支持退款功能的前提下,买家付款成功 if (return_code == "TRADE_FINISHED" || return_code == "TRADE_SUCCESS") { string msg; Log.Error("AliPayNotifyUrl", receipt_amount + "==" + trade_no + "==" + return_code + "==" + out_trade_no + "==" + gmt_payment); //判断该笔订单是否在商户网站中已经做过处理 ///支付回调的业务处理 //bool res = OrderBll.Value.CompleteAliPay(receipt_amount, trade_no, return_code, out_trade_no, gmt_payment, out msg); bool res = true; if (res == false) { Response.Write("添加支付信息失败!"); } Log.Error("AliPayNotifyUrl", "支付成功"); Response.Write("success"); //请不要修改或删除 } } else { //验证失败 Log.Error("AliPayNotifyUrl", "支付验证失败"); Response.Write("验证失败!"); } } catch (Exception e) { Response.Write("添加支付信息失败!"); Log.Error("AliPayNotifyUrl", "添加支付信息失败"); } } else { //无返回参数 Response.Write("无返回参数!"); Log.Error("AliPayNotifyUrl", "无返回参数"); } }
/// <summary> /// 微信支付回调 /// </summary> /// <returns></returns> public string WxAppNotifyUrl() { Pay.Log Log = new Pay.Log(AppPayConfig.LogPath); Log.Info("WxAppNotifyUrl", "支付回调"); AppPayNotify notify = new AppPayNotify(System.Web.HttpContext.Current); AppPayData notifyData = notify.GetNotifyData(); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 AppPayData res = new AppPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); Log.Error(this.GetType().ToString(), "The Pay result is error : " + res.ToXml()); Response.Write(res.ToXml()); Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); //查询订单,判断订单真实性 if (!AppQueryOrder(transaction_id)) { //若订单查询失败,则立即返回结果给微信支付后台 AppPayData res = new AppPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); Log.Error(this.GetType().ToString(), "Order query failure : " + res.ToXml()); Response.Write(res.ToXml()); Response.End(); } //查询订单成功 else { AppPayData res = new AppPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); Log.Info(this.GetType().ToString(), "Order query success : " + res.ToXml()); Log.Info(this.GetType().ToString(), "Order query success,notifyData : " + notifyData.ToXml()); var returnCode = notifyData.GetValue("return_code").ToString(); var transactionNo = transaction_id; //微信订单号 var outTradeNo = notifyData.GetValue("out_trade_no").ToString(); //自定义订单号 var attach = notifyData.GetValue("attach").ToString(); //身份证 var endTime = notifyData.GetValue("time_end").ToString(); //交易结束时间 //var body = notifyData.GetValue("body").ToString();//projectIdlist var totalFee = notifyData.GetValue("total_fee").ToString();; //支付金额 int userId = Convert.ToInt32(attach.Split('|')[0]); string msg; try { ///修改数据库订单状态 //var result = OrderBll.Value.CompleteWePay(userId, totalFee, transactionNo, returnCode, outTradeNo, attach, endTime, out msg); var result = true; Log.Info(this.GetType().ToString(), "CompleteWePay:" + result); } catch (Exception e) { Log.Error(this.GetType().ToString(), "CompleteWePay:" + e.ToString()); } Response.Write(res.ToXml()); Response.End(); } return(""); }