public ResultObj <PayStateModel> GetWeixinJsConfig(string clientId) { var log = LogManager.GetLogger("FycnApi", "wechat"); string url = string.Empty; PayStateModel payState = new PayStateModel(); //KeyJsonModel keyJsonInfo = PayHelper.AnalizeKey(k); try { IPay _ipay = new PayService(); WxPayConfig payConfig = _ipay.GenerateConfigModelWByClientId(clientId); if (string.IsNullOrEmpty(payConfig.APPID)) { payState.RequestState = "2"; payState.ProductJson = ""; payState.RequestData = ""; return(Content(payState)); } RedisHelper rh = new RedisHelper(4); string ticket = rh.StringGet(clientId + "-ticket"); if (string.IsNullOrEmpty(ticket)) { string urlAcess = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", payConfig.APPID, payConfig.APPSECRET); string jsonResult = HttpService.Get(urlAcess); log.Info("access_token:" + jsonResult); Dictionary <string, string> dicAcess = JsonHandler.GetObjectFromJson <Dictionary <string, string> >(jsonResult); string urlTicket = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={0}", dicAcess["access_token"]); string jsonTicket = HttpService.Get(urlTicket); log.Info("ticket:" + jsonTicket); Dictionary <string, string> dicTicket = JsonHandler.GetObjectFromJson <Dictionary <string, string> >(jsonTicket); if (dicTicket["errmsg"] == "ok") { ticket = dicTicket["ticket"]; rh.StringSet(clientId + "-ticket", dicTicket["ticket"], new TimeSpan(1, 50, 50)); } } payState.RequestState = "1"; payState.ProductJson = ""; string retJson = MakeJsSign(payConfig.APPID, ticket); log.Info("result:" + retJson); payState.RequestData = retJson; } catch (Exception ex) { } return(Content(payState)); }
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())); }
//微信支付 public ResultObj <PayStateModel> PostDataW(string clientId, string openId, string privilegeIds, string selfChosen, [FromBody] List <ProductPayModel> lstProductPay) { var log = LogManager.GetLogger("FycnApi", "wechat"); try { IPay _ipay = new PayService(); //移动支付赋值 WxPayConfig payConfig = _ipay.GenerateConfigModelWByClientId(clientId); payConfig.NOTIFY_URL = PathConfig.NotidyAddr + "/Wechat/PostPayResultW"; //结果通知方法 JsApi jsApi = new JsApi(); PayModel payInfo = new PayModel(); payInfo.openid = openId; //JsApi.payInfo = new PayModel(); //生成code 根据code取微信支付的openid和access_token //jsApi.GetOpenidAndAccessToken(code, payConfig, payInfo, "/wechat.html#/pay?clientId=" + clientId, ""); PayStateModel payState = new PayStateModel(); //JSAPI支付预处理 //string result = HttpService.Get(payInfo.redirect_url); //生成交易号 payInfo.trade_no = new PayHelper().GeneraterTradeNo(); payInfo.jsonProduct = clientId; //取商品信息 decimal totalFee = 0; string productNames = string.Empty; List <ProductListModel> lstProduct = new List <ProductListModel>(); IWechat _iwechat = new WechatService(); string waresId = string.Empty; string waresGroupId = string.Empty; foreach (ProductPayModel productInfo in lstProductPay) { waresId = waresId + productInfo.WaresId + ","; } lstProduct = _iwechat.GetProdcutAndGroupList(waresId.TrimEnd(','), waresGroupId.TrimEnd(',')); //遍历商品 foreach (ProductListModel productInfo in lstProduct) { var productPay = (from m in lstProductPay where m.WaresId == productInfo.WaresId select m).ToList <ProductPayModel>(); if (productPay.Count > 0) { totalFee = totalFee + Convert.ToInt32(productPay[0].Number) * Convert.ToDecimal(productInfo.WaresDiscountUnitPrice == 0 ? productInfo.WaresUnitPrice: productInfo.WaresDiscountUnitPrice); productNames = productNames + productInfo.WaresName + ","; productPay[0].TradeNo = payInfo.trade_no; } } payInfo.product_name = productNames.Length > 25 ? productNames.Substring(0, 25) : productNames; payState.ProductJson = JsonHandler.GetJsonStrFromObject(lstProductPay, false); /*******************优惠券信息**********************/ PrivilegeMemberRelationModel privilegeInfo = new PrivilegeMemberRelationModel(); privilegeInfo.ClientId = clientId; privilegeInfo.MemberId = openId; if (totalFee > 0.01M) { List <PrivilegeMemberRelationModel> lstPrivilege = new List <PrivilegeMemberRelationModel>(); if (string.IsNullOrEmpty(privilegeIds) && string.IsNullOrEmpty(selfChosen)) { lstPrivilege = _iwechat.GetCanUsePrivilege(privilegeInfo, privilegeIds, ref totalFee, lstProductPay); } else if (!string.IsNullOrEmpty(privilegeIds)) { lstPrivilege = _iwechat.GetChosenPrivilege(privilegeInfo, privilegeIds, ref totalFee, lstProductPay); } log.Info("ddddd" + lstPrivilege.Count); if (lstPrivilege.Count > 0) { string[] lstStr = lstPrivilege.Select(m => m.Id).ToArray(); if (string.IsNullOrEmpty(privilegeIds)) { payInfo.jsonProduct = payInfo.jsonProduct + "~" + string.Join(",", lstStr); } else { payInfo.jsonProduct = payInfo.jsonProduct + "~" + privilegeIds; } payState.PrivilegeJson = JsonHandler.GetJsonStrFromObject(lstPrivilege, false); } } //string total_fee = "1"; //检测是否给当前页面传递了相关参数 // 1.先取购买商品的详情返回给用户 并缓存到页面 2.支付成功后跳转到支付结果页并把缓存数据插入到销售记录表 //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 // jsApiPay.openid = openid; /* * decimal privilegeMoney = 0; * int weixinMoney = 0; * if (lstPrivilege.Count > 0) * { * if (lstPrivilege[0].Money>0) * { * privilegeMoney = lstPrivilege[0].Money; * weixinMoney = Convert.ToInt32(((totalFee - privilegeMoney) * 100)); * } * else * { * privilegeMoney = lstPrivilege[0].Discount; * weixinMoney = Convert.ToInt32(((totalFee) * 100)* (privilegeMoney/10)); * } * * } * else * { */ int weixinMoney = Convert.ToInt32((totalFee) * 100); //} payInfo.total_fee = (weixinMoney < 0 ? 1 : weixinMoney); //payInfo.jsonProduct = JsonHandler.GetJsonStrFromObject(keyJsonInfo, false); payState.TotalMoney = (totalFee < 0 ? Convert.ToDecimal(0.01) : totalFee); //写入交易中转 if (payInfo.total_fee == 0) { payState.RequestState = "2"; payState.RequestData = ""; return(Content(payState)); } RedisHelper helper = new RedisHelper(0); helper.StringSet(payInfo.trade_no.Trim(), JsonHandler.GetJsonStrFromObject(lstProductPay, false), 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.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())); }
//对k进行解码 k格式:{"m":"ABC123456789","t":[{"tid":"1-2","n":3},{"tid":"1-3","n":2}]} //支付宝支付 public ResultObj <PayStateModel> GetDataA(string k, bool isPayByTunnel = true) { PayStateModel payStateModel = new PayStateModel(); ////////////////////////////////////////////请求参数//////////////////////////////////////////// //解析k值 KeyJsonModel keyJsonInfo = new PayHelper().AnalizeKey(k); if (string.IsNullOrEmpty(keyJsonInfo.m) || keyJsonInfo.t.Count == 0) { payStateModel.RequestState = "2"; payStateModel.ProductJson = ""; payStateModel.RequestData = ""; return(Content(payStateModel)); } RedisHelper redisHelper = new RedisHelper(0); if (!redisHelper.KeyExists(keyJsonInfo.m)) { return(Content(payStateModel, ResultCode.Success, "机器不在线", new Pagination { })); } IPay _ipay = new PayService(); //移动支付赋值 Config config = _ipay.GenerateConfigModelA(keyJsonInfo.m); //生成交易号 string out_trade_no = new PayHelper().GeneraterTradeNo(); //取商品信息 decimal totalFee = 0; string productNames = string.Empty; List <ProductModel> lstProduct = new List <ProductModel>(); //按货道付款 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.UnitA); productNames = productNames + productInfo.WaresName + ","; productInfo.TradeNo = out_trade_no; tunnelInfo[0].p = productInfo.UnitA; 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.UnitA); productNames = productNames + productInfo.WaresName + ","; productInfo.TradeNo = out_trade_no; tunnelInfo[0].p = productInfo.UnitA; tunnelInfo[0].wid = productInfo.WaresId; tunnelInfo[0].tid = productInfo.TunnelId; } } } //订单名称,必填 string subject = productNames.Length > 25 ? productNames.Substring(0, 25) : productNames; //付款金额,必填 string total_fee = totalFee.ToString(); //收银台页面上,商品展示的超链接,必填 string show_url = ""; //商品描述,可空 string body = JsonHandler.GetJsonStrFromObject(keyJsonInfo, false); //写入交易中转 //RedisHelper helper = new RedisHelper(0); var log = LogManager.GetLogger("FycnApi", "tradeNo"); //helper.StringSet(out_trade_no.Trim(), body,new TimeSpan(0,10,30)); // FileHandler.WriteFile("data/", out_trade_no + ".wa", body); //////////////////////////////////////////////////////////////////////////////////////////////// //把请求参数打包成数组 /*********************老接口注释 2017年11月16日********************/ /* * SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>(); * sParaTemp.Add("partner", Config.partner); * sParaTemp.Add("seller_id", Config.seller_id); * sParaTemp.Add("_input_charset", Config.input_charset.ToLower()); * sParaTemp.Add("service", Config.service); * sParaTemp.Add("payment_type", Config.payment_type); * sParaTemp.Add("notify_url", Config.notify_url); * sParaTemp.Add("return_url", Config.return_url); * sParaTemp.Add("out_trade_no", out_trade_no); * sParaTemp.Add("subject", subject); * sParaTemp.Add("total_fee", total_fee); * sParaTemp.Add("show_url", show_url); * sParaTemp.Add("app_pay","Y");//启用此参数可唤起钱包APP支付。 * sParaTemp.Add("body", subject); * //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.2Z6TSk&treeId=60&articleId=103693&docType=1 * //如sParaTemp.Add("参数名","参数值"); * * //建立请求 * string sHtmlText = Config.GateWay+Submit.BuildRequestParaToString(sParaTemp, Encoding.UTF8); * * payStateModel.ProductJson = JsonHandler.GetJsonStrFromObject(lstProduct, false); * payStateModel.RequestData = sHtmlText; * payStateModel.RequestState = "1"; * * return Content(payStateModel); */ /**********************支付宝新接口添加**************************/ DefaultAopClient client = new DefaultAopClient(config.new_gatewayUrl, config.new_app_id, config.private_key, "json", "1.0", config.new_sign_type, config.alipay_public_key, config.new_charset, false); AlipayTradeWapPayModel model = new AlipayTradeWapPayModel(); model.Body = subject; model.Subject = subject; model.TotalAmount = total_fee; model.OutTradeNo = out_trade_no; model.PassbackParams = body; log.Info("99999-" + body); model.TimeoutExpress = "1m"; model.ProductCode = ""; model.QuitUrl = ""; AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); // 设置支付完成同步回调地址 request.SetReturnUrl(config.return_url); // 设置支付完成异步通知接收地址 request.SetNotifyUrl(config.notify_url); // 将业务model载入到request request.SetBizModel(model); AlipayTradeWapPayResponse response = null; try { response = client.pageExecute(request, null, "post"); response.OutTradeNo = out_trade_no; payStateModel.ProductJson = JsonHandler.GetJsonStrFromObject(lstProduct, false); payStateModel.RequestData = response.Body; payStateModel.RequestState = "1"; return(Content(payStateModel)); } catch (Exception exp) { throw exp; } //return Content(new PayStateModel()); }