//修改订单信息 public void SuccOrder() { WxPayAPI.WxPayData res = new WxPayAPI.WxPayData(); try { Log.WriteLog("SuccOrder:开始了"); WxPayAPI.WxPayData notifyData = GetNotifyData(); string out_trade_no = notifyData.GetValue("out_trade_no").ToString(); Log.WriteLog("SuccOrder:" + out_trade_no); //查询订单,判断订单真实性 if (!string.IsNullOrEmpty(out_trade_no)) { Order order = db.Orders.FirstOrDefault(c => c.Payno == out_trade_no); if (order != null) { order.Status = 1; order.PayTime = DateTime.Now; db.Orders.Update(order); if (db.SaveChanges() > 0) { Log.WriteLog("SuccOrder:Status"); var active = db.Actives.Find(order.ActiveId); active.Count++; db.Actives.Update(active); db.SaveChanges(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); Response.WriteAsync(res.ToXml()); Log.WriteLog("SuccOrder:SUCCESS"); } } else { Log.WriteLog("SuccOrder:订单查询失败"); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); Response.WriteAsync(res.ToXml()); } } } catch (Exception e) { //打印日志 Log.WriteLog("SuccOrder:Message=" + e.Message); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", e.Message); Response.WriteAsync(res.ToXml()); } }
private void ResponseWrite(bool isSuccess, string msg) { WxPayAPI.WxPayData res = new WxPayAPI.WxPayData(); res.SetValue("return_code", isSuccess ? "SUCCESS" : "FAIL"); res.SetValue("return_msg", msg); base.Response.Write(res.ToXml()); base.Response.End(); }
/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public WxPayAPI.WxPayData GetNotifyData() { string builder = Common.ReadStream2String(Request.Body); Log.WriteLog("weixin:" + builder); /* * //接收从微信后台POST过来的数据 * System.IO.Stream s = Request.Body; * * 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();*/ Log.WriteLog("GetNotifyData:" + builder.ToString()); //转换数据格式并验证签名 WxPayAPI.WxPayData data = new WxPayAPI.WxPayData(); try { Log.WriteLog(builder.ToString()); data.FromXml(builder.ToString()); } catch (WxPayAPI.WxPayException ex) { //若签名错误,则立即返回结果给微信支付后台 WxPayAPI.WxPayData res = new WxPayAPI.WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); Response.WriteAsync(res.ToXml()); Log.WriteLog("GetNotifyData:" + ex.Message); } return(data); }