Esempio 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);
        }
        public async void BuildTokenRequestMessage_CorrectSettings_CorrectBuildedTrustedTokenRequest()
        {
            var expectedAcceptContent = new MediaTypeWithQualityHeaderValue("application/json");
            var settings = new PayUClientSettings("http://www.localhost.com", "v1", "1", "2");

            var result = PayUClientRequestBuilder.BuildTokenRequestMessage(settings, new TrustedMerchant("*****@*****.**", "extCustomerId"));

            var content = await result.Content.ReadAsStringAsync();

            Assert.Equal(HttpMethod.Post, result.Method);
            Assert.Equal(new Uri("http://www.localhost.com/pl/standard/user/oauth/authorize"), result.RequestUri);
            Assert.Contains(expectedAcceptContent, result.Headers.Accept);
            Assert.Equal("grant_type=trusted_merchant&client_id=1&client_secret=2&email=test%40test.com&ext_customer_id=extCustomerId", content, true);
        }
        public async void BuildTokenRequestMessage_CorrectSettings_CorrectBuildedTokenRequest()
        {
            var expectedAcceptContent = new MediaTypeWithQualityHeaderValue("application/json");
            var settings = new PayUClientSettings("http://www.localhost.com", "v1", "1", "2");

            var result = PayUClientRequestBuilder.BuildTokenRequestMessage(settings, null);

            var content = await result.Content.ReadAsStringAsync();

            Assert.Equal(HttpMethod.Post, result.Method);
            Assert.Equal(new Uri("http://www.localhost.com/pl/standard/user/oauth/authorize"), result.RequestUri);
            Assert.Contains(expectedAcceptContent, result.Headers.Accept);
            Assert.Equal("grant_type=client_credentials&client_id=1&client_secret=2", content);
        }