Ejemplo n.º 1
0
        //微信支付
        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()));
        }
Ejemplo n.º 2
0
        //微信支付
        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()));
        }