Пример #1
0
        /// <summary>
        /// 公众号支付下单
        /// </summary>
        /// <param name="Model"></param>
        public WeChatPayRespone DoRequest(WeChatPayModel model)
        {
            WeChatPayByJsModel   Model   = model as WeChatPayByJsModel;
            WeChatPayByJsRespone Respone = new WeChatPayByJsRespone();
            var Parameters = new Dictionary <string, string>();

            Model.sNonceStr = WeChatPayHelper.GetRandomStr();//获取随机字符串
            Parameters.Add("appid", Model.sAppId);
            Parameters.Add("mch_id", Model.sMchId);
            Parameters.Add("nonce_str", Model.sNonceStr);
            Parameters.Add("body", Model.sBody);
            Parameters.Add("out_trade_no", Model.sOrderNo);
            Parameters.Add("total_fee", Model.iTotalFee.ToString());
            Parameters.Add("spbill_create_ip", Model.spbill_create_ip);
            Parameters.Add("notify_url", Model.sNotifyUrl);
            Parameters.Add("trade_type", Enum.GetName(typeof(WeChatPayType), Model.sTradeType));
            Parameters.Add("openid", Model.sOpenId);
            if (!string.IsNullOrEmpty(model.attach))
            {
                Parameters.Add("attach", model.attach); //添加商户自定义数据
            }
            //创建签名
            Model.sSign = WeChatPayHelper.CreateSign(Parameters, Model.sPayKey);
            Parameters.Add("sign", Model.sSign);
            string sRequestXmlStr = WeChatPayHelper.InstallXml(Parameters);
            //获取微信响应结果
            string sResponeXmlStr   = WeChatPayHelper.HttpPost(sUrl, sRequestXmlStr, null);
            var    ResponeParameter = WeChatPayHelper.GetDictionaryFromCDATAXml(sResponeXmlStr);

            Respone.sReturnParams = ResponeParameter;
            //判断返回结果
            if (ResponeParameter["return_code"] == "SUCCESS")
            {
                if (WeChatPayHelper.CheckSign(ResponeParameter, Model.sPayKey))
                {
                    if (ResponeParameter["result_code"] == "SUCCESS")
                    {
                        Respone.bSuccess   = true;
                        Respone.sReturnMsg = ResponeParameter["return_msg"];
                        Respone.sPrepayId  = ResponeParameter["prepay_id"];//微信生成的预支付会话标识,用于后续接口调用中使用,该值有效期为2小时
                    }
                    else
                    {
                        Respone.bSuccess   = false;
                        Respone.sReturnMsg = ResponeParameter["err_code_des"];
                    }
                }
                else
                {
                    Respone.bSuccess   = false;
                    Respone.sReturnMsg = "签名验证失败";
                }
            }
            else
            {
                Respone.bSuccess   = false;
                Respone.sReturnMsg = ResponeParameter["return_msg"];
            }
            return(Respone);
        }
Пример #2
0
        /// <summary>
        /// 扫码支付下单
        /// </summary>
        /// <param name="Model"></param>
        public WeChatPayRespone DoRequest(WeChatPayModel model)
        {
            WeChatPayByScanModel   Model   = model as WeChatPayByScanModel;
            WeChatPayByScanRespone Respone = new WeChatPayByScanRespone();
            var Parameters = new Dictionary <string, string>();

            Model.sNonceStr = WeChatPayHelper.GetRandomStr();//获取随机字符串
            Parameters.Add("appid", Model.sAppId);
            Parameters.Add("mch_id", Model.sMchId);
            Parameters.Add("nonce_str", Model.sNonceStr);
            Parameters.Add("body", Model.sBody);
            Parameters.Add("out_trade_no", Model.sOrderNo);
            Parameters.Add("total_fee", Model.iTotalFee.ToString());
            Parameters.Add("spbill_create_ip", Model.spbill_create_ip);
            Parameters.Add("notify_url", Model.sNotifyUrl);
            Parameters.Add("trade_type", Enum.GetName(typeof(WeChatPayType), Model.sTradeType));
            Parameters.Add("product_id", Model.sProductId);
            Parameters.Add("attach", Model.sAttach);//自定义参数
            //创建签名
            Model.sSign = WeChatPayHelper.CreateSign(Parameters, Model.sPayKey);
            Parameters.Add("sign", Model.sSign);
            string sRequestXmlStr = WeChatPayHelper.InstallXml(Parameters);
            //获取微信响应结果
            string sResponeXmlStr   = WeChatPayHelper.HttpPost(sUrl, sRequestXmlStr, null);
            var    ResponeParameter = WeChatPayHelper.GetDictionaryFromCDATAXml(sResponeXmlStr);

            Respone.sReturnParams = ResponeParameter;
            //判断返回结果
            if (ResponeParameter["return_code"] == "SUCCESS")
            {
                if (WeChatPayHelper.CheckSign(ResponeParameter, Model.sPayKey))
                {
                    if (ResponeParameter["result_code"] == "SUCCESS")
                    {
                        Respone.bSuccess   = true;
                        Respone.sReturnMsg = ResponeParameter["return_msg"];
                        Respone.sCodeUrl   = ResponeParameter["code_url"];//返回的支付二维码链接有效期2个小时
                    }
                    else
                    {
                        Respone.bSuccess   = false;
                        Respone.sReturnMsg = ResponeParameter["err_code_des"];
                    }
                }
                else
                {
                    Respone.bSuccess   = false;
                    Respone.sReturnMsg = "签名验证失败";
                }
            }
            else
            {
                Respone.bSuccess   = false;
                Respone.sReturnMsg = ResponeParameter["return_msg"];
            }
            return(Respone);
        }
