protected void Page_Load(object sender, EventArgs e) { string code = Request["code"] ?? ""; string state = Request["state"] ?? ""; PayLogHelper.WritePayLog(code + " ==== " + state); }
protected void Page_Load(object sender, EventArgs e) { System.IO.Stream s = 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(); PayLogHelper.WriteEvent(builder.ToString(), "扫呗支付"); //string testresponse = "{\"attach\":\"\",\"channel_trade_no\":\"4009262001201708186903067327\",\"end_time\":\"20170818160418\",\"key_sign\":\"b503c7c76e4df6379c6d43c5103595b9\",\"merchant_name\":\"45对接专用1号\",\"merchant_no\":\"852100210000005\",\"out_trade_no\":\"300508950021217081816035700005\",\"pay_type\":\"010\",\"receipt_fee\":\"10\",\"result_code\":\"01\",\"return_code\":\"01\",\"return_msg\":\"支付成功\",\"terminal_id\":\"30050895\",\"terminal_time\":\"20170818160255\",\"terminal_trace\":\"b609bfed0a8f4badaa5373d20b30a52c\",\"total_fee\":\"10\",\"user_id\":\"on9DrwJ7GgmOaxHBN_yIkSCeBZVo\"}"; JavaScriptSerializer jsonSerialize = new JavaScriptSerializer(); NotifyResponse ack = jsonSerialize.Deserialize <NotifyResponse>(builder.ToString()); //NotifyResponse ack = jsonSerialize.Deserialize<NotifyResponse>(testresponse); Response.ContentType = "application/json"; Response.HeaderEncoding = Encoding.UTF8; try { if (ack.CheckSign()) { if (ack.return_code == "01") { if (ack.result_code == "01") { string out_trade_no = ack.out_trade_no; decimal total_fee = Convert.ToDecimal(ack.total_fee); decimal payAmount = total_fee / 100; Flw_OrderBusiness.OrderPay(out_trade_no, payAmount, SelttleType.LcswPay); } } Response.Write("{\"return_code\":\"01\",\"return_msg\":\"success\"}"); } else { Response.Write("{\"return_code\":\"02\",\"return_msg\":\"签名失败\"}"); } } catch (Exception ex) { Response.Write("{\"return_code\":\"02\",\"return_msg\":\"签名失败\"}"); PayLogHelper.WriteError(ex); } }
/// <summary> /// 回调函数 /// </summary> public void PayNotifyUrl() { PayLogHelper.Debug("进入微信支付回调HttpPost WeChatPay PayNotifyUrl"); WxPayData notifyData = GetNotifyData(); string out_trade_no = notifyData.GetValue("out_trade_no").ToString(); string transaction_id = notifyData.GetValue("transaction_id").ToString(); string totalFee = notifyData.GetValue("total_fee").ToString(); if (notifyData.GetValue("return_code").ToString() == "SUCCESS" && notifyData.GetValue("result_code").ToString() == "SUCCESS") { PayLogHelper.Debug("WX:获取到notifyData值"); PayLogHelper.Debug("WXout_trade_no:" + out_trade_no); Response.Write("成功"); //支付成功,以下执行业务处理 using (HouseMovingDBEntities db = new HouseMovingDBEntities()) { ResponseMessage msg = new ResponseMessage(); msg.Status = true; var entity = db.Order.FirstOrDefault(p => p.OrderNo == out_trade_no && p.PayState != "2"); if (entity != null) { entity.PayTime = DateTime.Now.ToString(FormatDateTime.LongDateTimeStr); entity.PayState = "1"; entity.PayType = "WX"; db.Entry(entity).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } //注释下处代码,允许微信回调,重复通知申请支付的网站,让客户端做重复处理 WxPayData res = new WxPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); PayLogHelper.Debug("WX:order query success : " + res.ToXml()); System.Web.HttpContext.Current.Response.Write(res.ToXml()); System.Web.HttpContext.Current.Response.End(); } } else { PayLogHelper.Debug("WX支付失败"); Response.Write("失败"); //失败 //msg.Result = AESEncryption.AESEncrypt("false"); } }
/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public WxPayData GetNotifyData() { //接收从微信后台POST过来的数据 System.IO.Stream s = System.Web.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(); PayLogHelper.Debug("WX:" + 1111); //转换数据格式并验证签名 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); PayLogHelper.Debug("WX:若签名错误" + "Sign check error : " + res.ToXml()); } PayLogHelper.Debug("WX:Check sign success"); return(data); }
/// <summary> /// 微信退款 /// </summary> /// <param name="origTransactionNo">支付时,微信流水号</param> /// <param name="origOutTradeNo">支付时,商户订单号</param> /// <param name="refundFee">退款金额 单位:分</param> /// <param name="totalFee">订单总金额 单位:分</param> public ActionResult WeChatRefund(string origTransactionNo, string origOutTradeNo, string refundFee, string totalFee) { //origOutTradeNo = "20181102225722191"; //refundFee = "1"; //totalFee = "1"; ResponseMessage msg = new ResponseMessage(); using (HouseMovingDBEntities db = new HouseMovingDBEntities()) { msg.Status = true; var entity = db.Order.FirstOrDefault(p => p.OrderNo == origOutTradeNo); if (DateTime.Parse(entity.ServiceTime).AddHours(-2) <= DateTime.Now) { msg.Status = false; msg.Result = "504"; msg.Msg = "定单离服务时间小于2小时,不能取消订单!"; return(Json(msg, JsonRequestBehavior.AllowGet)); } } //若 出错 看http://www.cnblogs.com/ithome8/p/5189926.html WxPayData data = Refund.Run2(origTransactionNo, origOutTradeNo, refundFee, totalFee); try { if (data.GetValue("return_code").ToString() == "SUCCESS" && data.GetValue("result_code").ToString() == "SUCCESS") { using (HouseMovingDBEntities db = new HouseMovingDBEntities()) { try { db.Database.ExecuteSqlCommand("update [Order] set PayState=2 where OrderNo= '" + origOutTradeNo + "'"); msg.Status = true; } catch (Exception e) { msg.Status = false; msg.Result = "500"; msg.Msg = "退款数据更新失败"; } return(Json(msg, JsonRequestBehavior.AllowGet)); } PayLogHelper.Debug("WX退款成功" + origTransactionNo); msg.Status = true; } else { PayLogHelper.Debug("WX退款失败" + origTransactionNo); msg.Status = false; msg.Result = "501"; //msg.Msg = data.GetValue("err_code_des").ToString(); } } catch (WxPayException ex) { msg.Status = false; msg.Result = "502"; msg.Msg = "退款出错"; //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); PayLogHelper.Debug("WX:若签名错误" + "Sign check error : " + res.ToXml()); } return(Json(msg, JsonRequestBehavior.AllowGet)); }
protected void Page_Load(object sender, EventArgs e) { System.IO.Stream s = 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.GetEncoding("GBK").GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); PayLogHelper.WriteEvent(builder.ToString(), "新大陆支付"); JavaScriptSerializer jsonSerialize = new JavaScriptSerializer(); PosStarCallback callback = jsonSerialize.Deserialize <PosStarCallback>(builder.ToString()); if (callback != null) { string out_trade_no = callback.TxnLogId; decimal total_fee = Convert.ToDecimal(callback.TxnAmt); decimal payAmount = total_fee / 100; Flw_OrderBusiness.OrderPay(out_trade_no, payAmount, SelttleType.StarPos); #region MyRegion // Flw_Order order = Flw_OrderBusiness.GetOrderModel(out_trade_no); // if (order != null) // { // decimal PayCount = order.PayCount != null ? (decimal)order.PayCount * 100 : 0; //应付金额 // decimal FreePay = order.FreePay != null ? (decimal)order.FreePay * 100 : 0; //减免金额 // if (total_fee == PayCount - FreePay) // { // string payTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // string sql = string.Format("update Flw_Order set OrderStatus=2, RealPay='{0}', PayTime='{1}' where OrderID='{2}'", payAmount, payTime, out_trade_no); // XCCloudBLL.ExecuteSql(sql); // OrderPayNotify.AddOrderPayCache(out_trade_no, payAmount, payTime, 2); // sql = @"SELECT a.StoreID, SettleFee FROM Base_StoreInfo a // INNER JOIN Flw_Order b ON b.StoreID = a.StoreID // INNER JOIN Base_SettlePPOS c ON c.ID = a.SettleID // WHERE b.OrderID = '" + out_trade_no + "'"; // DataTable dt = XCCloudBLL.ExecuterSqlToTable(sql); // if (dt.Rows.Count > 0) // { // //获取当前结算费率,计算手续费 // double fee = Convert.ToDouble(dt.Rows[0]["SettleFee"]); // double d = Math.Round(Convert.ToDouble(payAmount) * fee, 2, MidpointRounding.AwayFromZero); //最小单位为0.01元 // if (d < 0.01) d = 0.01; // sql = "update Flw_Order set PayFee='" + d + "' where OrderID='" + out_trade_no + "'"; // XCCloudBLL.ExecuteSql(sql); // } // } // else // { // //支付异常 // //PayList.AddNewItem(out_trade_no, amount); // string sql = string.Format("update Flw_Order set OrderStatus=3, RealPay='{0}', PayTime=GETDATE() where OrderID='{1}'", payAmount, out_trade_no); // XCCloudBLL.ExecuteSql(sql); // } // } #endregion } Response.ContentType = "application/json"; Response.HeaderEncoding = Encoding.GetEncoding("GBK"); //string blank = ""; //blank = blank.PadLeft(64, ' '); //string responseWrite= string r = "{\"RspCode\":\"000000\",\"RspDes\":\"\"}"; //Response.Write(string.Format("{\"RspCode\":\"000000\",\"RspDes\":\"{0}\"}", blank)); Response.Write(r); }
protected void Page_Load(object sender, EventArgs e) { SortedDictionary <string, string> sPara = GetRequestPost(); PayLogHelper.WriteEvent(Request.Form.ToString(), "支付宝支付"); if (sPara.Count > 0)//判断是否有带返回参数 { Notify aliNotify = new Notify(AliPayConfig.charset, AliPayConfig.sign_type, AliPayConfig.pid, AliPayConfig.mapiUrl, AliPayConfig.alipay_public_key); bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]); if (verifyResult && CheckParams())//验证成功 { ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //请在这里加上商户的业务逻辑程序代码 //——请根据您的业务逻辑来编写程序(以下代码仅作参考)—— //获取支付宝的通知返回参数,可参考技术文档中服务器异步通知参数列表 //商户订单号 string out_trade_no = Request.Form["out_trade_no"]; //支付宝交易号 string trade_no = Request.Form["trade_no"]; //获得支付总金额total_amount string total_amount = Request.Form["total_amount"]; //交易状态 //在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,才是买家付款成功。 string trade_status = Request.Form["trade_status"]; //交易状态 if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED") { try { decimal amount = Convert.ToDecimal(total_amount); Flw_OrderBusiness.OrderPay(out_trade_no, amount, SelttleType.AliWxPay); } catch (Exception ex) { PayLogHelper.WriteError(ex); } Response.Write("success"); //请不要修改或删除 } else { Response.Write("fail"); } //——请根据您的业务逻辑来编写程序(以上代码仅作参考)—— ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } else//验证失败 { //PayLogHelper.WriteEvent("支付宝验证失败"); Response.Write("fail"); } } else { Response.Write("无通知参数"); } }
public WxPayData GetNotifyData() { //接收从微信后台POST过来的数据 System.IO.Stream s = 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(); // string temp = @"<xml><appid><![CDATA[wx359e3843fe4c20e6]]></appid> //<bank_type><![CDATA[ABC_DEBIT]]></bank_type> //<cash_fee><![CDATA[1]]></cash_fee> //<device_info><![CDATA[100010_SC-201609020843]]></device_info> //<fee_type><![CDATA[CNY]]></fee_type> //<is_subscribe><![CDATA[Y]]></is_subscribe> //<mch_id><![CDATA[1264100601]]></mch_id> //<nonce_str><![CDATA[9551f570dc8b434a98c7fb5e95543664]]></nonce_str> //<openid><![CDATA[oaxaDv5ATDpyyTJjh03vl_x55a9I]]></openid> //<out_trade_no><![CDATA[03ad8b23b8554450b272b543bd5f9dbd]]></out_trade_no> //<result_code><![CDATA[SUCCESS]]></result_code> //<return_code><![CDATA[SUCCESS]]></return_code> //<sign><![CDATA[774AB611BAEF0D61B94AD5FD5DF3097D]]></sign> //<time_end><![CDATA[20170301141219]]></time_end> //<total_fee>1</total_fee> //<trade_type><![CDATA[NATIVE]]></trade_type> //<transaction_id><![CDATA[4009262001201703011791419767]]></transaction_id> //</xml>"; //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { PayLogHelper.WriteEvent(builder.ToString(), "微信支付"); data.FromXml(builder.ToString()); string resule = data.GetValue("result_code").ToString(); if (resule == "SUCCESS") { string out_trade_no = data.GetValue("out_trade_no").ToString(); decimal total_fee = Convert.ToDecimal(data.GetValue("total_fee")); decimal payAmount = total_fee / 100; Flw_OrderBusiness.OrderPay(out_trade_no, payAmount, SelttleType.AliWxPay); #region MyRegion //Flw_Order order = Flw_OrderBusiness.GetOrderModel(out_trade_no); //if (order != null) //{ // decimal PayCount = order.PayCount != null ? (decimal)order.PayCount * 100 : 0; //应付金额 // decimal FreePay = order.FreePay != null ? (decimal)order.FreePay * 100 : 0; //减免金额 // if (total_fee == PayCount - FreePay) // { // string payTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); // string sql = string.Format("update Flw_Order set OrderStatus=2, RealPay='{0}', PayTime='{1}' where OrderID='{2}'", payAmount, payTime, out_trade_no); // XCCloudBLL.ExecuteSql(sql); // OrderPayNotify.AddOrderPayCache(out_trade_no, payAmount, payTime, 2); // sql = "SELECT SettleFee FROM Base_StoreInfo a, Base_SettleOrg b WHERE a.SettleID = b.ID AND a.StoreID = '" + order.StoreID + "'"; // DataTable dt = XCCloudBLL.ExecuterSqlToTable(sql); // if (dt.Rows.Count > 0) // { // //获取当前结算费率,计算手续费 // double fee = Convert.ToDouble(dt.Rows[0]["SettleFee"]); // double d = Math.Round(Convert.ToDouble(payAmount) * fee, 2, MidpointRounding.AwayFromZero); //最小单位为0.01元 // if (d < 0.01) d = 0.01; // sql = "update Flw_Order set PayFee='" + d + "' where OrderID='" + out_trade_no + "'"; // XCCloudBLL.ExecuteSql(sql); // } // } // else // { // //支付异常 // //PayList.AddNewItem(out_trade_no, amount); // string sql = string.Format("update Flw_Order set OrderStatus=3, RealPay='{0}', PayTime=GETDATE() where OrderID='{1}'", payAmount, out_trade_no); // XCCloudBLL.ExecuteSql(sql); // } //} #endregion } //data.FromXml(temp); } catch (WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); Response.Write(res.ToXml()); Response.End(); } return(data); }