private void Authenticate()
        {
            object body = new
            {
                grant_type = "client_credentials"
            };

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebRequest request     = this.httpHelper.GetWebRequest("/authorize", "post", null);
            string     credentials = string.Format("{0}:{1}", this.clientId, this.clientSecret);
            string     encodedAuth = Convert.ToBase64String(Encoding.GetEncoding("UTF-8").GetBytes(credentials));

            request.Headers.Add("Authorization", string.Format("Basic {0}", encodedAuth));
            request.Headers.Add("api-sdk", string.Format("dotnet-{0}", Endpoints.Version));

            try
            {
                dynamic response = this.httpHelper.SendRequest(request, body);
                this.token = response.access_token;
            }
            catch (WebException ex)
            {
                throw GnException.Build("", 401);
            }
        }
        private object RequestEndpoint(string endpoint, string method, object query, object body)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebRequest request = this.httpHelper.GetWebRequest(endpoint, method, query);

            request.Headers.Add("Authorization", string.Format("Bearer {0}", this.token));
            request.Headers.Add("api-sdk", string.Format("dotnet-{0}", Version));
            if (partnerToken != null)
            {
                request.Headers.Add("partner-token", this.partnerToken);
            }

            try
            {
                //return httpHelper.SendRequestNoJsonObject(request, body);
                return(httpHelper.SendRequest(request, body));
            }
            catch (WebException e)
            {
                if (e.Response != null && (e.Response is HttpWebResponse))
                {
                    var          statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
                    StreamReader reader     = new StreamReader(e.Response.GetResponseStream());
                    throw GnException.Build(reader.ReadToEnd(), statusCode);
                }
                else
                {
                    throw GnException.Build("", 500);
                }
            }
        }
Exemplo n.º 3
0
        private object RequestEndpoint(string endpoint, string method, object query, object body)
        {
            WebRequest request = this.httpHelper.GetWebRequest(endpoint, method, query);

            request.Headers.Add("Authorization", string.Format("Bearer {0}", this.token));
            request.Headers.Add("api-sdk", string.Format("dotnet-{0}", Version));

            try
            {
                return(httpHelper.SendRequest(request, body));
            }
            catch (WebException e)
            {
                if (e.Response != null && (e.Response is HttpWebResponse))
                {
                    var          statusCode = (int)((HttpWebResponse)e.Response).StatusCode;
                    StreamReader reader     = new StreamReader(e.Response.GetResponseStream());
                    throw GnException.Build(reader.ReadToEnd(), statusCode);
                }
                else
                {
                    throw GnException.Build("", 500);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Método para criar uma transação
        /// </summary>
        /// <param name="transaction">Objeto da classe TransactionRequest que é obrigatório.</param>
        /// <returns></returns>
        public TransactionResponse CreateTransaction(TransactionRequest transaction)
        {
            //var body = JsonConvert.SerializeObject(transaction);
            var response = this._endpoints.CreateCharge(null, transaction);

            int code = response.code;

            if (code != 200)
            {
                string json = response;
                throw GnException.Build(json, code);
            }

            var data = JsonConvert.DeserializeObject <TransactionResponse>(response.data.ToString());

            //var data = response.data.ToString();

            return(data);
        }