public Response GetOpenId(string token, [FromBody] WeChatInfo param) { Response response = new Response(); string[] strAry = token.Split(','); string tmpToken = strAry[0]; string appName = strAry[1]; if (string.IsNullOrEmpty(tmpToken) || !tmpToken.Equals(_token)) { response.code = "404"; response.message = "Invild token"; } else { var data = WeChatPayHelper.GetOpenId(param, appName); if (data == null) { response.code = "500"; response.message = "Get data failed"; } else { response.code = "200"; response.content = data; } } return(response); }
/// <summary> /// 将小程序用户code转换成openid /// </summary> /// <param name="param"></param> /// <returns></returns> public static string GetOpenId(WeChatInfo param, string appName) { WeChatPayFactory factory = new WeChatPayFactory(appName); try { if (param == null) { throw new Exception("Param is null"); } string strResult = factory.GetOpenId(param); if (string.IsNullOrWhiteSpace(strResult)) { throw new Exception("DAL.WeChat.WeChatPayFactory.GetOpenId()==null"); } return(strResult); } catch (Exception ex) { LogHelper.WriteLog(new Log() { message = ex.Message }, "GetOpenId"); return(string.Empty); } }
IWeChatPay IWeChatPay.init(WeChatInfo info) { if (_info == null) { _info = info; } return(this); }
IWeChatSmallApp IWeChatSmallApp.init(WeChatInfo info) { if (_info == null) { _info = info; } return(this);; }
IWeChatGetUser IWeChatGetUser.init(WeChatInfo info) { if (_info == null) { _info = info; } return(this); }
IWeChatWeb IWeChatWeb.init(WeChatInfo info) { if (_info == null) { _info = info; } return(this); }
IWeChatSendMessage IWeChatSendMessage.init(WeChatInfo info) { if (_info == null) { _info = info; } return(this); }
IWeChatGetMessage IWeChatGetMessage.init(WeChatInfo info) { if (_info == null) { _info = info; var urlvalue = _context.Request.QueryString.Value; _params = ModelHelper.urlParameterToDictionary(urlvalue); this.receiveMessages(); } return(this); }
public ActionResult Add(WeChatModel model) { WeChatInfo weChatInfo = new WeChatInfo(); if (ModelState.IsValid) { weChatInfo.name = model.name; weChatInfo.appid = model.appid; weChatInfo.secret = model.secret; WeChats.CreateWeChat(weChatInfo); return(PromptView("公众号添加成功")); } return(View(model)); }
public ActionRes GetOpenId() { HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; WeChatInfo wechatInfo = Helper.GetOpenId(context.Request["js_code"]); System_User systemUser = db.System_User.Where(user => user.WxOpenId == wechatInfo.openid).FirstOrDefault(); if (systemUser != null) { return(ActionRes.Success(systemUser)); } else { return(ActionRes.Success(wechatInfo)); } }
//更新 public ActionResult Edit(int id = -1) { WeChatInfo weChatInfo = WeChats.GetWeChatById(id); if (weChatInfo == null) { return(PromptView("公众号不存在")); } WeChatModel model = new WeChatModel(); model.id = weChatInfo.id; model.name = weChatInfo.name; model.appid = weChatInfo.appid; model.secret = weChatInfo.secret; return(View(model)); }
//获取列表 public static WeChatInfo[] GetWeChatList() { DataTable dt = BonSite.Core.BSData.RDBS.GetWeChatList(); WeChatInfo[] weChatList = new WeChatInfo[dt.Rows.Count]; int index = 0; foreach (DataRow row in dt.Rows) { WeChatInfo weChatInfo = new WeChatInfo(); weChatInfo.id = TypeHelper.ObjectToInt(row["id"]); weChatInfo.name = row["name"].ToString(); weChatInfo.appid = row["appid"].ToString(); weChatInfo.secret = row["secret"].ToString(); weChatList[index] = weChatInfo; index++; } return(weChatList); }
/// <summary> /// 检查充值金额是否符合设定 /// </summary> /// <param name="total_fee"></param> /// <param name="rechargeSetting"></param> /// <returns></returns> private bool CheckRechargeMatched(WeChatInfo param) { string strTotal_fee = (param.total_fee * 1.0 / 100).ToString(); string[] aryRecharge = param.rechargeSetting.Split(','); if (string.Empty.Equals(aryRecharge[aryRecharge.Length - 1])) { return(true); } foreach (string rc in aryRecharge) { if (rc.Equals(strTotal_fee)) { return(true); } } return(false); }
/// <summary> /// 获取微信的AccessToken /// </summary> /// <param name="info"></param> /// <returns></returns> public static string getAccessToken(WeChatInfo info) { var key = $"WeChatApplicationWithAccessToken_{info.appid}"; var url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={info.appid}&secret={info.appsecret}"; //获取缓存中的值 var tokenCache = CacheHelper.get <string>(key); //如果没有,则重新远程获取并加入缓存 if (string.IsNullOrEmpty(tokenCache)) { //远程拉取字符串 var str = getFormWechatService(url); //反序列化 AccessToken token = JsonHelper.jsonToModel <AccessToken>(str); tokenCache = token.access_token; //存入缓存,绝对过期时间 CacheHelper.add(key, token.access_token, token.expires_in); } return(tokenCache); }
/// <summary> /// 获取微信的JsApiTicket /// </summary> /// <param name="info"></param> /// <returns></returns> public static string getJsApi_Ticket(WeChatInfo info) { var key = $"WeChatApplicationWithJsApiTicket_{info.appid}"; var access_token = getAccessToken(info); var url = $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={access_token}&type=jsapi"; //获取缓存中的值 var apiCache = CacheHelper.get <string>(key); //如果没有,则重新远程获取并加入缓存 if (string.IsNullOrEmpty(apiCache)) { //远程拉取字符串 var str = getFormWechatService(url); //反序列化 JsApiTicket ticket = JsonHelper.jsonToModel <JsApiTicket>(str); apiCache = ticket.ticket; //存入缓存,绝对过期时间 CacheHelper.add(key, apiCache, ticket.expires_in); } return(apiCache); }
/// <summary> /// 创建预支付订单 /// </summary> /// <param name="param"></param> /// <returns></returns> public static WeChatResult UnifiedOrder(WeChatInfo param, string appName) { WeChatPayFactory factory = new WeChatPayFactory(appName); try { if (param == null) { throw new Exception("WeChatInfo is null"); } WeChatResult wxResult = factory.UnifiedOrder(param); //if (wxResult == null) throw new Exception("DAL.WeChat.WeChatPayFactory.UnifiedOrder()==0"); return(wxResult); } catch (Exception ex) { LogHelper.WriteLog(new Log() { message = ex.Message }, "UnifiedOrder"); throw ex; } }
public ActionResult Edit(WeChatModel model, int id) { WeChatInfo weChatInfo = WeChats.GetWeChatById(id); if (weChatInfo == null) { return(PromptView("公众号不存在")); } if (ModelState.IsValid) { weChatInfo.name = model.name; weChatInfo.appid = model.appid; weChatInfo.secret = model.secret; weChatInfo.id = model.id; WeChats.UpdateWeChat(weChatInfo); AddAdminOperateLog("修改公众号", "修改公众号,公众号ID为:" + id); return(PromptView("公众号修改成功")); } return(View(model)); }
//更新 public static void UpdateWeChat(WeChatInfo weChatInfo) { BonSite.Data.WeChat.UpdateWeChat(weChatInfo); BonSite.Core.BSCache.Remove(CacheKeys.SITE_WECHAT_LIST); }
//更新 public static void UpdateWeChat(WeChatInfo weChatInfo) { BonSite.Core.BSData.RDBS.UpdateWeChat(weChatInfo); }
//增加 public static int CreateWeChat(WeChatInfo weChatInfo) { return(BonSite.Core.BSData.RDBS.CreateWeChat(weChatInfo)); }
/// <summary> /// 创建预支付订单 /// </summary> /// <param name="param"></param> /// <returns></returns> public WeChatResult UnifiedOrder(WeChatInfo param) { WeChatResult wxResult = new WeChatResult(); // 回调地址 string notifyUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["notifyUrl"].ToString(); try { // 检查服务端是否处于调整中 if (CheckServerMantain(param.costCenterCode)) { wxResult.isMantain = true; return(wxResult); } // 检查充值金额和设置是否符合 wxResult.matched = true; if (!CheckRechargeMatched(param)) { wxResult.matched = false; return(wxResult); } string openid = param.openId; string ordertime = SalesOrder.Common.convertDateTime(DateTime.Now.ToString()); bool isTestUser = (new RechargeFactory()).GetUserInfo(openid).isTestUser; // 本地交易号前三位(用于在商户平台上区分支付场景,回调时手动去除不存数据库) string fcode = wcf.paymentCode; int len = fcode.Length; /***统一下单1***/ WxPayData data = new WxPayData(wcf); data.SetValue("body", param.costCenterCode + "-餐卡充值"); data.SetValue("attach", param.appName); data.SetValue("out_trade_no", fcode + WxPayApi.GenerateOutTradeNo(param.costCenterCode)); //data.SetValue("total_fee", param.total_fee); data.SetValue("total_fee", isTestUser ? 1 : param.total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(5).ToString("yyyyMMddHHmmss")); //data.SetValue("goods_tag", "test"); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", openid); data.SetValue("notify_url", notifyUrl); WriteLog(data.ToJson().ToString()); if (param.total_fee > 0) { WxPayData result = WxPayApi.UnifiedOrder(wcf, data); WriteLog(result.ToJson().ToString()); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } wxResult.prepay_id = result.GetValue("prepay_id").ToString(); wxResult.paySign = result.GetValue("sign").ToString(); wxResult.nonceStr = result.GetValue("nonce_str").ToString(); } /***订单写入本地***/ string st = data.GetValue("out_trade_no").ToString(); WxOrder order = new WxOrder { appName = param.appName, type = param.type, cardId = param.cardId, out_trade_no = st.Substring(len, st.Length - len), openid = data.GetValue("openid").ToString(), attach = data.GetValue("attach").ToString(), coupons = param.coupons == null || !param.coupons.Any() ? null : param.coupons.SelectMany(q => q).Where(q => !string.IsNullOrWhiteSpace(q)).GroupBy(q => q) .Select(q => new Coupon { price = int.Parse(q.Key), qty = q.Count() }).ToList(), total_fee = int.Parse(data.GetValue("total_fee").ToString()), time_start = data.GetValue("time_start").ToString(), time_expire = data.GetValue("time_expire").ToString() }; WxOrderFactory wof = new WxOrderFactory(); /***本地没有写成功的话直接返回NULL***/ if (wof.CreatePayOrder(order) <= 0) { return(null); } return(wxResult); } catch (Exception e) { throw e; } }
/// <summary> /// 将小程序用户code转换成openid /// </summary> /// <param name="param"></param> /// <returns></returns> public string GetOpenId(WeChatInfo param) { string openid = WxPayApi.GetOpenId(wcf, param.code); return(openid); }