//post数据 public static WxPayDataTool GetResult(string url, WxPayDataTool inputobj, bool isUseCert = true) { string xml = inputobj.ToXml(); //LogDB.DebugTest("发送的数据:" + xml); string response = UtilTool.Post(url, xml, isUseCert); //LogDB.DebugTest("接收的结果:"+response); WxPayDataTool result = new WxPayDataTool(); result.FromXml(response); return(result); }
public new WxPayDataTool ProcessNotify() { WxPayDataTool notifyData = GetNotifyData(_InputStream); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayDataTool res = new WxPayDataTool(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); // Util.WriteFile(@"D:\ppp\log\wx.txt", "统一下单支付结果回调出错:" + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } string transaction_id = notifyData.GetValue("transaction_id").ToString(); string out_trade_no = notifyData.GetValue("out_trade_no").ToString(); string total = notifyData.GetValue("total_fee").ToString(); //查询订单,判断订单真实性 OrderqueryInfo info = new OrderqueryInfo() { Transaction_id = transaction_id }; if (!QueryOrder(info)) { //若订单查询失败,则立即返回结果给微信支付后台 WxPayDataTool res = new WxPayDataTool(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); //Util.WriteFile(@"D:\ppp\log\wx.txt", "订单查询失败:" + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } WxPayDataTool result = new WxPayDataTool(); result.SetValue("transaction_id", transaction_id); result.SetValue("out_trade_no", out_trade_no); result.SetValue("total_fee", total); if (notifyData.IsSet("result_code")) { result.SetValue("result_code", notifyData.GetValue("result_code").ToString()); } if (notifyData.IsSet("return_code")) { result.SetValue("return_code", notifyData.GetValue("return_code").ToString()); } return(result); }
/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public WxPayDataTool GetNotifyData(Stream InputStream) { //接收从微信后台POST过来的数据 System.IO.Stream s = 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(); //转换数据格式并验证签名 WxPayDataTool data = new WxPayDataTool(); try { //LogDB.DebugTest("支付结果回调信息:" + builder.ToString()); data.FromXml(builder.ToString()); } catch (Exception ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayDataTool res = new WxPayDataTool(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); //LogDB.DebugTest("异步回调信息出错:" + res.ToXml()); page.Response.Write(res.ToXml()); page.Response.End(); } return(data); }