示例#1
0
        /**
         *
         * 撤销订单API接口
         * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个
         * @param int timeOut 接口超时时间
         * @throws WxPayException
         * @return 成功时返回API调用结果,其他抛异常
         */
        public static WxPayData Reverse(WxPayData inputObj, int timeOut = 6)
        {
            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", WxPayConfig.GetConfig().GetAppID());  //公众账号ID
            inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID()); //商户号
            inputObj.SetValue("nonce_str", GenerateNonceStr());              //随机字符串
            inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256); //签名类型
            inputObj.SetValue("sign", inputObj.MakeSign());                  //签名
            string xml = inputObj.ToXml();

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



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



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

            WxPayData result = new WxPayData();

            result.FromXml(response);

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

            return(result);
        }
示例#2
0
        /**
         *
         * 统一下单
         * @param WxPaydata inputObj 提交给统一下单API的参数
         * @param int timeOut 超时时间
         * @throws WxPayException
         * @return 成功时返回,其他抛异常
         */
        public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            // 测试
            //string url = "https://api.mch.weixin.qq.com/sandboxnew/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", WxPayConfig.GetConfig().GetNotifyUrl());//异步通知url
            }

            inputObj.SetValue("appid", WxPayConfig.GetConfig().GetAppID());  //公众账号ID
            inputObj.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID()); //商户号
            //H5支付提供真实ip
            if (!inputObj.IsSet("spbill_create_ip"))
            {
                inputObj.SetValue("spbill_create_ip", WxPayConfig.GetConfig().GetIp()); //终端ip
            }
            inputObj.SetValue("nonce_str", GenerateNonceStr());                         //随机字符串
            inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256);            //签名类型

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

            var start = DateTime.Now;

            /*测试---------------------------------------
             * string sandbox_url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";
             * WxPayData d = new WxPayData();
             * d.SetValue("mch_id", WxPayConfig.GetConfig().GetMchID());
             * d.SetValue("nonce_str", GenerateNonceStr());
             * d.SetValue("sign", d.MakeSign()) ;
             * string r = HttpService.Post(d.ToXml(), sandbox_url, false, timeOut);
             * WxPayData d1 = new WxPayData();
             * d1.FromXml(r);
             * string sandbox_key = d1.GetValue("sandbox_signkey").ToString();
             * log.Info("sandbox验签返回" + d1.ToXml().ToString());
             * inputObj.SetValue("sign", inputObj.MakeSign(WxPayData.SIGN_TYPE_MD5, sandbox_key));
             * xml = inputObj.ToXml();
             * ------------------------------------------测试结束*/


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

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

            WxPayData result = new WxPayData();

            result.FromXml(response);


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

            return(result);
        }