public ResultObj <PayStateModel> GetUrl(string m, string code, string retBack = "") { //var log = LogManager.GetLogger("FycnApi", "wechat"); //log.Info("mmmmmmmmmmmmmmmm:"+m); string url = string.Empty; //KeyJsonModel keyJsonInfo = PayHelper.AnalizeKey(k); IPay _ipay = new PayService(); WxPayConfig payConfig = _ipay.GenerateConfigModelWByClientId(m); PayStateModel payState = new PayStateModel(); if (string.IsNullOrEmpty(payConfig.APPID)) { payState.RequestState = "2"; payState.ProductJson = ""; payState.RequestData = ""; return(Content(payState)); } PayModel payInfo = new PayModel(); //JsApi.payInfo = new PayModel(); payInfo.k = m; JsApi jsApi = new JsApi(); string backUrl = "/wechat.html?clientId=" + m; if (!string.IsNullOrEmpty(retBack)) { backUrl = backUrl + HttpUtility.UrlDecode(retBack); } jsApi.GetOpenidAndAccessToken(code, payConfig, payInfo, backUrl, "snsapi_userinfo"); if (string.IsNullOrEmpty(payInfo.openid)) { payState.RequestState = "0"; payState.ProductJson = ""; payState.RequestData = payInfo.redirect_url; return(Content(payState)); } //根据code 返回access_token //string urlAcess = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", payConfig.APPID,payConfig.APPSECRET,code); //string jsonResult = HttpService.Get(urlAcess); //log.Info("access_token:" + jsonResult); //Dictionary<string,string> dicAcess = JsonHandler.GetObjectFromJson<Dictionary<string,string>>(jsonResult); string accessToken = payInfo.access_token;//dicAcess["access_token"]; //取授权用户信息 string urlUserInfo = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", accessToken, payInfo.openid); string jsonUserInfo = HttpService.Get(urlUserInfo); payState.RequestState = "1"; payState.ProductJson = jsonUserInfo; payState.RequestData = ""; IWechat iwechat = new WechatService(); WechatMemberModel memberInfo = new WechatMemberModel(); memberInfo.ClientId = m; memberInfo.OpenId = payInfo.openid; List <WechatMemberModel> lstMemberInfo = iwechat.IsExistMember(memberInfo); if (lstMemberInfo.Count == 0) { WechatMemberModel createMemberInfo = JsonHandler.GetObjectFromJson <WechatMemberModel>(jsonUserInfo); createMemberInfo.ClientId = m; iwechat.CreateMember(createMemberInfo); } //if(iwechat.IsExistMember(memberInfo)) //log.Info("test"); return(Content(payState)); }
//微信支付 public ResultObj <PayStateModel> GetDataW(string k, string code) { bool isPayByTunnel = true; try { //解码机器传过来的key值 //解析k值 KeyJsonModel keyJsonInfo = new PayHelper().AnalizeKey(k); RedisHelper redisHelper = new RedisHelper(0); if (!redisHelper.KeyExists(keyJsonInfo.m)) { PayStateModel payStateNull = new PayStateModel(); return(Content(payStateNull, ResultCode.Success, "机器不在线", new Pagination { })); } IPay _ipay = new PayService(); //移动支付赋值 WxPayConfig payConfig = _ipay.GenerateConfigModelW(keyJsonInfo.m); JsApi jsApi = new JsApi(); PayModel payInfo = new PayModel(); //JsApi.payInfo = new PayModel(); payInfo.k = k; //生成code 根据code取微信支付的openid和access_token jsApi.GetOpenidAndAccessToken(code, payConfig, payInfo, "/m.html?k=" + payInfo.k, ""); PayStateModel payState = new PayStateModel(); if (string.IsNullOrEmpty(payInfo.openid)) { payState.RequestState = "0"; payState.ProductJson = ""; payState.RequestData = payInfo.redirect_url; return(Content(payState)); } //JSAPI支付预处理 if (string.IsNullOrEmpty(keyJsonInfo.m) || keyJsonInfo.t.Count == 0) { payState.RequestState = "2"; payState.ProductJson = ""; payState.RequestData = ""; return(Content(payState)); } //生成交易号 payInfo.trade_no = new PayHelper().GeneraterTradeNo(); //取商品信息 decimal totalFee = 0; string productNames = string.Empty; List <ProductModel> lstProduct = new List <ProductModel>(); if (keyJsonInfo.t.Count > 0 && keyJsonInfo.t[0].tid.Length > 10) { isPayByTunnel = false; } //按货道付款 if (isPayByTunnel) { lstProduct = _ipay.GetProducInfo(keyJsonInfo.m, keyJsonInfo.t); //遍历商品 foreach (ProductModel productInfo in lstProduct) { var tunnelInfo = (from m in keyJsonInfo.t where m.tid == productInfo.TunnelId select m).ToList <KeyTunnelModel>(); if (tunnelInfo.Count > 0) { productInfo.Num = string.IsNullOrEmpty(tunnelInfo[0].n) ? "1" : tunnelInfo[0].n; totalFee = totalFee + Convert.ToInt32(productInfo.Num) * Convert.ToDecimal(productInfo.UnitW); productNames = productNames + productInfo.WaresName + ","; productInfo.TradeNo = payInfo.trade_no; tunnelInfo[0].p = productInfo.UnitW; tunnelInfo[0].wid = productInfo.WaresId; } } } else { lstProduct = _ipay.GetProducInfoByWaresId(keyJsonInfo.m, keyJsonInfo.t); //遍历商品 foreach (ProductModel productInfo in lstProduct) { var tunnelInfo = (from m in keyJsonInfo.t where m.tid == productInfo.WaresId select m).ToList <KeyTunnelModel>(); if (tunnelInfo.Count > 0) { productInfo.Num = string.IsNullOrEmpty(tunnelInfo[0].n) ? "1" : tunnelInfo[0].n; totalFee = totalFee + Convert.ToInt32(productInfo.Num) * Convert.ToDecimal(productInfo.UnitW); productNames = productNames + productInfo.WaresName + ","; productInfo.TradeNo = payInfo.trade_no; tunnelInfo[0].p = productInfo.UnitW; tunnelInfo[0].wid = productInfo.WaresId; tunnelInfo[0].tid = productInfo.TunnelId; } } } payInfo.product_name = productNames.Length > 25 ? productNames.Substring(0, 25) : productNames; //string total_fee = "1"; //检测是否给当前页面传递了相关参数 // 1.先取购买商品的详情返回给用户 并缓存到页面 2.支付成功后跳转到支付结果页并把缓存数据插入到销售记录表 //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 // jsApiPay.openid = openid; payInfo.total_fee = Convert.ToInt32((totalFee * 100)); payInfo.jsonProduct = JsonHandler.GetJsonStrFromObject(keyJsonInfo, false); //写入交易中转 /* * RedisHelper helper = new RedisHelper(0); * * helper.StringSet(JsApi.payInfo.trade_no.Trim(), JsApi.payInfo.jsonProduct, new TimeSpan(0, 10, 30)); */ // FileHandler.WriteFile("data/", JsApi.payInfo.trade_no + ".wa", JsApi.payInfo.jsonProduct); WxPayData unifiedOrderResult = jsApi.GetUnifiedOrderResult(payInfo, payConfig); // Log.Write("GetDataW", "step step"); string wxJsApiParam = jsApi.GetJsApiParameters(payConfig, payInfo);//获取H5调起JS API参数 payState.RequestState = "1"; payState.ProductJson = JsonHandler.GetJsonStrFromObject(lstProduct, false); payState.RequestData = wxJsApiParam; return(Content(payState)); } catch (Exception ex) { PayStateModel payStateError = new PayStateModel(); payStateError.RequestState = "3"; payStateError.RequestData = ex.Message; return(Content(payStateError)); } return(Content(new PayStateModel())); }