Exemplo n.º 1
0
        public void setAuthenticationParamsForInvalidAccount()
        {
            ConnectionManager connMgr = ConnectionManager.Instance;
            AuthenticationHandler authHandler = new AuthenticationHandler("incomplete-credentials_api1.paypal.com");
            HttpWebRequest httpRequest = connMgr.GetConnection("http://paypal.com");

            authHandler.SetAuthenticationParams(httpRequest, "");
        }
Exemplo n.º 2
0
        public void testOAuthSignature()
        {
            ConnectionManager connMgr = ConnectionManager.Instance;
            HttpWebRequest httpRequest = connMgr.GetConnection("http://paypal.com");

            AuthenticationHandler authHandler = new AuthenticationHandler("jb-us-seller_api1.paypal.com");
            authHandler.SetOAuthToken("token", "tokenSecret");
            authHandler.SetAuthenticationParams(httpRequest, "http://svcs.sandbox.paypal.com/Service");
            Assert.Contains(BaseConstants.XPAYPALSECURITYOAUTHSIGN, httpRequest.Headers);
        }
Exemplo n.º 3
0
        public void setAuthenticationParamsForSignatureAccount()
        {
            ConnectionManager connMgr = ConnectionManager.Instance;
            AuthenticationHandler authHandler = new AuthenticationHandler("jb-us-seller_api1.paypal.com");
            HttpWebRequest httpRequest = connMgr.GetConnection("http://paypal.com");

            authHandler.SetAuthenticationParams(httpRequest, "");

            Assert.AreEqual(4, httpRequest.Headers.Count);
            List<string> expectedHeaders = new List<string>(new string[]
            {
                BaseConstants.XPAYPALSECURITYUSERID, BaseConstants.XPAYPALSECURITYPASSWORD,
                BaseConstants.XPAYPALAPPLICATIONID, BaseConstants.XPAYPALSECURITYSIGNATURE
            });
            foreach (string headerName in expectedHeaders)
                Assert.Contains(headerName, httpRequest.Headers);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Calls the platform API web service for given payload and returns the response payload.
        /// </summary>
        /// <returns>returns the response payload</returns>
        public string makeRequest(string method, string requestPayload, string apiUsername,
                                    string accessToken, string accessTokenSecret)
        {
            ConfigManager configMgr = ConfigManager.Instance;
            string uri, responseString = string.Empty;
            AuthenticationHandler authHandler = new AuthenticationHandler(apiUsername);
            // Construct the URL to invoke
            if (configMgr.GetProperty("binding") != "SOAP")
            {
                uri = getAPIEndpoint(method);

            }
            else
            {
                uri = configMgr.GetProperty("endpoint");

            }
            log.Debug("Connecting to " + uri);

            // Constructing HttpWebRequest object
            ConnectionManager conn = ConnectionManager.Instance;
            HttpWebRequest httpRequest = conn.getConnection(uri);
            httpRequest.Method = RequestMethod;

            // Set up Headers
            if(accessToken != null && accessTokenSecret != null)
                authHandler.SetOAuthToken(accessToken, accessTokenSecret);
            authHandler.SetAuthenticationParams(httpRequest, uri);
            if (configMgr.GetProperty("binding") == "SOAP")
                requestPayload = authHandler.appendSoapHeaders(requestPayload, accessToken, accessTokenSecret);
            else
            {
                httpRequest.Headers.Add(BaseConstants.XPAYPALREQUESTDATAFORMAT, BaseConstants.RequestDataformat);
                httpRequest.Headers.Add(BaseConstants.XPAYPALRESPONSEDATAFORMAT, BaseConstants.ResponseDataformat);
                httpRequest.Headers.Add(BaseConstants.XPAYPALDEVICEIPADDRESS, configMgr.GetProperty("IPAddress"));
            }
            // Add tracking header
            httpRequest.Headers.Add(BaseConstants.XPAYPALREQUESTSOURCE,
                    BaseConstants.SDK_NAME + "-" + BaseConstants.SDK_VERSION);

            if (log.IsDebugEnabled)
            {
                foreach (string headerName in httpRequest.Headers)
                {
                    log.Debug(headerName + ":" + httpRequest.Headers[headerName]);
                }
            }
            // Adding payLoad to HttpWebRequest object
            try
            {
                using (StreamWriter myWriter = new StreamWriter(httpRequest.GetRequestStream()))
                {
                    myWriter.Write(requestPayload);
                    log.Debug(requestPayload);
                }
            }
            catch (WebException ex)
            {
                throw new ConnectionException(ex.Message);
            }

            // Fire request. Retry if configured to do so
            int numRetries = (configMgr.GetProperty("requestRetries") != null ) ?
                Int32.Parse(configMgr.GetProperty("requestRetries")) : 0;
            int retries = 0;

            do {
                try
                {
                    // calling the plaftform API web service and getting the response
                    using (WebResponse response = httpRequest.GetResponse())
                    {
                        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                        {
                            responseString = sr.ReadToEnd();
                            log.Debug("Service response");
                            log.Debug(responseString);
                            return responseString;
                        }
                    }
                }
                // server responses in the range of 4xx and 5xx throw a WebException
                catch (WebException we)
                {
                    HttpStatusCode statusCode =  ( (HttpWebResponse) we.Response ).StatusCode;

                    log.Info("Got " + statusCode.ToString() + " response from server");
                    if (!requiresRetry(we))
                    {
                        throw new ConnectionException("Invalid HTTP response " + we.Message);
                    }
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            } while ( retries++ < numRetries);

            throw new ConnectionException("Invalid HTTP response");
        }