Пример #3
0
        /// <summary>
        /// 执行微信申请退款
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public WeChatPayRespone DoRequest(WeChatPayModel model)
        {
            WeChatPayRefundModel   Model   = model as WeChatPayRefundModel;
            WeChatPayRefundRespone Respone = new WeChatPayRefundRespone();
            var Parameters = new Dictionary <string, string>();

            Model.sNonceStr = WeChatPayHelper.GetRandomStr();//获取随机字符串
            Parameters.Add("appid", Model.sAppId);
            Parameters.Add("mch_id", Model.sMchId);
            Parameters.Add("nonce_str", Model.sNonceStr);
            Parameters.Add("out_trade_no", Model.sOrderNo);
            Parameters.Add("out_refund_no", Model.sRefundNo);
            Parameters.Add("total_fee", Model.iTotalFee.ToString());   //订单总金额
            Parameters.Add("refund_fee", Model.iRefundFee.ToString()); //退款金额
            //创建签名
            Model.sSign = WeChatPayHelper.CreateSign(Parameters, Model.sPayKey);
            Parameters.Add("sign", Model.sSign);
            string sRequestXmlStr = WeChatPayHelper.InstallXml(Parameters);
            //获取微信响应结果
            string sResponeXmlStr   = WeChatPayHelper.HttpPost(sUrl, sRequestXmlStr, Model, true);//申请退款需要证书
            var    ResponeParameter = WeChatPayHelper.GetDictionaryFromCDATAXml(sResponeXmlStr);

            //判断返回结果
            if (ResponeParameter["return_code"] == "SUCCESS")
            {
                if (WeChatPayHelper.CheckSign(ResponeParameter, Model.sPayKey))
                {     //验证签名正确
                    if (ResponeParameter["result_code"] == "SUCCESS")
                    { //申请退款成功
                        Respone.bSuccess = true;
                    }
                    else
                    {
                        Respone.bSuccess   = false;
                        Respone.sReturnMsg = ResponeParameter["err_code_des"];
                    }
                }
                else
                {
                    //签名验证失败
                    Respone.bSuccess   = false;
                    Respone.sReturnMsg = "签名验证失败";
                }
            }
            else
            {
                Respone.bSuccess   = false;
                Respone.sReturnMsg = ResponeParameter["return_msg"];
            }
            return(Respone);
        }
