Ejemplo n.º 1
0
        /**
         *
         * 查询订单
         * @param WxPayData inputObj 提交给查询订单API的参数
         * @param int timeOut 超时时间
         * @throws WxPayException
         * @return 成功时返回订单查询结果,其他抛异常
         */
        public static WxPayData OrderQuery(WxPayData inputObj, int timeOut = 6)
        {
            string sendUrl = "https://api.mch.weixin.qq.com/pay/orderquery";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
            {
                throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
            }
            JsApiConfig jsApiConfig = new JsApiConfig();

            inputObj.SetValue("appid", jsApiConfig.AppId);                     //公众账号ID
            inputObj.SetValue("mch_id", jsApiConfig.Partner);                  //商户号
            inputObj.SetValue("nonce_str", GenerateNonceStr());                //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign(jsApiConfig.Key));     //签名
            string xml       = inputObj.ToXml();
            var    startTime = DateTime.Now;                                   //开始时间
            string response  = HttpService.Post(xml, sendUrl, false, timeOut); //调用HTTP通信接口提交数据
            var    endTime   = DateTime.Now;                                   //结束时间
            int    timeCost  = (int)((endTime - startTime).TotalMilliseconds); //计算所用时间
            //将xml格式的数据转化为对象以返回
            WxPayData result = new WxPayData();

            result.FromXml(response, jsApiConfig.Key);
            ReportCostTime(sendUrl, timeCost, result);//测速上报
            return(result);
        }
Ejemplo n.º 2
0
        /**
         *
         * 测速上报接口实现
         * @param WxPayData inputObj 提交给测速上报接口的参数
         * @param int timeOut 测速上报接口超时时间
         * @throws WxPayException
         * @return 成功时返回测速上报接口返回的结果,其他抛异常
         */
        public static WxPayData Report(WxPayData inputObj, int timeOut = 1)
        {
            JsApiConfig jsApiConfig = new JsApiConfig();
            string      url         = "https://api.mch.weixin.qq.com/payitil/report";

            //检测必填参数
            if (!inputObj.IsSet("interface_url"))
            {
                throw new WxPayException("接口URL,缺少必填参数interface_url!");
            }
            if (!inputObj.IsSet("return_code"))
            {
                throw new WxPayException("返回状态码,缺少必填参数return_code!");
            }
            if (!inputObj.IsSet("result_code"))
            {
                throw new WxPayException("业务结果,缺少必填参数result_code!");
            }
            if (!inputObj.IsSet("user_ip"))
            {
                throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
            }
            if (!inputObj.IsSet("execute_time_"))
            {
                throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
            }

            inputObj.SetValue("appid", jsApiConfig.AppId);                      //公众账号ID
            inputObj.SetValue("mch_id", jsApiConfig.Partner);                   //商户号
            inputObj.SetValue("user_ip", DTRequest.GetIP());                    //终端ip
            inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss")); //商户上报时间
            inputObj.SetValue("nonce_str", GenerateNonceStr());                 //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign(jsApiConfig.Key));      //签名
            string xml = inputObj.ToXml();

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

            WxPayData result = new WxPayData();

            result.FromXml(response, jsApiConfig.Key);
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 接收从微信支付后台发送过来的数据并验证签名
        /// </summary>
        /// <returns>微信支付后台返回的数据</returns>
        public static WxPayData GetNotifyData()
        {
            //接收从微信后台POST过来的数据
            System.IO.Stream s = HttpContext.Current.Request.InputStream;
            int count          = 0;

            byte[]        buffer  = new byte[1024];
            StringBuilder builder = new StringBuilder();

            while ((count = s.Read(buffer, 0, 1024)) > 0)
            {
                builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
            }
            s.Flush();
            s.Close();
            s.Dispose();

            //转换数据格式并验证签名
            WxPayData   data        = new WxPayData();
            JsApiConfig jsApiConfig = new JsApiConfig();

            try
            {
                data.FromXml(builder.ToString(), jsApiConfig.Key);
            }
            catch (WxPayException ex)
            {
                //若签名错误,则立即返回结果给微信支付后台
                WxPayData res = new WxPayData();
                res.SetValue("return_code", "FAIL");
                res.SetValue("return_msg", ex.Message);
                HttpContext.Current.Response.Write(res.ToXml());
                HttpContext.Current.Response.End();
            }

            return(data);
        }