Example #1
0
        private WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6)
        {
            string url = WrapConfig.UnifiedorderUrl;

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no"))
            {
                throw new Exception("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!inputObj.IsSet("body"))
            {
                throw new Exception("缺少统一支付接口必填参数body!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new Exception("缺少统一支付接口必填参数total_fee!");
            }
            else if (!inputObj.IsSet("trade_type"))
            {
                throw new Exception("缺少统一支付接口必填参数trade_type!");
            }

            //JSAPI关联参数
            if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
            {
                throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
            }

            //回调url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", WrapConfig.WeChatCallBackUrl);
            }

            //支付场景

            inputObj.SetValue("appid", BasicConfig.Wechat_PayaAppId); //公众账号ID
            inputObj.SetValue("mch_id", BasicConfig.WeChat_MchId);    //商户号
            inputObj.SetValue("spbill_create_ip", "8.8.8.8");         //终端ip
            inputObj.SetValue("nonce_str", GenerateNonceStr());       //随机字符串
            string trade_type = inputObj.GetValue("trade_type").ToString();
            string keyvalue   = BasicConfig.WeChat_AppKey;

            inputObj.SetValue("sign", inputObj.MakeSign(trade_type, keyvalue)); //签名

            string xml = inputObj.ToXml();

            Log.Info("支付请求参数xml:" + xml);

            string response = WHttpUtil.Post(xml, url, false, timeOut);

            WeChatPayData result = new WeChatPayData();

            result.LoadXml(response, trade_type, keyvalue);

            return(result);
        }
Example #2
0
        private WeChatPayData UnifiedOrder(WeChatPayData inputObj, int timeOut = 6)
        {
            try
            {
                string url = WrapConfig.UnifiedorderUrl;

                //检测必填参数
                if (!inputObj.IsSet("out_trade_no"))
                {
                    throw new Exception("缺少统一支付接口必填参数out_trade_no!");
                }
                else if (!inputObj.IsSet("body"))
                {
                    throw new Exception("缺少统一支付接口必填参数body!");
                }
                else if (!inputObj.IsSet("total_fee"))
                {
                    throw new Exception("缺少统一支付接口必填参数total_fee!");
                }
                else if (!inputObj.IsSet("trade_type"))
                {
                    throw new Exception("缺少统一支付接口必填参数trade_type!");
                }

                //JSAPI关联参数
                if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
                {
                    throw new Exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
                }

                //NATIVE关联参数
                if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
                {
                    throw new Exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
                }

                //回调url
                if (!inputObj.IsSet("notify_url"))
                {
                    throw new Exception("统一支付接口中,缺少必填参数notify_url!trade_type为JSAPI时,notify_url为必填参数!");
                }

                //支付场景
                string trade_type = inputObj.GetValue("trade_type").ToString();
                string keyvalue   = "";


                if (trade_type == "JSAPI")
                {
                    inputObj.SetValue("appid", BasicConfig.Sprogram_AppId); //公众账号ID
                    inputObj.SetValue("mch_id", BasicConfig.WeChat_MchId);  //商户号

                    if (trade_type == "JSAPI")
                    {
                        keyvalue = BasicConfig.WeChat_AppKey;
                    }
                }

                inputObj.SetValue("nonce_str", GenerateNonceStr());                 //随机字符串
                inputObj.SetValue("sign", inputObj.MakeSign(trade_type, keyvalue)); //签名

                string xml = inputObj.ToXml();

                //Log.Info("xml:" + xml);

                string response = WHttpUtil.Post(xml, url, false, timeOut);

                WeChatPayData result = new WeChatPayData();

                result.LoadXml(response, trade_type, keyvalue);

                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }