/// <summary> /// 接收从微信支付后台发送过来的数据并验证签名 /// </summary> /// <returns>微信支付后台返回的数据</returns> public static async Task <WxPayData> GetNotifyData(WxPayConfig wxPayConfig, Stream body) { //接收从微信后台POST过来的数据 int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = await body.ReadAsync(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } WxPayLog.Info("WxPayApi", "Receive data from WeChat : " + builder.ToString()); //转换数据格式并验证签名 WxPayData data = new WxPayData(); //可能抛出异常 data.FromXml(wxPayConfig, builder.ToString()); WxPayLog.Info("WxPayApi", "Check sign success"); return(data); }
/** * * 测速上报接口实现 * @param WxPayData inputObj 提交给测速上报接口的参数 * @param int timeOut 测速上报接口超时时间 * @throws WxPayException * @return 成功时返回测速上报接口返回的结果,其他抛异常 */ public static WxPayData Report(WxPayConfig wxPayConfig, WxPayData inputObj, int timeOut = 1) { 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", wxPayConfig.AppID); //公众账号ID inputObj.SetValue("mch_id", wxPayConfig.MchID); //商户号 inputObj.SetValue("user_ip", wxPayConfig.Ip); //终端ip inputObj.SetValue("time", DateTime.Now.ToString("yyyyMMddHHmmss")); //商户上报时间 inputObj.SetValue("nonce_str", GenerateNonceStr()); //随机字符串 inputObj.SetValue("sign_type", WxPayData.SIGN_TYPE_HMAC_SHA256); //签名类型 inputObj.SetValue("sign", inputObj.MakeSign(wxPayConfig)); //签名 string xml = inputObj.ToXml(); WxPayLog.Info("WxPayApi", "Report request : " + xml); string response = HttpService.Post(wxPayConfig, xml, url, false, timeOut); WxPayLog.Info("WxPayApi", "Report response : " + response); WxPayData result = new WxPayData(); result.FromXml(wxPayConfig, response); return(result); }