Пример #4
0
        /// <summary>
        /// 企业付款请求
        /// </summary>
        /// <param name="Model"></param>
        public WeChatPayRespone DoRequest(WeChatPayModel model)
        {
            WeChatPayByEnterpriseModel   Model   = model as WeChatPayByEnterpriseModel;
            WeChatPayByEnterpriseRespone Respone = new WeChatPayByEnterpriseRespone();
            var Parameters = new Dictionary <string, string>();

            Model.sNonceStr = WeChatPayHelper.GetRandomStr();//获取随机字符串
            Parameters.Add("mch_appid", Model.sAppId);
            Parameters.Add("mchid", Model.sMchId);
            Parameters.Add("nonce_str", Model.sNonceStr);
            Parameters.Add("partner_trade_no", Model.sOrderNo);
            Parameters.Add("openid", Model.sOpenId);
            Parameters.Add("check_name", "NO_CHECK");                   //不校验真实姓名
            Parameters.Add("amount", Model.iTotalFee.ToString());       //付款金额
            Parameters.Add("desc", Model.sDescribe);
            Parameters.Add("spbill_create_ip", Model.spbill_create_ip); //该IP同在商户平台设置的IP白名单中的IP没有关联,该IP可传用户端或者服务端的IP。
            //创建签名
            Model.sSign = WeChatPayHelper.CreateSign(Parameters, Model.sPayKey);
            Parameters.Add("sign", Model.sSign);
            string sRequestXmlStr = WeChatPayHelper.InstallXml(Parameters);
            //获取微信响应结果
            string sResponeXmlStr   = WeChatPayHelper.HttpPost(sUrl, sRequestXmlStr, model, true);//需要证书
            var    ResponeParameter = WeChatPayHelper.GetDictionaryFromCDATAXml(sResponeXmlStr);

            Respone.sReturnParams = ResponeParameter;
            //判断返回结果
            if (ResponeParameter["return_code"] == "SUCCESS")
            {
                if (ResponeParameter["result_code"] == "SUCCESS")
                {
                    Respone.bSuccess   = true;
                    Respone.sReturnMsg = ResponeParameter["return_msg"];
                }
                else
                {
                    Respone.bSuccess   = false;
                    Respone.sReturnMsg = ResponeParameter["err_code_des"];
                }
            }
            else
            {
                Respone.bSuccess   = false;
                Respone.sReturnMsg = ResponeParameter["return_msg"];
            }
            return(Respone);
        }
        /// <summary>
        /// Post请求微信系统
        /// </summary>
        /// <param name="sUrl">请求的Url</param>
        /// <param name="PostData">传输的数据</param>
        /// <param name="sCertPath">证书路径</param>
        /// <param name="IsNeedCert">是否需要证书</param>
        /// <returns></returns>
        public static string HttpPost(string sUrl, string PostData, WeChatPayModel Model, bool isUseCert = false)
        {
            byte[] bPostData = System.Text.Encoding.UTF8.GetBytes(PostData);
            string sResult   = string.Empty;

            try
            {
                HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(sUrl);
                webRequest.ProtocolVersion = HttpVersion.Version10;
                webRequest.Timeout         = 30000;
                webRequest.Method          = "POST";
                webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

                //判断是否需要证书
                if (isUseCert)
                {                                                                     //微信退款需要证书
                    string           path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Model.sCertPath);
                    X509Certificate2 cert = new X509Certificate2(path, Model.sMchId); //证书的密码就是商户号
                    webRequest.ClientCertificates.Add(cert);
                }
                if (bPostData != null)
                {
                    Stream postDataStream = webRequest.GetRequestStream();
                    postDataStream.Write(bPostData, 0, bPostData.Length);
                }
                HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
                if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (var zipStream =
                                   new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.GetEncoding(webResponse.CharacterSet)))
                            {
                                sResult = sr.ReadToEnd();
                            }
                        }
                    }
                }
                else if (webResponse.ContentEncoding.ToLower() == "deflate")//如果使用了deflate则先解压
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (var zipStream =
                                   new System.IO.Compression.DeflateStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.GetEncoding(webResponse.CharacterSet)))
                            {
                                sResult = sr.ReadToEnd();
                            }
                        }
                    }
                }
                else
                {
                    using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.UTF8))
                        {
                            sResult = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Info("微信支付相关的请求异常:" + ex.Message);
                logger.Info(ex);
            }
            logger.Info("请求的Url:" + sUrl);
            logger.Info("返回的结果:" + sResult);
            return(sResult);
        }
        /// <summary>
        /// 查询微信支付结果请求
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public WeChatPayRespone DoRequest(WeChatPayModel model)
        {
            WeChatPayQueryModel   Model   = model as WeChatPayQueryModel;
            WeChatPayQueryRespone Respone = new WeChatPayQueryRespone();
            var Parameters = new Dictionary <string, string>();

            Model.sNonceStr = WeChatPayHelper.GetRandomStr();//获取随机字符串
            Parameters.Add("appid", Model.sAppId);
            Parameters.Add("mch_id", Model.sMchId);
            Parameters.Add("transaction_id", Model.sTransactionNo);
            Parameters.Add("out_trade_no", Model.sOrderNo);
            Parameters.Add("nonce_str", Model.sNonceStr);
            //创建签名
            Model.sSign = WeChatPayHelper.CreateSign(Parameters, Model.sPayKey);
            Parameters.Add("sign", Model.sSign);
            string sRequestXmlStr = WeChatPayHelper.InstallXml(Parameters);
            //获取微信响应结果
            string sResponeXmlStr   = WeChatPayHelper.HttpPost(sUrl, sRequestXmlStr, null);
            var    ResponeParameter = WeChatPayHelper.GetDictionaryFromCDATAXml(sResponeXmlStr);

            Respone.sReturnParams = ResponeParameter;
            //判断返回结果
            if (ResponeParameter["return_code"] == "SUCCESS")
            {
                if (WeChatPayHelper.CheckSign(ResponeParameter, Model.sPayKey))
                {
                    if (ResponeParameter["result_code"] == "SUCCESS")
                    {//查询成功
                        if (ResponeParameter["trade_state"] == "SUCCESS")
                        {
                            Respone.bSuccess      = true;
                            Respone.sReturnMsg    = ResponeParameter["return_msg"];
                            Respone.TradeState    = true;//交易状态 支付成功
                            Respone.TradeStateMsg = ResponeParameter["trade_state"];
                        }
                        else
                        {
                            Respone.bSuccess      = true;
                            Respone.sReturnMsg    = ResponeParameter["return_msg"];
                            Respone.TradeState    = false;//交易状态
                            Respone.TradeStateMsg = ResponeParameter["trade_state"];
                        }
                    }
                    else
                    {
                        Respone.bSuccess      = false;
                        Respone.sReturnMsg    = ResponeParameter["err_code_des"];
                        Respone.TradeState    = false;
                        Respone.TradeStateMsg = Respone.sReturnMsg;
                    }
                }
                else
                {
                    //签名验证失败
                    Respone.bSuccess   = false;
                    Respone.sReturnMsg = "签名验证失败";
                    Respone.TradeState = false;
                }
            }
            else
            {
                Respone.bSuccess   = false;
                Respone.sReturnMsg = ResponeParameter["return_msg"];
            }
            return(Respone);
        }