/** * * 检测签名是否正确 * 正确返回true,错误抛异常 */ public bool CheckSign() { //如果没有设置签名,则跳过检测 if (!IsSet("sign")) { WxPayException exx = new WxPayException("WxPayData签名存在但不合法!"); log4net.LogHelper.WriteError(this.GetType(), exx); throw exx; } //如果设置了签名但是签名为空,则抛异常 else if (GetValue("sign") == null || GetValue("sign").ToString() == "") { WxPayException exx = new WxPayException("WxPayData签名存在但不合法!"); log4net.LogHelper.WriteError(this.GetType(), exx); throw exx; } //获取接收到的签名 string return_sign = GetValue("sign").ToString(); //第三方支付需要查询KEY的值 string key = string.Empty; if (IsSet("appid")) { string appid = GetValue("appid").ToString(); PayCenterSetting setting = new PayCenterSettingBLL().GetPayCenterSetting(appid); if (setting != null && setting.Id > 0) { key = setting.Key; } } //在本地计算新的签名 string cal_sign = MakeSign(key); if (cal_sign == return_sign) { return(true); } WxPayException ex = new WxPayException("WxPayData签名验证错误!"); log4net.LogHelper.WriteError(this.GetType(), ex); throw ex;; }
public ActionResult GetData(int typeid, string value) { string sqlwhere = string.Empty; try { switch (typeid) { //case 1: // sqlwhere = "XcxAppid = '" + value + "' or WxAppid='" + value + "'"; // var modellist1 = new PayAppidRelationBLL().GetList(sqlwhere); // return Json(new { code = 1, modellist = modellist1 }, JsonRequestBehavior.AllowGet); // break; //case 2: // sqlwhere = "openId = '" + value + "' or Id='" + value + "'"; // var modellist2 = new C_UserInfoBLL().GetList(sqlwhere); // return Json(new { code = 1, modellist = modellist2 }, JsonRequestBehavior.AllowGet); // break; //case 3: // sqlwhere = "UserID = '" + value + "'"; // var modellist3 = new CityUserCashLogBLL().GetList(sqlwhere, 10, 1, "", "addtime desc"); // return Json(new { code = 1, modellist = modellist3 }, JsonRequestBehavior.AllowGet); // break; //case 4: // var TableName = new C_UserCashBLL().GetTableName(value); // var modellist4 = new C_UserCashBLL().GetListBySql($"select * from {TableName} where UserID={value}"); // return Json(new { code = 1, modellist = modellist4 }, JsonRequestBehavior.AllowGet); // break; case 5: sqlwhere = "BindingId = '" + value + "'"; if (value.Length > 10) { sqlwhere = "Appid = '" + value + "'"; } var modellist5 = new PayCenterSettingBLL().GetList(sqlwhere); return(Json(new { code = 1, modellist = modellist5 }, JsonRequestBehavior.AllowGet)); case 7: var modellist7 = OpenAuthorizerConfigBLL.SingleModel.GetList(); return(Json(new { code = 1, modellist = modellist7 }, JsonRequestBehavior.AllowGet)); break; case 8: var modellist8 = OpenAuthorizerInfoBLL.SingleModel.GetList(); return(Json(new { code = 1, modellist = modellist8 }, JsonRequestBehavior.AllowGet)); break; case 9: var modellist9 = UserXcxTemplateBLL.SingleModel.GetList(); return(Json(new { code = 1, modellist = modellist9 }, JsonRequestBehavior.AllowGet)); break; } } catch (Exception ex) { return(Json(new { code = -1, msg = ex.Message }, JsonRequestBehavior.AllowGet)); } //if (typeid == 1) //{ // string sqlwhere = "XcxAppid = '" + value + "' or WxAppid='" + value + "'"; // var modellist = new PayAppidRelationBLL().GetList(sqlwhere); // return Json(new { code = -1, modellist = modellist }, JsonRequestBehavior.AllowGet); //} //else if (typeid == 2) //{ // string sqlwhere = "openId = '" + value + "' or Id='" + value+"'"; // var modellist = new C_UserInfoBLL().GetList(sqlwhere); // return Json(new { code = -1, modellist = modellist }, JsonRequestBehavior.AllowGet); //} //else if (typeid == 4) //{ // var TableName = new C_UserCashBLL().GetTableName(value); // var modellist = new C_UserCashBLL().GetListBySql($"select * from {TableName} where UserID={value}"); // return Json(new { code = -1, modellist = modellist }, JsonRequestBehavior.AllowGet); //} //else if (typeid == 3) //{ // string sqlwhere = "UserID = '" + value + "'"; // var modellist = new CityUserCashLogBLL().GetList(sqlwhere,10,1,"","addtime desc"); // return Json(new { code = -1, modellist = modellist }, JsonRequestBehavior.AllowGet); //} //else if (typeid == 5) //{ // string sqlwhere = "BindingId = '" + value + "'"; // if(value.Length>10) // { // sqlwhere ="Appid = '" + value + "'"; // } // var modellist = new PayCenterSettingBLL().GetList(sqlwhere); // return Json(new { code = -1, modellist = modellist }, JsonRequestBehavior.AllowGet); //} //else if (typeid == 6) //{ // //var modellist = new opencomponentconfigBLL().getCurrentModel(); // return Json(new { code = -1, modellist = "" }, JsonRequestBehavior.AllowGet); //} return(Json(new { code = -1, msg = "参数异常" }, JsonRequestBehavior.AllowGet)); }
//public ActionResult AddRelation(string wxappid, string xcxappid, int typeid = 0) //{ // try // { // if (string.IsNullOrEmpty(wxappid) || string.IsNullOrEmpty(xcxappid)) // { // return Json(new { code = -1, msg = "appid 不能为空" }); // } // if (typeid == 0) // { // var model = new PayAppidRelationBLL().GetModel($"WxAppid='{wxappid}' and XcxAppid='{xcxappid}'"); // if (model != null) // { // return Json(new { code = -1, msg = "不能重复添加" }); // } // model = new PayAppidRelation(); // model.AddTime = DateTime.Now; // model.WxAppid = wxappid; // model.XcxAppid = xcxappid; // var result = new PayAppidRelationBLL().Add(model); // if (Convert.ToInt32(result) > 0) // { // return Json(new { code = 1, msg = "添加成功" }); // } // return Json(new { code = -1, msg = "添加失败" }); // } // else // { // var model = new PayAppidRelationBLL().GetModel($"WxAppid='{wxappid}' and XcxAppid='{xcxappid}'"); // if (model == null) // { // return Json(new { code = -1, msg = "请先添加" }); // } // model.AddTime = DateTime.Now; // model.WxAppid = wxappid; // model.XcxAppid = xcxappid; // var result = new PayAppidRelationBLL().Update(model); // if (Convert.ToInt32(result) > 0) // { // return Json(new { code = 1, msg = "修改成功" }); // } // return Json(new { code = -1, msg = "修改失败" }); // } // } // catch (Exception ex) // { // return Json(new { code = 0, msg = "操作异常,msg=" + ex.Message }); // } //} public ActionResult AddShanghu(string appid, string mc_id, string mc_key, int typeid = 0) { try { if (string.IsNullOrEmpty(appid)) { return(Json(new { code = -1, msg = "appid 不能为空" })); } if (string.IsNullOrEmpty(mc_id)) { return(Json(new { code = -1, msg = "mc_id 不能为空" })); } if (string.IsNullOrEmpty(mc_key)) { return(Json(new { code = -1, msg = "mc_key 不能为空" })); } if (typeid == 0) { var model = new PayCenterSettingBLL().GetModel($"Appid='{appid}'"); if (model != null) { return(Json(new { code = -1, msg = "不能重复添加" })); } model = new Entity.MiniSNS.PayCenterSetting(); model.Appid = appid; model.Mch_id = mc_id; model.Key = mc_key; model.BindingType = 5; model.Status = 0; var result = new PayCenterSettingBLL().Add(model); if (Convert.ToInt32(result) > 0) { return(Json(new { code = 1, msg = "添加成功" })); } return(Json(new { code = -1, msg = "添加失败" })); } else { var model = new PayCenterSettingBLL().GetModel($"Appid='{appid}'"); if (model == null) { return(Json(new { code = -1, msg = "请先添加" })); } model.Appid = appid; model.Mch_id = mc_id; model.Key = mc_key; model.BindingType = 5; model.Status = 0; var result = new PayCenterSettingBLL().Update(model); if (Convert.ToInt32(result) > 0) { return(Json(new { code = 1, msg = "修改成功" })); } return(Json(new { code = -1, msg = "修改失败" })); } } catch (Exception ex) { return(Json(new { code = 0, msg = "操作异常,msg=" + ex.Message })); } }
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(); } }