第1肯定是配置的参数类
public class JsApiConfig { #region 字段 private string mch_id = string.Empty; private string key = string.Empty; private string appid = string.Empty; private string appsecret = string.Empty; private string redirect_url = string.Empty; private string notify_url = string.Empty; #endregion public JsApiConfig(int wid, SysModuleNameEnum moduleName = SysModuleNameEnum.小程序商城) { if (moduleName == SysModuleNameEnum.小程序商城) { BLL.minapp_setting settingBll = new BLL.minapp_setting(); Model.minapp_setting setting = settingBll.GetModelByWid(wid); appid = setting.app_id; //绑定支付的APPID(必须配置) appsecret = setting.app_secret; //公众帐号secert(仅JSAPI支付的时候需要配置) } else if (moduleName == SysModuleNameEnum.微商城) { BLL.wx_userweixin uwxBll = new BLL.wx_userweixin(); Model.wx_userweixin uwx = uwxBll.GetModel(wid); appid = uwx.AppId; //绑定支付的APPID(必须配置) appsecret = uwx.AppSecret; //公众帐号secert(仅JSAPI支付的时候需要配置) } BLL.wx_payment_wxpay wxPayBll = new BLL.wx_payment_wxpay(); Model.wx_payment_wxpay paymentInfo = wxPayBll.GetModelByWid(wid); if (paymentInfo != null) { mch_id = paymentInfo.mch_id; //商户号(必须配置) key = paymentInfo.paykey; //商户支付密钥,参考开户邮件设置(必须配置) redirect_url = "";// MyCommFun.getAppSettingValue("webapi_url") + "/api/Pay/redirect_url"; notify_url = MyCommFun.getAppSettingValue("webapi_url") + "/api/Pay/notify_url"; } } #region 属性 ////// 商户号(必须配置) /// public string MchId { get { return mch_id; } set { mch_id = value; } } /// /// 获取或设交易安全校验码 /// public string Key { get { return key; } set { key = value; } } /// /// 绑定支付的APPID(必须配置) /// public string AppId { get { return appid; } set { appid = value; } } /// /// 公众帐号secert(仅JSAPI支付的时候需要配置) /// public string AppSecret { get { return appsecret; } set { appsecret = value; } } /// /// 获取用户的OPENID回调地址 /// public string Redirect_url { get { return redirect_url; } } /// /// 获取服务器异步通知页面路径 /// public string Notify_url { get { return notify_url; } } #endregion }
第2就是正主来了,所有的微信支付的逻辑方法
public class JsApiPay { /** * * 测速上报 * @param string interface_url 接口URL * @param int timeCost 接口耗时 * @param WxPayData inputObj参数数组 */ public static void ReportCostTime(int wid, string interface_url, int timeCost, WxPayData inputObj, SysModuleNameEnum moduleName = SysModuleNameEnum.小程序商城) { //如果仅失败上报 if (inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" && inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS") { return; } //上报逻辑 WxPayData data = new WxPayData(); 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(wid, data, 1,moduleName); } catch (WxPayException ex) { //不做任何处理 } } /** * * 测速上报接口实现 * @param WxPayData inputObj 提交给测速上报接口的参数 * @param int timeOut 测速上报接口超时时间 * @throws WxPayException * @return 成功时返回测速上报接口返回的结果,其他抛异常 */ public static WxPayData Report(int wid, WxPayData inputObj, int timeOut = 1, SysModuleNameEnum moduleName = SysModuleNameEnum.小程序商城) { JsApiConfig jsApiConfig = new JsApiConfig(wid, moduleName); string url = "https://api.mch.weixin.qq.com/payitil/report"; //检测必填参数 if (!inputObj.IsSet("interface_url")) { throw new WxPayException("接口URL,缺少必填参数interface_url!"); } if (!inputObj.IsSet("return_code")) { throw new WxPayException("返回状态码,缺少必填参数return_code!"); } if (!inputObj.IsSet("result_code")) { throw new WxPayException("业务结果,缺少必填参数result_code!"); } if (!inputObj.IsSet("user_ip")) { throw new WxPayException("访问接口IP,缺少必填参数user_ip!"); } if (!inputObj.IsSet("execute_time_")) { throw new WxPayException("接口耗时,缺少必填参数execute_time_!"); } inputObj.SetValue("appid", jsApiConfig.AppId);//公众账号ID inputObj.SetValue("mch_id", jsApiConfig.MchId);//商户号 inputObj.SetValue("user_ip", MXRequest.GetIP());//终端ip inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss"));//商户上报时间 inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign(jsApiConfig.Key));//签名 string xml = inputObj.ToXml(); string response = HttpService.Post(xml, url, false, timeOut); WxPayData result = new WxPayData(); result.FromXml(response, jsApiConfig.Key); return result; } /** * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数 * @return 时间戳 */ public static string GenerateTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } /** * 生成随机串,随机串包含字母或数字 * @return 随机串 */ public static string GenerateNonceStr() { return Guid.NewGuid().ToString().Replace("-", ""); } ////// 接收从微信支付后台发送过来的数据暂不验证签名 /// /// 微信支付后台返回的数据 public static WxPayData GetNotifyData() { string xml = @" "; //接收从微信后台POST过来的数据 //if (1==1) //{//测试专用 System.IO.Stream s = HttpContext.Current.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); // } //转换数据格式并验证签名 WxPayData data = new WxPayData(); try { LogHelper.Debug("builder.ToString()=" + builder.ToString(), 0, "微信支付回调", "获取回调数据xml", "GetNotifyData"); data.FromXml(builder.ToString()); // data.FromXml(xml); } catch (WxPayException ex) { //若有错误,则立即返回结果给微信支付后台 WxPayData res = new WxPayData(); res.SetValue("return_code", "FAIL"); res.SetValue("return_msg", ex.Message); HttpContext.Current.Response.Write(res.ToXml()); HttpContext.Current.Response.End(); } return data; } /** * * 查询订单 * @param WxPayData inputObj 提交给查询订单API的参数 * @param int timeOut 超时时间 * @throws WxPayException * @return 成功时返回订单查询结果,其他抛异常 */ public static WxPayData OrderQuery(int wid, WxPayData inputObj, int timeOut = 6, SysModuleNameEnum moduleName = SysModuleNameEnum.小程序商城) { string sendUrl = "https://api.mch.weixin.qq.com/pay/orderquery"; //检测必填参数 if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id")) { throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!"); } JsApiConfig jsApiConfig = new JsApiConfig(wid); inputObj.SetValue("appid", jsApiConfig.AppId);//公众账号ID inputObj.SetValue("mch_id", jsApiConfig.MchId);//商户号 inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串 inputObj.SetValue("sign", inputObj.MakeSign(jsApiConfig.Key));//签名 string xml = inputObj.ToXml(); var startTime = DateTime.Now; //开始时间 string response = HttpService.Post(xml, sendUrl, false, timeOut);//调用HTTP通信接口提交数据 var endTime = DateTime.Now; //结束时间 int timeCost = (int)((endTime - startTime).TotalMilliseconds); //计算所用时间 //将xml格式的数据转化为对象以返回 WxPayData result = new WxPayData(); result.FromXml(response, jsApiConfig.Key); ReportCostTime(wid, sendUrl, timeCost, result);//测速上报 return result; } } 20