예제 #1
0
        /**
         * 调用统一下单,获得下单结果
         * @return 统一下单结果
         * @失败时抛异常WxPayException
         */
        public static JObject GetUnifiedOrderResult(string out_trade_no, string total_fee, string subject)
        {
            //统一下单
            WxPayData data = new WxPayData();

            data.SetValue("body", subject);
            data.SetValue("out_trade_no", out_trade_no);
            data.SetValue("total_fee", total_fee);
            data.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//通知地址
            data.SetValue("trade_type", "APP");
            //data.SetValue("trade_type", "JSAPI");

            WxPayData result = WxPayApi.UnifiedOrder(data);

            if (!result.IsSet("appid") || !result.IsSet("prepay_id") || string.IsNullOrEmpty(result.GetValue("prepay_id").ToString()))
            {
                WebApiConfig.log.Error("微信统一下单出错!" + result.GetValue("return_msg") ?? string.Empty);
                throw new Exception("微信统一下单出错 !");
            }


            WxPayData res = new WxPayData();

            res.SetValue("prepayid", result.GetValue("prepay_id"));
            res.SetValue("appid", result.GetValue("appid"));
            res.SetValue("partnerid", result.GetValue("mch_id"));
            res.SetValue("package", "Sign=WXPay");
            res.SetValue("noncestr", result.GetValue("nonce_str"));
            var timestamp = WxPayApi.GenerateTimeStamp();

            res.SetValue("timestamp", timestamp);
            var sign  = res.MakeSign();
            var value = new JObject(
                new JProperty("prepay_id", result.GetValue("prepay_id")),
                new JProperty("appid", result.GetValue("appid")),
                new JProperty("partnerid", result.GetValue("mch_id")),
                new JProperty("package", "Sign=WXPay"),
                new JProperty("noncestr", result.GetValue("nonce_str")),
                new JProperty("timestamp", timestamp),
                new JProperty("sign", sign));

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

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

            inputObj.SetValue("appid", WxPayConfig.APPID);               //公众账号ID
            inputObj.SetValue("mch_id", WxPayConfig.MCHID);              //商户号
            inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr()); //随机字符串
            inputObj.SetValue("sign", inputObj.MakeSign());              //签名

            string xml = inputObj.ToXml();

            var start = DateTime.Now;

            WebApiConfig.log.Debug("WxPayApi", "OrderQuery request : " + xml);
            string response = HttpService.Post(xml, url, false, timeOut);//调用HTTP通信接口提交数据

            WebApiConfig.log.Debug("WxPayApi", "OrderQuery response : " + response);

            var end      = DateTime.Now;
            int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时

            //将xml格式的数据转化为对象以返回
            WxPayData result = new WxPayData();

            result.FromXml(response);

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

            return(result);
        }