/// <summary> /// 发送图文消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="articles"></param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static string GetNewsXml(string openId, string serverId, List <WXMesage> articles) { StringBuilder strhtml = new StringBuilder(); if (articles != null && articles.Count > 0) { strhtml.Append("<xml>"); strhtml.Append("<ToUserName><![CDATA[" + openId + "]]></ToUserName>"); strhtml.Append("<FromUserName><![CDATA[" + serverId + "]]></FromUserName>"); strhtml.Append("<CreateTime>" + WxUtils.GetWeixinDateTime(DateTime.Now) + "</CreateTime>"); strhtml.Append("<MsgType><![CDATA[news]]></MsgType>"); strhtml.Append("<ArticleCount>" + articles.Count + "</ArticleCount>"); strhtml.Append("<Articles>"); foreach (WXMesage item in articles) { strhtml.Append("<item>"); strhtml.Append("<Title><![CDATA[" + item.Title + "]]></Title>"); strhtml.Append("<Description><![CDATA[" + item.Description + "]]></Description>"); strhtml.Append("<PicUrl><![CDATA[" + item.PicUrl + "]]></PicUrl>"); strhtml.Append("<Url><![CDATA[" + item.Url + "]]></Url>"); strhtml.Append("</item>"); } strhtml.Append("</Articles>"); strhtml.Append("</xml> "); } return(strhtml.ToString()); }
/// <summary> /// 发送文本信息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="content"></param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static string GetTextXml(string openId, string serverId, string content) { string xml = string.Empty; if (!string.IsNullOrEmpty(content)) { xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[{3}]]></Content> </xml>", openId, serverId, WxUtils.GetWeixinDateTime(DateTime.Now), content); } return(xml); }
public PayResult ToPayResult() { try { PayResult result = new PayResult(); result.appid = GetValue("appid") == null ? "" : GetValue("appid").ToString(); result.mch_id = GetValue("mch_id") == null ? "" : GetValue("mch_id").ToString(); result.attach = GetValue("attach") == null ? "" : GetValue("attach").ToString(); result.err_code = GetValue("err_code") == null ? "" : GetValue("err_code").ToString(); result.err_code_des = GetValue("err_code_des") == null ? "" : GetValue("err_code_des").ToString(); result.fee_type = GetValue("fee_type") == null ? "" : GetValue("fee_type").ToString(); result.is_subscribe = GetValue("is_subscribe") == null ? "" : GetValue("is_subscribe").ToString(); result.openid = GetValue("openid") == null ? "" : GetValue("openid").ToString(); result.out_trade_no = GetValue("out_trade_no") == null ? "" : GetValue("out_trade_no").ToString(); result.result_code = GetValue("result_code") == null ? "" : GetValue("result_code").ToString(); result.time_end = DateTime.ParseExact(GetValue("time_end").ToString(), "yyyyMMddHHmmss", null); result.total_fee = Convert.ToInt32(GetValue("total_fee").ToString()); result.trade_type = GetValue("trade_type") == null ? "" : GetValue("trade_type").ToString(); result.transaction_id = GetValue("transaction_id") == null ? "" : GetValue("transaction_id").ToString(); if (!string.IsNullOrEmpty(result.attach)) { //支付类型是在支付的时候paytype参数带过来的 string paytype = WxUtils.getAttachValue(result.attach, "paytype"); if (!string.IsNullOrEmpty(paytype)) { int type; if (int.TryParse(paytype, out type)) { result.paytype = type; } } else { log4net.LogHelper.WriteError(typeof(ResultNotify), new WxPayException("警报:回调函数获取不到支付类型!")); } } if (string.IsNullOrEmpty(result.transaction_id) || string.IsNullOrEmpty(result.openid)) { return(null); } return(result); } catch (Exception ex) { throw ex; } }
/// <summary> /// 发送语音消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="mediaId"></param> /// <returns></returns> public static string GetVoiceXml(string openId, string serverId, string mediaId) { string xml = string.Empty; if (!string.IsNullOrEmpty(mediaId)) { xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[voice]]></MsgType> <Voice> <MediaId><![CDATA[{3}]]></MediaId> </Voice> </xml>", openId, serverId, WxUtils.GetWeixinDateTime(DateTime.Now), mediaId); } return(xml); }
/// <summary> /// 发送视频消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="mediaId"></param> /// <param name="title"></param> /// <param name="description"></param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static string GetVideoXml(string openId, string serverId, string mediaId, string title, string description) { string xml = string.Empty; if (!string.IsNullOrEmpty(mediaId)) { xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[video]]></MsgType> <Video> <MediaId><![CDATA[{3}]]></MediaId> <Title><![CDATA[{4}]]></Title> <Description><![CDATA[{5}]]></Description> </Video> </xml>", openId, serverId, WxUtils.GetWeixinDateTime(DateTime.Now), mediaId, title, description); } return(xml); }
/// <summary> /// 发送音乐消息 /// </summary> /// <param name="accessToken"></param> /// <param name="openId"></param> /// <param name="title">音乐标题(非必须)</param> /// <param name="description">音乐描述(非必须)</param> /// <param name="musicUrl">音乐链接</param> /// <param name="hqMusicUrl">高品质音乐链接,wifi环境优先使用该链接播放音乐</param> /// <param name="thumbMediaId">视频缩略图的媒体ID</param> /// <param name="timeOut">代理请求超时时间(毫秒)</param> /// <returns></returns> public static string GetMusicXml(string openId, string serverId, string title, string description, string musicUrl, string hqMusicUrl, string thumbMediaId) { string xml = string.Empty; if (!string.IsNullOrEmpty(musicUrl)) { xml = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[music]]></MsgType> <Music> <Title><![CDATA[{3}]]></Title> <Description><![CDATA[{4}]]></Description> <MusicUrl><![CDATA[{5}]]></MusicUrl> <HQMusicUrl><![CDATA[{6}]]></HQMusicUrl> <ThumbMediaId><![CDATA[{7}]]></ThumbMediaId> </Music> </xml>", openId, serverId, WxUtils.GetWeixinDateTime(DateTime.Now), title, description, musicUrl, hqMusicUrl, thumbMediaId); } return(xml); }
public override void ProcessNotify() { try { WxPayData notifyData = GetNotifyData(); //检查支付结果中transaction_id是否存在 if (!notifyData.IsSet("transaction_id") || !notifyData.IsSet("appid")) { //若transaction_id不存在,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "支付结果中微信订单号不存在"); log4net.LogHelper.WriteError(GetType(), new Exception("transaction_id不存在 : " + res.ToXml())); context.Response.Write(res.ToXml()); context.Response.End(); return; } string transactionId = notifyData.GetValue("transaction_id").ToString(); string appid = notifyData.GetValue("appid").ToString(); PayCenterSetting setting = new PayCenterSettingBLL().GetPayCenterSetting(appid); //增加重复回调判断 int re = RedisUtil.Get <int>(string.Format(MemCacheKey.ProcessNotify, transactionId)); if (re != 0) { return; } RedisUtil.Set(string.Format(MemCacheKey.ProcessNotify, transactionId), 1, TimeSpan.FromMinutes(30)); //查询订单,判断订单真实性 if (!QueryOrder(transactionId, setting)) { //若订单查询失败,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", "订单查询失败"); log4net.LogHelper.WriteError(GetType(), new Exception("订单查询失败: " + notifyData.ToJson())); context.Response.Write(res.ToXml()); context.Response.End(); } //查询订单成功 else { //说付款成功:插入记录 //这里要注意,微信通知过来之后,15秒之内没有给微信回复处理状态,微信还会第二次,第三次通知。 //带过来的信息一模一样,所以这要做标志判断,万一处理过程出现问题没有给微信回复。 //在以后多次请求的时候避免多次进行业务处理,插入多条记录 PayResult result = notifyData.ToPayResult(); //log4net.LogHelper.WriteInfo(this.GetType(), Newtonsoft.Json.JsonConvert.SerializeObject(result)); if (result == null) { return; } if ((WxUtils.getAttachValue(result.attach, "from") == "shop"))//商城支付 { } else { int id = Convert.ToInt32(new PayResultBLL().Add(result));//插入记录,论坛,直播、有约 result.Id = id; NotifyOper(result); } } } catch (Exception ex) { log4net.LogHelper.WriteError(typeof(ResultNotify), ex); } finally { //最后要给微信放回接收成功数据,不然微信会连续多次发送同样请求 WxPayData res = new WxPayData(); res.SetValue("return_code", "SUCCESS"); res.SetValue("return_msg", "OK"); context.Response.Write(res.ToXml()); context.Response.End(); } }
//回调根据PayResult处理回调 public bool NotifyOper(PayResult result) { //log4net.LogHelper.WriteInfo(this.GetType(), "回调到这里"); //log4net.LogHelper.WriteInfo(this.GetType(), JsonConvert.SerializeObject(result)); if (WxUtils.getAttachValue(result.attach, "from") == "city") { CityMordersBLL citybll = new CityMordersBLL(result); string orderidstr = WxUtils.getAttachValue(result.attach, "orderid"); if (string.IsNullOrEmpty(orderidstr)) { log4net.LogHelper.WriteError(GetType(), new Exception(JsonConvert.SerializeObject(result))); return(false); } int orderid = Convert.ToInt32(orderidstr); CityMorders order = citybll.GetModel(orderid);//citybll.GetModel(" Id=" + orderid + ""); //修改订单支付状态 if (order == null) { Exception ex = new Exception("警报:根据支付单号找不到相关订单!" + " out_trade_no='" + result.out_trade_no + "'"); log4net.LogHelper.WriteError(GetType(), ex); log4net.LogHelper.WriteError(GetType(), new Exception(JsonConvert.SerializeObject(result))); return(false); } if (order.payment_status != 0) { return(false); } if (order.Percent < 0 || order.Percent > 100) { Exception ex = new Exception("警报:出现异常订单!订单提成百分比为:" + order.Percent + " out_trade_no='" + result.out_trade_no + "'"); log4net.LogHelper.WriteError(GetType(), ex); log4net.LogHelper.WriteError(GetType(), new Exception(JsonConvert.SerializeObject(result))); return(false); } citybll.Order = order; //修改order支付商户 order.mch_id = result.mch_id; order.appid = result.appid; citybll.Update(order); switch (result.paytype) { //小程序商城 case (int)ArticleTypeEnum.MiniappGoods: return(citybll.MiniappStoreGoods()); //小程序餐饮 case (int)ArticleTypeEnum.MiniappFoodGoods: return(citybll.MiniappFoodGoods()); //小程序储值 case (int)ArticleTypeEnum.MiniappSaveMoneySet: return(citybll.MiniappSaveMoney()); //小程序砍价 case (int)ArticleTypeEnum.MiniappBargain: return(citybll.MiniappBargainMoney()); //小程序拼团 case (int)ArticleTypeEnum.MiniappGroups: return(citybll.MiniappStoreGroup()); //小程序行业版 case (int)ArticleTypeEnum.MiniappEnt: return(citybll.MiniappEntGoods()); //小程序足浴版 case (int)ArticleTypeEnum.MiniappFootbath: return(citybll.MiniappFootbath()); case (int)ArticleTypeEnum.MiniappMultiStore: return(citybll.MiniappMultiStore()); //小程序专业版积分兑换(微信+积分方式兑换) case (int)ArticleTypeEnum.MiniappExchangeActivity: return(citybll.PayMiniappExchangeActivity()); //小程序同城模拟板 case (int)ArticleTypeEnum.City_StoreBuyMsg: return(citybll.cityBuyMsg()); } } return(false); }