コード例 #1
0
ファイル: WeChatPaymentWrapper.cs プロジェクト: Bobom/kuanmai
        public static AccessToken GetWeChatToken(WeChatPayConfig config, AccessToken oldToken,out bool changed)
        {
            changed = false;
            AccessToken token = null;
            TokenRequest request = null;
            bool needGet = false;
            if (oldToken==null)
            {
                needGet = true;
            }
            else
            {
                if(oldToken.ExpiresTime<DateTime.Now)
                {
                    needGet = true;
                }
            }

            if(needGet)
            {
                request = new TokenRequest(config);
                BaseResponse res = request.Execute();
                if (res != null)
                {
                    changed = true;
                    AccessTokenResponse tokenRes = (AccessTokenResponse)res;
                    if (tokenRes.Access_Token != null)
                    {
                        token = tokenRes.Access_Token;
                        oldToken = token;
                    }
                }
            }
            return token;
        }
コード例 #2
0
ファイル: WeChatPaymentWrapper.cs プロジェクト: Bobom/kuanmai
 public static JSAPITicket GetJSAPITicket(WeChatPayConfig config,AccessToken oldToken,JSAPITicket oldTicket, out bool changed)
 {
     changed = false;
     JSAPITicket ticket = null;
     bool needGet = false;
     JSAPITicketRequest request = null;
     if(oldTicket==null)
     {
         needGet = true;
     }
     else
     {
         if(oldTicket.ExpiresTime<DateTime.Now)
         {
             needGet = true;
         }
     }
     if(needGet)
     {
         changed = true;
         bool tChanged = false;
         request = new JSAPITicketRequest(config);
         AccessToken token = WeChatPaymentWrapper.GetWeChatToken(config,oldToken,out tChanged);
         request.Access_Token = token;
         BaseResponse res = request.Execute();
         if(res!=null)
         {
             JSAPITicketResponse jsRes = (JSAPITicketResponse)res;
             ticket = jsRes.Ticket;
         }
     }
     return ticket;
 }
コード例 #3
0
 static PersistentValueManager()
 {
     KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatPayConfig.xml"));
     KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatAccessToken.xml"));
     KMLogger.GetLogger().Info(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatJSAPITicket.xml"));
     config = XMLUtil.DeserializeXML<WeChatPayConfig>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatPayConfig.xml"));
     WeChatAccessToken = XMLUtil.DeserializeXML<AccessToken>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatAccessToken.xml"));
     WeChatJsApiTicket= XMLUtil.DeserializeXML<JSAPITicket>(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Config\\WeChatJSAPITicket.xml"));
 }
コード例 #4
0
ファイル: AuthHelper.cs プロジェクト: Bobom/kuanmai
 public static WeChatUserInfo GetUserInfo(WeChatPayConfig config, AccessToken token)
 {
     if (token == null || string.IsNullOrEmpty(token.Access_Token) || string.IsNullOrEmpty(token.OpenId))
     {
         throw new Exception("Access token and open id must be provided");
     }
     UserInfoRequest userInfoRequest = new UserInfoRequest(config);
     userInfoRequest.AccessToken = token.Access_Token;
     userInfoRequest.OpenId = token.OpenId;
     BaseResponse res = userInfoRequest.Execute();
     UserInfoResponse response = null;
     if (res != null)
     {
         response = (UserInfoResponse)res;
     }
     return response.UserInfo;
 }
コード例 #5
0
        public static AccessToken GetWeChatAccessToken()
        {
            if (WeChatAccessToken==null)
            {
                WeChatAccessToken = RequestWeChatAccessToken();
            }
            else
            {
                if(WeChatAccessToken.ExpiresTime < DateTime.Now)
                {
                    WeChatAccessToken = RequestWeChatAccessToken();
                }
                else if(DateTimeUtil.ConvertDateTimeToInt(WeChatAccessToken.ExpiresTime) - DateTimeUtil.ConvertDateTimeToInt(DateTime.Now)<20)
                {
                    WeChatAccessToken = RequestWeChatAccessToken();
                }
            }

            return WeChatAccessToken;
        }