private string SendHB(string opid, string stid, string hb, string cursjd) { var str = string.Empty; DateTime now = DateTime.Now; TimeSpan span = now.TimeOfDay; TimeSpan end = new TimeSpan(15, 0, 0); TimeSpan end1 = new TimeSpan(12, 0, 0); //cursjd.Equals("1") || //cursjd.Equals("1") && DateTime.Now.Hour >= 11 && span < end1 //cursjd.Equals("2") && DateTime.Now.Hour >= 14 && span < end if (cursjd.Equals("1") && DateTime.Now.Hour >= 11 && span < end1) { #region 设置参数信息 try { string mchbillno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28); string nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", WebConfigurationManager.AppSettings["wxappid8"]); //公众账号ID packageReqHandler.SetParameter("mch_id", WebConfigurationManager.AppSettings["WeixinPay_PartnerId8"]); //商户号 packageReqHandler.SetParameter("mch_billno", mchbillno); //填入商家订单号 packageReqHandler.SetParameter("send_name", WebConfigurationManager.AppSettings["sendname8"]); //红包发送者名称 packageReqHandler.SetParameter("re_openid", opid); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", hb); //付款金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", WebConfigurationManager.AppSettings["hbzf8"]); //红包祝福语 packageReqHandler.SetParameter("client_ip", _context.Request.UserHostAddress); //调用接口的机器Ip地址 packageReqHandler.SetParameter("act_name", WebConfigurationManager.AppSettings["hbname8"]); //活动名称 packageReqHandler.SetParameter("remark", WebConfigurationManager.AppSettings["hbDesc8"]); //备注信息 string sign = packageReqHandler.CreateMd5Sign("key", WebConfigurationManager.AppSettings["WeixinPay_Key8"]); packageReqHandler.SetParameter("sign", sign); //签名 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = WebConfigurationManager.AppSettings["zswz8"]; //私钥(在安装证书时设置) string password = WebConfigurationManager.AppSettings["WeixinPay_PartnerId8"]; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); #endregion #region 发起post请求 HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream; stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); WxZFData tdata = new WxZFData(); tdata.FromXml(responseContent); string return_code = tdata.GetValue("return_code").ToString(); //状态码 string return_msg = tdata.GetValue("return_msg").ToString(); //状态码 string result_code = tdata.GetValue("result_code").ToString().ToUpper(); if ("SUCCESS".Equals(return_code)) { //using (WXDBEntities db = new WXDBEntities()) //{ // OperateLoginfo mt = new OperateLoginfo() // { // Title = "五洲国际红包", // Descs = "进入", // AddTime = DateTime.Now, // UpdateTime = DateTime.Now, // Status = 0, // Orders = 0, // Extent1 = return_msg, // Extent2 = result_code, // LogType = 0 // }; // db.OperateLoginfo.AddObject(mt); // db.SaveChanges(); //} if ("SUCCESS".Equals(result_code)) { //红包发送成功! using (WXDBEntities db = new WXDBEntities()) { //OperateLoginfo mt = new OperateLoginfo() //{ // Title = "五洲国际红包", // Descs = stid, // AddTime = DateTime.Now, // UpdateTime = DateTime.Now, // Status = 0, // Orders = 0, // Extent1 = "", // Extent2 = cursjd, // LogType = 0 //}; //db.OperateLoginfo.AddObject(mt); //db.SaveChanges(); var model = new Oders() { OrderId = mchbillno, Title = "五洲红包", Number = 1, Mobile = "", Soucre = opid, Remark = "345", AddTime = DateTime.Now, UpdateTime = DateTime.Now, OrderStatus = 0, Status = 0, Orders = 0, Extent1 = stid, Extent2 = "789", AddUser = cursjd, UpdateUser = "******", CheckTime = DateTime.Now, CheckoutTime = DateTime.Now, Totals = Convert.ToInt32(hb) }; db.Oders.AddObject(model); db.SaveChanges(); } //加入数据库操作 str = "1|发送红包成功|" + hb; } else { string err_code = tdata.GetValue("err_code").ToString(); string err_code_des = tdata.GetValue("err_code_des").ToString(); using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = "五洲国际红包", Descs = err_code, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = "", Extent2 = err_code_des, LogType = 1 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } //红包发送失败 str = "2|发送红包失败!"; } } else { using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = "五洲国际红包", Descs = return_msg, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = "", Extent2 = "", LogType = 2 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } str = "3|发送红包失败"; } return(str); } catch (Exception ex) { using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = "五洲国际红包", Descs = ex.Message, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = hb, Extent2 = "", LogType = 6 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } return("4|获取失败!"); } } else { str = "2|发送红包失败!"; return(str); } #endregion }
private string SendHB(HttpContext context, string opid, string name, string phone, string hb) { #region 设置参数信息 string mchbillno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28); string nonceStr = TenPayV3Util.GetNoncestr(); RequestHandler packageReqHandler = new RequestHandler(null); //设置package订单参数 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", WebConfigurationManager.AppSettings["wxappid"]); //公众账号ID packageReqHandler.SetParameter("mch_id", WebConfigurationManager.AppSettings["WeixinPay_PartnerId"]); //商户号 packageReqHandler.SetParameter("mch_billno", mchbillno); //填入商家订单号 packageReqHandler.SetParameter("send_name", "D3舍首饰设计创意生活"); //红包发送者名称 packageReqHandler.SetParameter("re_openid", opid); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", hb); //付款金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", WebConfigurationManager.AppSettings["hbzf"]); //红包祝福语 packageReqHandler.SetParameter("client_ip", context.Request.UserHostAddress); //调用接口的机器Ip地址 packageReqHandler.SetParameter("act_name", WebConfigurationManager.AppSettings["hbname"]); //活动名称 packageReqHandler.SetParameter("remark", WebConfigurationManager.AppSettings["hbDesc"]); //备注信息 string sign = packageReqHandler.CreateMd5Sign("key", WebConfigurationManager.AppSettings["WeixinPay_Key"]); packageReqHandler.SetParameter("sign", sign); //签名 //最新的官方文档中将以下三个字段去除了 //packageReqHandler.SetParameter("nick_name", "提供方名称"); //提供方名称 //packageReqHandler.SetParameter("max_value", "100"); //最大红包金额,单位分 //packageReqHandler.SetParameter("min_value", "100"); //最小红包金额,单位分 //发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = WebConfigurationManager.AppSettings["zswz"]; //私钥(在安装证书时设置) string password = WebConfigurationManager.AppSettings["WeixinPay_PartnerId"]; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); #endregion #region 发起post请求 try { HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url); webrequest.ClientCertificates.Add(cer); webrequest.Method = "post"; byte[] postdatabyte = Encoding.UTF8.GetBytes(data); webrequest.ContentLength = postdatabyte.Length; Stream stream; stream = webrequest.GetRequestStream(); stream.Write(postdatabyte, 0, postdatabyte.Length); stream.Close(); HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); //using (WXDBEntities db = new WXDBEntities()) //{ // OperateLoginfo mt = new OperateLoginfo() // { // Title = name, // Descs = "返回成功", // AddTime = DateTime.Now, // UpdateTime = DateTime.Now, // Status = 0, // Orders = 0, // Extent1 = "", // Extent2 = "", // LogType = 1 // }; // db.OperateLoginfo.AddObject(mt); // db.SaveChanges(); //} WxZFData tdata = new WxZFData(); tdata.FromXml(responseContent); string return_code = tdata.GetValue("return_code").ToString(); //状态码 string return_msg = tdata.GetValue("return_msg").ToString(); //状态码 //using (WXDBEntities db = new WXDBEntities()) //{ // OperateLoginfo mt = new OperateLoginfo() // { // Title = name, // Descs = "返回值:" + return_code, // AddTime = DateTime.Now, // UpdateTime = DateTime.Now, // Status = 0, // Orders = 0, // Extent1 = "", // Extent2 = "", // LogType = 1 // }; // db.OperateLoginfo.AddObject(mt); // db.SaveChanges(); //} var str = string.Empty; if ("SUCCESS".Equals(return_code)) { //string zfsign = tdata.GetValue("sign").ToString(); string result_code = tdata.GetValue("result_code").ToString(); //using (WXDBEntities db = new WXDBEntities()) //{ // //+ "|" + err_code + "|" + err_code_des // OperateLoginfo mt = new OperateLoginfo() // { // Title = name, // Descs = "返回值:" + result_code, // AddTime = DateTime.Now, // UpdateTime = DateTime.Now, // Status = 0, // Orders = 0, // Extent1 = "", // Extent2 = "", // LogType = 1 // }; // db.OperateLoginfo.AddObject(mt); // db.SaveChanges(); //} if ("SUCCESS".Equals(result_code)) { //红包发送成功! using (WXDBEntities db = new WXDBEntities()) { var model = new Forms() { Title = "万圣节红包", FormType = 0, Name = name, Number = 1, Mobile = phone, Age = 0, Source = opid, Income = "", Remark = "", AddTime = DateTime.Now, Status = 0, Orders = 0, UpdateTime = DateTime.Now, Extend = context.Request.UserHostAddress, Extend2 = "", Type = 8, JFCount = Convert.ToDouble(hb) }; db.Forms.AddObject(model); db.SaveChanges(); } //加入数据库操作 str = "1|发送红包成功"; } else { string err_code = tdata.GetValue("err_code").ToString(); string err_code_des = tdata.GetValue("err_code_des").ToString(); using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = name, Descs = err_code, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = "", Extent2 = err_code_des, LogType = 1 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } //红包发送失败 str = "2|发送红包失败!"; } } else { using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = name, Descs = return_msg, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = "", Extent2 = "", LogType = 1 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } str = "3|发送红包失败"; } return(str); } catch (Exception ex) { using (WXDBEntities db = new WXDBEntities()) { OperateLoginfo mt = new OperateLoginfo() { Title = name, Descs = ex.Message, AddTime = DateTime.Now, UpdateTime = DateTime.Now, Status = 0, Orders = 0, Extent1 = "", Extent2 = "", LogType = 5 }; db.OperateLoginfo.AddObject(mt); db.SaveChanges(); } return("4|获取失败!"); } #endregion }