示例#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 GetTokenBody(PayUClientSettings settings)
        {
            var content = new Dictionary <string, string>(3);

            AddBaseContent(settings, PayUContainer.GrantType.ClientCredentials, content);

            return(new FormUrlEncodedContent(content));
        }
        public void Constructor_CorrectParameters_CreatePayUClientSettingsInstance()
        {
            var result = new PayUClientSettings(PayUContainer.PayUApiUrl.Production, PayUContainer.Version.v2_1, "clientId", "clientSecret");

            Assert.NotNull(result);
            Assert.Equal("https://secure.payu.com", result.Url);
            Assert.Equal("v2_1", result.ApiVersion);
            Assert.Equal("clientId", result.ClientId);
            Assert.Equal("clientSecret", result.ClientSecret);
        }
        public PayUClientMockIntegrationTests()
        {
            var settings = new PayUClientSettings(
                "https://private-anon-45d00fcdba-payu21.apiary-mock.com",
                "v2_1",
                "145227",
                "12f071174cb7eb79d4aac5bc2f0756"
                );

            this.client = new PayUClient(settings);
        }
        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);
        }
        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 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);
        }
示例#9
0
 public PayUClient(PayUClientSettings settings, IHttpClientFactory clientFactory)
     : base(settings, clientFactory)
 {
 }
示例#10
0
 public PayUClient(PayUClientSettings settings)
     : base(settings)
 {
 }
示例#11
0
 public BaseClient(PayUClientSettings settings, IHttpClientFactory clientFactory)
 {
     this.settings      = settings;
     this.clientFactory = clientFactory;
 }
示例#12
0
 public BaseClient(PayUClientSettings settings)
 {
     this.settings = settings;
 }
示例#13
0
 public PayUApiHttpCommunicator(IHttpClientFactory clientFactory, PayUClientSettings settings)
 {
     this.clientFactory = clientFactory;
     this.settings      = settings;
 }
 private static void AddBaseContent(PayUClientSettings settings, string grantType, Dictionary <string, string> content)
 {
     content.Add(PayUContainer.PropsName.Grant_Type, grantType);
     content.Add(PayUContainer.PropsName.Client_Id, settings.ClientId);
     content.Add(PayUContainer.PropsName.Client_Secret, settings.ClientSecret);
 }