/// <summary> /// 刷新票据凭证 /// </summary> /// <param name="accessToken"></param> /// <returns></returns> public static string RefreshAccessToken(string accessToken) { lock ( locker ) { if (WechatApi.AccessToken != accessToken) { return(WechatApi.AccessToken); } AccessTokenResponseMessage response = WechatApi.GetAccessTokenFromWechat(); WechatAccessToken token = new WechatAccessToken(); token.AppId = ConfigurationManager.AppSettings["AppId"]; token.AccessToken = response.AccessToken; token.ExpiresIn = response.ExpiresIn; token.UpdateTime = DateTime.Now; RedisHelper redis = new RedisHelper(); redis.StringSet <WechatAccessToken>(token.AppId, token); WechatApi.AccessToken = token.AccessToken; return(WechatApi.AccessToken); } }
public string GetSignature(string url) { Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("noncestr", nonceStr); dic.Add("jsapi_ticket", WechatApi.GetAccessTokenAndRefresh()); dic.Add("timestamp", timestamp); dic.Add("url", url); Dictionary <string, string> sortDic = dic.OrderBy(e => e.Key).ToDictionary(k => k.Key, v => v.Value); StringBuilder sb = new StringBuilder(); foreach (KeyValuePair <string, string> pair in sortDic) { sb.Append(pair.Key); sb.Append("="); sb.Append(pair.Value); sb.Append("&"); } sb.Remove(sb.Length - 1, 1); string sign = StringCoding.EncodeSHA1(sb.ToString()).ToLower(); LogHelper.Info(String.Format("WxPayJsConfig GetSignature:{0} -> {1}", sb.ToString(), sign)); return(sign); }
/// <summary> /// 获取票据凭证 /// </summary> /// <returns></returns> public static string GetAccessToken() { lock ( locker ) { if (string.IsNullOrEmpty(WechatApi.AccessToken)) { WechatAccessToken token = WechatApi.GetAccessTokenFromDb(); if (token == null) { AccessTokenResponseMessage response = WechatApi.GetAccessTokenFromWechat(); token = new WechatAccessToken(); token.AppId = ConfigurationManager.AppSettings["AppId"]; token.AccessToken = response.AccessToken; token.ExpiresIn = response.ExpiresIn; token.UpdateTime = DateTime.Now; RedisHelper redis = new RedisHelper(); redis.StringSet <WechatAccessToken>(token.AppId, token); WechatApi.AccessToken = token.AccessToken; } else { WechatApi.AccessToken = token.AccessToken; } } return(WechatApi.AccessToken); } }
/// <summary> /// 获取微信用户 Access Token /// </summary> /// <param name="code"></param> /// <returns></returns> public static UserAccessTokenResponseMessage GetUserAccessToken(string code) { string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", ConfigurationManager.AppSettings["AppId"], ConfigurationManager.AppSettings["AppSecret"], code); UserAccessTokenResponseMessage response = WechatApi.GetWechatApi <UserAccessTokenResponseMessage>(url); if (response.ErrorCode.HasValue && response.ErrorCode.Value != 0) { throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url); } return(response); }
/// <summary> /// 从微信服务器获取票据凭证 /// </summary> /// <returns></returns> public static AccessTokenResponseMessage GetAccessTokenFromWechat() { string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", ConfigurationManager.AppSettings["AppId"], ConfigurationManager.AppSettings["AppSecret"]); LogHelper.Info("正在调用接口获取票据凭证..."); AccessTokenResponseMessage response = WechatApi.GetWechatApi <AccessTokenResponseMessage>(url); LogHelper.Info("已获取票据凭证。"); if (response.ErrorCode.HasValue && response.ErrorCode.Value != 0) { throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url); } return(response); }
private static T PostWechatApiWithToken <T>(string rawUrl, string request) where T : WechatApiResponseMessage { string accessToken = WechatApi.GetAccessToken(); string url = rawUrl + "access_token=" + accessToken; T response = WechatApi.PostWechatApi <T>(url, request); if (response.ErrorCode.HasValue) { switch (response.ErrorCode.Value) { case 0: break; // 如果是票据过期问题,则重新获取票据 case 40001: case 40014: case 42001: LogHelper.Info(string.Format("调用微信接口时票据凭证无效或过期, errcode : {0}, errmsg : {1}, interface : {2}", response.ErrorCode, response.ErrorMessage, url)); // 重新获取票据凭证 accessToken = WechatApi.RefreshAccessToken(accessToken); url = rawUrl + "access_token=" + accessToken; response = WechatApi.PostWechatApi <T>(url, request); if (response.ErrorCode.HasValue && response.ErrorCode.Value != 0) { throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url); } break; default: throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url); } } return(response); }
public static string GetTicket() { string accessToken = WechatApi.GetAccessToken(); string url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi"; string responseInfo = WechatApi.HttpGet(url1); WechatTicketResponse response = JsonConvert.DeserializeObject <WechatTicketResponse>(responseInfo); if (response.errcode == 42001) { WechatApi.RefreshAccessToken(accessToken); accessToken = WechatApi.GetAccessToken(); url1 = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi"; responseInfo = WechatApi.HttpGet(url1); WechatTicketResponse responseLast = JsonConvert.DeserializeObject <WechatTicketResponse>(responseInfo); return(responseLast.ticket); } else { string ticket = response.ticket; return(ticket); } }
/// <summary> /// 获取微信服务器的IP地址 /// </summary> /// <returns></returns> public static CallbackIpResponseMessage GetCallbackIp() { string rawUrl = "https://api.weixin.qq.com/cgi-bin/getcallbackip?"; return(WechatApi.GetWechatApiWithToken <CallbackIpResponseMessage>(rawUrl)); }
/// <summary> /// 发送文本消息 /// </summary> /// <param name="message"></param> /// <returns></returns> public static void SendTextMessage(CustomTextMessage message) { WechatApi.SendCustomMessage(message); }
public static void SendCustomTemplateMessage(string message) { string rawUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?"; WechatApi.PostWechatApiWithToken <WechatApiTemplateResponseMessage>(rawUrl, message); }
/// <summary> /// 调用客服接口向用户推送消息 /// </summary> /// <param name="message"></param> /// <returns></returns> public static void SendCustomMessage(CustomMessage message) { string rawUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?"; WechatApi.PostWechatApiWithToken <WechatApiResponseMessage>(rawUrl, message); }
private static T PostWechatApi <T>(string url, string request) where T : WechatApiResponseMessage { string body = WechatApi.HttpPost(url, request); return(JsonConvert.DeserializeObject <T>(body)); }
/// <summary> /// 通过 GET 方式调用微信 API /// </summary> /// <typeparam name="T"></typeparam> /// <param name="rawUrl"></param> /// <returns></returns> private static T GetWechatApi <T>(string rawUrl) where T : WechatApiResponseMessage { string responseBody = WechatApi.HttpGet(rawUrl); return(JsonConvert.DeserializeObject <T>(responseBody)); }