Ejemplo n.º 1
0
        public static T GetTokenFromCache <T>(
            this ObjectCache cache,
            PayUClientSettings settings,
            IHttpClientFactory clientFactory,
            TrustedMerchant tm)
            where T : PayUToken
        {
            var tokenKey = tm != null?
                           string.Format(CultureInfo.InvariantCulture, PayUContainer.TokenCacheTrustedFormat, tm.Email, tm.ExtCustomerId) :
                               PayUContainer.TokenCacheKey;

            var cachedObject = (T)cache[tokenKey];

            if (cachedObject == null)
            {
                var request      = PayUClientRequestBuilder.BuildTokenRequestMessage(settings, tm);
                var communicator = new PayUApiHttpCommunicator <PayUToken>(clientFactory, settings);
                cachedObject = (T)communicator.Send(request);
                var policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(cachedObject.ExpireIn - 30);
                cache.Set(tokenKey, cachedObject, policy);
            }

            return(cachedObject);
        }
        private static HttpContent GetTrustedTokenBody(PayUClientSettings settings, TrustedMerchant tm)
        {
            var content = new Dictionary <string, string>(5);

            AddBaseContent(settings, PayUContainer.GrantType.TrustedMerchant, content);
            if (!string.IsNullOrEmpty(tm.Email) && !string.IsNullOrEmpty(tm.ExtCustomerId))
            {
                content.Add(PayUContainer.PropsName.Email, tm.Email);
                content.Add(PayUContainer.PropsName.Ex_Customer_Id, tm.ExtCustomerId);
            }

            return(new FormUrlEncodedContent(content));
        }
        public static HttpRequestMessage BuildTokenRequestMessage(PayUClientSettings settings, TrustedMerchant tm)
        {
            var message = new HttpRequestMessage();

            message.Headers.Accept.Add(PayUContainer.ContentJson);
            message.Method     = HttpMethod.Post;
            message.RequestUri = PayUClientUrlBuilder.BuildOAuthTokenUrl(settings.Url);
            message.Content    = tm != null?GetTrustedTokenBody(settings, tm) : GetTokenBody(settings);

            return(message);
        }