Ejemplo n.º 1
0
        private static void WriteCommandToLogger(int No, string CommandString)
        {
            if (String.IsNullOrEmpty(isWriteCommandToLogger))
            {
                isWriteCommandToLogger = System.Configuration.ConfigurationManager.AppSettings["isWriteCommandToLogger"];

                if (String.IsNullOrEmpty(isWriteCommandToLogger))
                {
                    isWriteCommandToLogger = "False";
                }
            }

            if (isWriteCommandToLogger.Trim(new char[] { ' ', ' ', '\t', '\r', '\n', '\v', '\f' }).ToLower() == "true")
            {
                if (log == null)
                {
                    log = new Shove._IO.Log("DataCommand");
                }

                log.Write(No.ToString() + ", " + CommandString);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 微信接口通知处理函数
        /// </summary>
        /// <param name="HandleBusiness">委托函数</param>
        /// <param name="errorDescription">返回的错误描述</param>
        /// <returns></returns>
        public static bool Handle(DelegateHandleBusiness HandleBusiness, ref string errorDescription)
        {
            errorDescription = string.Empty;
            Shove._IO.Log log = new Shove._IO.Log("WeixinShanghu");

            #region Check Agrament

            Stream stream = HttpContext.Current.Request.InputStream;

            if (stream.Length < 1)
            {
                errorDescription = "数据流为空";
                return(false);
            }

            string requestContent = "";
            byte[] b = new byte[stream.Length];
            stream.Read(b, 0, (int)stream.Length);
            requestContent = System.Text.Encoding.UTF8.GetString(b);

            if (string.IsNullOrEmpty(requestContent))
            {
                errorDescription = "数据流为空";
                return(false);
            }

            log.Write("微信支付通知数据:" + requestContent);

            if (string.IsNullOrEmpty(Utility.AppID))
            {
                errorDescription = "发生错误:appid为null,请查看页面的Load事件是否调用了Utility.InitializePayConfig()方法";
                return(false);
            }

            if (string.IsNullOrEmpty(Utility.MchID))
            {
                errorDescription = "发生错误:mchid为null,请查看页面的Load事件是否调用了Utility.InitializePayConfig()方法";
                return(false);
            }

            if (string.IsNullOrEmpty(Utility.PayKey))
            {
                errorDescription = "发生错误:paykey为null,请查看页面的Load事件是否调用了Utility.InitializePayConfig()方法";
                return(false);
            }

            /*
             *  返回 XMl 示例
             *<xml>
             *   <appid><![CDATA[wxdf9f01a2ae215175]]></appid>
             *   <attach><![CDATA[Pay_Data]]></attach>
             *   <bank_type><![CDATA[CFT]]></bank_type>
             *   <cash_fee><![CDATA[1]]></cash_fee>
             *   <fee_type><![CDATA[CNY]]></fee_type>
             *   <is_subscribe><![CDATA[Y]]></is_subscribe>
             *   <mch_id><![CDATA[1243322702]]></mch_id>
             *   <nonce_str><![CDATA[bda67e73dbf446498c87717ccaa2d4dc]]></nonce_str>
             *   <openid><![CDATA[obIe-jsl8G_M36uOMV-qBgKSnFGo]]></openid>
             *   <out_trade_no><![CDATA[20150909110449679]]></out_trade_no>
             *   <result_code><![CDATA[SUCCESS]]></result_code>
             *   <return_code><![CDATA[SUCCESS]]></return_code>
             *   <sign><![CDATA[2FBEAF62577AADAC3B9C841BAB2539E6]]></sign>
             *   <time_end><![CDATA[20150909111319]]></time_end>
             *   <total_fee>1</total_fee>
             *   <trade_type><![CDATA[NATIVE]]></trade_type>
             *   <transaction_id><![CDATA[1003250181201509090826253035]]></transaction_id>
             *</xml>
             */

            #endregion

            // 解析
            System.Xml.XPath.XPathDocument xmldoc = null;

            try
            {
                xmldoc = new System.Xml.XPath.XPathDocument(new System.IO.StringReader(requestContent));
            }
            catch (Exception ex)
            {
                errorDescription = "解析通知数据 XML 错误。";
                log.Write("解析通知数据xml错误。" + ex.ToString());

                return(false);
            }

            System.Xml.XPath.XPathNavigator nav = xmldoc.CreateNavigator();
            string appid, attach, bankType, cashFee, feeType, isSubscribe, mchId, nonceStr, openid, outTradeNo,
                   resultCode, returnCode, timeEnd, TotalFee, TradeType, TransactionId, sign;

            try
            {
                resultCode = nav.SelectSingleNode("xml/result_code").Value;
                returnCode = nav.SelectSingleNode("xml/return_code").Value;
            }
            catch (Exception ex)
            {
                errorDescription = "解析通知数据 XML 异常。";
                log.Write("解析通知数据xml异常。" + ex.ToString());

                return(false);
            }

            if (resultCode.ToUpper() != "SUCCESS")
            {
                errorDescription = "发生错误:请求微信网关成功,但微信接口返回错误。错误状态码:" + resultCode + ",描述:" + returnCode;
                log.Write("发生错误:请求微信网关成功,但微信接口返回错误。错误状态码:" + resultCode + ",描述:" + returnCode);

                return(false);
            }

            try
            {
                appid         = nav.SelectSingleNode("xml/appid").Value;
                bankType      = nav.SelectSingleNode("xml/bank_type").Value;
                cashFee       = nav.SelectSingleNode("xml/cash_fee").Value;
                feeType       = nav.SelectSingleNode("xml/fee_type").Value;
                isSubscribe   = nav.SelectSingleNode("xml/is_subscribe").Value;
                mchId         = nav.SelectSingleNode("xml/mch_id").Value;
                nonceStr      = nav.SelectSingleNode("xml/nonce_str").Value;
                openid        = nav.SelectSingleNode("xml/openid").Value;
                outTradeNo    = nav.SelectSingleNode("xml/out_trade_no").Value;
                timeEnd       = nav.SelectSingleNode("xml/time_end").Value;
                TotalFee      = nav.SelectSingleNode("xml/total_fee").Value;
                TradeType     = nav.SelectSingleNode("xml/trade_type").Value;
                TransactionId = nav.SelectSingleNode("xml/transaction_id").Value;

                sign = nav.SelectSingleNode("xml/sign").Value;
            }
            catch (Exception ex)
            {
                errorDescription = "解析通知数据 XML 异常。";
                log.Write("解析通知数据xml异常。" + ex.ToString());

                return(false);
            }

            try
            {
                // attach 接口附加返回参数,防止为空
                attach = nav.SelectSingleNode("xml/attach").Value;
            }
            catch { attach = string.Empty; }

            SortedDictionary <string, string> sParaTemp = new SortedDictionary <string, string>();
            sParaTemp.Add("appid", appid);
            sParaTemp.Add("attach", attach);
            sParaTemp.Add("bank_type", bankType);
            sParaTemp.Add("cash_fee", cashFee);
            sParaTemp.Add("fee_type", feeType);
            sParaTemp.Add("is_subscribe", isSubscribe);
            sParaTemp.Add("mch_id", mchId);
            sParaTemp.Add("nonce_str", nonceStr);
            sParaTemp.Add("openid", openid);
            sParaTemp.Add("out_trade_no", outTradeNo);
            sParaTemp.Add("result_code", resultCode);
            sParaTemp.Add("return_code", returnCode);
            sParaTemp.Add("time_end", timeEnd);
            sParaTemp.Add("total_fee", TotalFee);
            sParaTemp.Add("trade_type", TradeType);
            sParaTemp.Add("transaction_id", TransactionId);

            Utility utility = new Utility();

            string _sign = utility.CreateSign(sParaTemp, Utility.PayKey).ToUpper();

            if (sign.Trim().ToUpper() != _sign)
            {
                errorDescription = "微信响应结果签名错误";
                log.Write("签名错误。微信签名结果:" + sign + ",系统签名:" + _sign);

                return(false);
            }

            string returnWeChartData = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";

            if (!HandleBusiness(requestContent))
            {
                log.Write("发生错误:调用HandleBusiness()方法错误。等待微信重新通知。");
                return(false);
            }

            // 响应微信支付,订单处理成功
            HttpContext.Current.Response.Write(returnWeChartData);
            return(true);
        }