예제 #1
0
파일: WxPayApi.cs 프로젝트: ntzw/WxHelper
        /**
         *
         * 撤销订单API接口
         * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个
         * @param int timeOut 接口超时时间
         * @throws WxPayException
         * @return 成功时返回API调用结果,其他抛异常
         */
        public static WxPayData Reverse(WxPayData inputObj, string appid, int timeOut = 6)
        {
            AppConfig config = AppConfigLibrary.Get(appid);
            string    url    = "https://api.mch.weixin.qq.com/secapi/pay/reverse";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
            {
                throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
            }

            inputObj.SetValue("appid", config.AppId);           //公众账号ID
            inputObj.SetValue("mch_id", config.Mchid);          //商户号
            inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign());     //签名
            string xml = inputObj.ToXml();

            var start = DateTime.Now;//请求开始时间

            LogManager.Debug("WxPayApi", "Reverse request : " + xml);

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

            LogManager.Debug("WxPayApi", "Reverse response : " + response);

            var end      = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);

            WxPayData result = new WxPayData(appid);

            result.FromXml(response);

            ReportCostTime(url, appid, timeCost, result);//测速上报

            return(result);
        }
예제 #2
0
파일: WxPayApi.cs 프로젝트: ntzw/WxHelper
        /**
         *
         * 统一下单
         * @param WxPaydata inputObj 提交给统一下单API的参数
         * @param int timeOut 超时时间
         * @throws WxPayException
         * @return 成功时返回,其他抛异常
         */
        public static WxPayData UnifiedOrder(WxPayData inputObj, string appid, int timeOut = 6)
        {
            AppConfig    config = AppConfigLibrary.Get(appid);
            const string url    = "https://api.mch.weixin.qq.com/pay/unifiedorder";

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

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

            //异步通知url未设置,则使用配置文件中的url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", config.NotifyUrl);//异步通知url
            }

            inputObj.SetValue("appid", config.AppId);           //公众账号ID
            inputObj.SetValue("mch_id", config.Mchid);          //商户号
            inputObj.SetValue("spbill_create_ip", config.Ip);   //终端ip
            inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串

            //签名
            inputObj.SetValue("sign", inputObj.MakeSign());
            string xml = inputObj.ToXml();

            var start = DateTime.Now;

            LogManager.Debug("WxPayApi", "UnfiedOrder request : " + xml);
            string response = HttpService.Post(xml, url, false, timeOut, appid);

            LogManager.Debug("WxPayApi", "UnfiedOrder response : " + response);

            var end      = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);

            WxPayData result = new WxPayData(appid);

            result.FromXml(response);

            ReportCostTime(url, appid, timeCost, result);//测速上报

            return(result);
        }