/// <summary> /// 统一下单 /// </summary> /// <param name="inputObj">提交给统一下单API的参数</param> /// <param name="timeOut">超时时间</param> /// <returns></returns> public static WxData UnifiedOrder(WxData inputObj, int timeOut = 10) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //检测必填参数 if (!inputObj.IsSet("out_trade_no")) { throw new WxException("缺少统一支付接口必填参数out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxException("缺少统一支付接口必填参数body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxException("缺少统一支付接口必填参数total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxException("缺少统一支付接口必填参数trade_type!"); } //关联参数 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxException("统一支付接口中,缺少必填参数product_id!trade_type为NATIVE时,product_id为必填参数!"); } //异步通知url未设置,则使用配置文件中的url if (!inputObj.IsSet("notify_url")) { inputObj.SetValue("notify_url", WxConfig.NOTIFY_URL); //异步通知url } //终端ip if (inputObj.GetValue("trade_type").ToString() == "JSAPI") { inputObj.SetValue("spbill_create_ip", WxUtility.GetClientIP()); } else if (inputObj.GetValue("trade_type").ToString() == "NATIVE") { inputObj.SetValue("spbill_create_ip", WxConfig.IP); } else { inputObj.SetValue("spbill_create_ip", WxUtility.GetClientIP()); } inputObj.SetValue("appid", WxConfig.APPID); //公众账号ID inputObj.SetValue("mch_id", WxConfig.MCHID); //商户号 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 //签名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml(); DateTime start = DateTime.Now; Log.Debug("WxPayApi", "统一下单 request数据 : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "统一下单 response数据 : " + response); DateTime end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds); WxData result = new WxData(); result.FromXml(response); ReportCostTime(url, timeCost, result); //测速上报 return(result); }
/// <summary> /// 生成内容 /// </summary> /// <returns></returns> public override string GenerateContent() { this.CreateTime = WxUtility.GetTime().ToString(); return(string.Format(this.Template, this.ToUserName, this.FromUserName, this.CreateTime, this.MsgType, this.Content, this.MsgId)); }