public async Task <ResultModel <string> > GetOpenID(GetOpenIDRequestModel requestModel) { try { Guid userID = _userService.GetUserID(requestModel.Token); ApplicationDTO applicationInfo = await _applicationService.GetApplicationInfoAsync(requestModel.AppID, userID); if (applicationInfo == null) { return(ResultModel <string> .Fail("未注册该小程序")); } var weChatConfig = new WeChatConfigModel { APPID = applicationInfo.AppID, APPSECRET = applicationInfo.AppSecret }; var manager = new WeChatMiniProgramManager(weChatConfig); string openID = manager.GetOpenIDByCode(requestModel.Code); return(ResultModel <string> .Success(openID, "获取OpenID成功")); } catch (InvalidOperationException ex) { return(ResultModel <string> .Fail(ex.Message)); } catch (WeChatException ex) { return(ResultModel <string> .Fail(ex.Message)); } }
public ExampleByWeChatPublicNumberManager(string appID, string appSecret) { _weChatConfig = new WeChatConfigModel { APPID = appID, APPSECRET = appSecret }; }
public async Task GetOpenIDByCode(string code) { var config = new WeChatConfigModel { APPID = "wx93154cd53fc514f9", APPSECRET = "f41448533e3aa671524202d5c0fbc243" }; var weChatAppletManager = new WeChatMiniProgramManager(config); string openID = await weChatAppletManager.GetOpenIDByCodeAsync(code); Console.WriteLine($"OpenID为:{openID}"); }
/// <summary> /// 生成扫描支付模式一URL /// </summary> /// <param name="prodeutM">商品ID</param> /// <returns>模式一URL</returns> public string GetPrePayUrlByMode1(WeChatPayOrderModel prodeutM) { WeChatConfigModel configM = new WeChatConfigModel(); WeChatDataModel data = new WeChatDataModel(); data.SetValue("appid", configM.APPID); //公众帐号id data.SetValue("mch_id", configM.MCHID); //商户号 data.SetValue("time_stamp", CommonManager.GetTimeStamp()); //时间戳 data.SetValue("nonce_str", CommonManager.GetRandomStrByGUID(32)); //随机字符串 data.SetValue("product_id", prodeutM.ID); //商品ID data.SetValue("sign", data.MakeSign(configM.KEY)); //签名 string url = "weixin://wxpay/bizpayurl?" + data.ToUrlParams(); return(url); }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM">配置对象</param> public RQCodePayManager(WeChatConfigModel configM) : base(configM) { }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM"></param> public WeChatPayManager(WeChatConfigModel configM) { _config = configM; }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM">配置对象</param> public JsAPIPayManager(WeChatConfigModel configM) : base(configM) { }
public static WeChatConfig ToEntity(this WeChatConfigModel model, WeChatConfig destination, IObjectMapper ObjectMapper) { return(model.MapTo(destination, ObjectMapper)); }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM"></param> public WXSSManager(WeChatConfigModel configM) { _config = configM; }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM"></param> public WeChatPublicNumberManager(WeChatConfigModel configM) { Config = configM; }
/// <summary> /// POST请求 /// </summary> /// <param name="url">url地址</param> /// <param name="data">xml</param> /// <param name="isUseCert">是否使用证书</param> /// <param name="timeout">超时时间</param> /// <param name="config">微信支付配置对象</param> /// <returns>请求返回数据</returns> public static string PostJson <T>(string url, T data, bool isUseCert, int timeout, WeChatConfigModel config) { GC.Collect(); string result = ""; HttpWebRequest request = null; HttpWebResponse response = null; try { ServicePointManager.DefaultConnectionLimit = 200; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeout * 1000; request.ContentType = "application/json"; byte[] dataBuffer = Encoding.UTF8.GetBytes(data.ToJson()); request.ContentLength = dataBuffer.Length; if (isUseCert) { var cert = new X509Certificate2($"{AppDomain.CurrentDomain.BaseDirectory}{config.SSLCERT_PATH}", config.SSLCERT_PASSWORD); request.ClientCertificates.Add(cert); } Stream reqStream = request.GetRequestStream(); reqStream.Write(dataBuffer, 0, dataBuffer.Length); reqStream.Close(); response = (HttpWebResponse)request.GetResponse(); var sr = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException("读取流失败"), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (ThreadAbortException) { Thread.ResetAbort(); } finally { response?.Close(); request?.Abort(); } return(result); }
/// <summary> /// POST请求 /// </summary> /// <param name="url">url地址</param> /// <param name="formItems"></param> /// <param name="isUseCert">是否使用证书</param> /// <param name="timeout">超时时间</param> /// <param name="config">微信支付配置对象</param> /// <returns>请求返回数据</returns> public static string PostFormData(string url, ICollection <FormItemModel> formItems, bool isUseCert, int timeout, WeChatConfigModel config) { GC.Collect(); var result = ""; HttpWebRequest request = null; HttpWebResponse response = null; try { ServicePointManager.DefaultConnectionLimit = 200; if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeout * 1000; string boundary = "----" + DateTime.Now.Ticks.ToString("x");//分隔符 request.ContentType = $"multipart/form-data; boundary={boundary}"; //请求流 using (var postStream = new MemoryStream()) { #region 处理Form表单请求内容 //是否用Form上传文件 bool formUploadFile = formItems != null && formItems.Count > 0; if (formUploadFile) { //文件数据模板 string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + "\r\nContent-Type: application/octet-stream" + "\r\n\r\n"; //文本数据模板 string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}"; foreach (FormItemModel item in formItems) { string formdata; if (item.IsFile) { //上传文件 formdata = string.Format( fileFormdataTemplate, item.Key, //表单键 item.FileName); } else { //上传文本 formdata = string.Format( dataFormdataTemplate, item.Key, item.Value); } //统一处理 byte[] formdataBytes = Encoding.UTF8.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata); postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件内容 if (item.FileContent == null || item.FileContent.Length <= 0) { continue; } using (Stream stream = item.FileContent) { var buffer = new byte[1024]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { postStream.Write(buffer, 0, bytesRead); } } } //结尾 byte[] footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); } else { request.ContentType = "application/x-www-form-urlencoded"; } #endregion request.ContentLength = postStream.Length; #region 输入二进制流 { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream(); var buffer = new byte[1024]; int bytesRead; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } } #endregion } if (isUseCert) { var cert = new X509Certificate2($"{AppDomain.CurrentDomain.BaseDirectory}{config.SSLCERT_PATH}", config.SSLCERT_PASSWORD); request.ClientCertificates.Add(cert); } response = (HttpWebResponse)request.GetResponse(); var sr = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException("读取流失败"), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (ThreadAbortException) { Thread.ResetAbort(); } finally { response?.Close(); request?.Abort(); } return(result); }
/// <summary> /// 构造方法 /// </summary> /// <param name="configM"></param> public WeChatMiniProgramManager(WeChatConfigModel configM) { Config = configM; }