Пример #1
0
        /// <summary>
        /// 统一下单
        /// </summary>
        /// <param name="inputObj">提交给撤销订单API接口的参数</param>
        /// <param name="timeOut">接口超时时间</param>
        /// <returns>成功时返回API调用结果,其他抛异常</returns>
        /// <exception cref="MWeChatException"></exception>
        public WeChatDataModel UnifiedOrder(WeChatDataModel inputObj, int timeOut = 6)
        {
            string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

            //检测必填参数
            if (!inputObj.IsSet("out_trade_no"))
            {
                throw new MWeChatException("缺少统一支付接口必填参数out_trade_no!");
            }
            else if (!inputObj.IsSet("body"))
            {
                throw new MWeChatException("缺少统一支付接口必填参数body!");
            }
            else if (!inputObj.IsSet("total_fee"))
            {
                throw new MWeChatException("缺少统一支付接口必填参数total_fee!");
            }
            else if (!inputObj.IsSet("trade_type"))
            {
                throw new MWeChatException("缺少统一支付接口必填参数trade_type!");
            }
            //关联参数
            if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
            {
                throw new MWeChatException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
            }
            if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
            {
                throw new MWeChatException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
            }
            //异步通知url未设置,则使用配置文件中的url
            if (!inputObj.IsSet("notify_url"))
            {
                inputObj.SetValue("notify_url", _config.NOTIFY_URL);              //异步通知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", CommonManager.GetRandomStrByGUID(32)); //随机字符串
            //签名
            inputObj.SetValue("sign", inputObj.MakeSign(_config.KEY));
            string          xml      = inputObj.ToXml();
            var             start    = DateTime.Now;
            string          response = WeChatHttpManager.Post(xml, url, false, timeOut, _config);
            var             end      = DateTime.Now;
            int             timeCost = (int)((end - start).TotalMilliseconds);
            WeChatDataModel result   = new WeChatDataModel();

            result.FromXml(response, _config.KEY);
            ReportCostTime(url, timeCost, result);//测速上报
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获得JsAPI参数
        /// </summary>
        /// <param name="unifiedOrderResult">下单对象</param>
        /// <returns>JsAPI参数</returns>
        public string GetJsApiParameters(WeChatDataModel unifiedOrderResult)
        {
            WeChatDataModel jsApiParam = new WeChatDataModel();

            jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid"));
            jsApiParam.SetValue("timeStamp", CommonManager.GetTimeStamp());
            jsApiParam.SetValue("nonceStr", CommonManager.GetRandomStrByGUID());
            jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id"));
            jsApiParam.SetValue("signType", "MD5");
            jsApiParam.SetValue("paySign", jsApiParam.MakeSign(_config.KEY));
            string parameters = jsApiParam.ToJson();

            return(parameters);
        }
Пример #3
0
        /// <summary>
        /// 生成扫描支付模式二URL
        /// </summary>
        /// <param name="prodeutM">商品ID</param>
        /// <returns>模式二URL(2小时内有效)</returns>
        public string GetPrePayUrlByMode2(WeChatPayOrderModel prodeutM)
        {
            WeChatDataModel data = new WeChatDataModel();

            data.SetValue("body", prodeutM.Description);                                                              //商品描述
            data.SetValue("attach", prodeutM.Attach);                                                                 //附加数据
            data.SetValue("out_trade_no", CommonManager.GetRandomStrByGUID(32));                                      //随机字符串
            data.SetValue("total_fee", prodeutM.TotalPrice);                                                          //总金额
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));                                     //交易起始时间
            data.SetValue("time_expire", DateTime.Now.AddMinutes(prodeutM.EffectiveTime).ToString("yyyyMMddHHmmss")); //交易结束时间
            data.SetValue("goods_tag", prodeutM.Tag);                                                                 //商品标记
            data.SetValue("trade_type", "NATIVE");                                                                    //交易类型
            data.SetValue("product_id", prodeutM.ID);                                                                 //商品ID
            WeChatDataModel result = UnifiedOrder(data);                                                              //调用统一下单接口
            string          url    = result.GetValue("code_url").ToString();                                          //获得统一下单接口返回的二维码链接

            return(url);
        }
Пример #4
0
        /// <summary>
        /// 调用统一下单获取下单结果
        /// </summary>
        /// <param name="orderM">订单模型</param>
        /// <returns>下单结果</returns>
        public WeChatDataModel GetUnifiedOrderResult(WeChatPayOrderModel orderM)
        {
            WeChatDataModel data = new WeChatDataModel();

            data.SetValue("body", orderM.Description);
            data.SetValue("attach", orderM.Attach);
            data.SetValue("out_trade_no", orderM.ID);
            data.SetValue("total_fee", orderM.TotalPrice);
            data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
            data.SetValue("time_expire", DateTime.Now.AddMinutes(orderM.EffectiveTime).ToString("yyyyMMddHHmmss"));
            data.SetValue("goods_tag", orderM.Tag);
            data.SetValue("trade_type", "JSAPI");
            data.SetValue("openid", OpenID);
            WeChatDataModel result = UnifiedOrder(data);

            if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString().MIsNullOrEmpty())
            {
                throw new MWeChatException("配置错误");
            }
            else
            {
                return(result);
            }
        }
Пример #5
0
        /// <summary>
        /// 测速上报
        /// </summary>
        /// <param name="interface_url">接口URL</param>
        /// <param name="timeCost">接口耗时</param>
        /// <param name="inputObj">inputObj参数数组</param>
        private void ReportCostTime(string interface_url, int timeCost, WeChatDataModel inputObj)
        {
            //如果不需要进行上报
            if (_config.REPORT_LEVENL == 0)
            {
                return;
            }
            //如果仅失败上报
            if (_config.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" &&
                inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS")
            {
                return;
            }
            //上报逻辑
            WeChatDataModel data = new WeChatDataModel();

            data.SetValue("interface_url", interface_url);
            data.SetValue("execute_time_", timeCost);
            //返回状态码
            if (inputObj.IsSet("return_code"))
            {
                data.SetValue("return_code", inputObj.GetValue("return_code"));
            }
            //返回信息
            if (inputObj.IsSet("return_msg"))
            {
                data.SetValue("return_msg", inputObj.GetValue("return_msg"));
            }
            //业务结果
            if (inputObj.IsSet("result_code"))
            {
                data.SetValue("result_code", inputObj.GetValue("result_code"));
            }
            //错误代码
            if (inputObj.IsSet("err_code"))
            {
                data.SetValue("err_code", inputObj.GetValue("err_code"));
            }
            //错误代码描述
            if (inputObj.IsSet("err_code_des"))
            {
                data.SetValue("err_code_des", inputObj.GetValue("err_code_des"));
            }
            //商户订单号
            if (inputObj.IsSet("out_trade_no"))
            {
                data.SetValue("out_trade_no", inputObj.GetValue("out_trade_no"));
            }
            //设备号
            if (inputObj.IsSet("device_info"))
            {
                data.SetValue("device_info", inputObj.GetValue("device_info"));
            }
            try
            {
                Report(data);
            }
            catch (MWeChatException)
            {
                //不做任何处理
            }
        }