public function handle($httpConfig, $request, $options) { $credential = $this->apiContext->getCredential(); $config = $this->apiContext->getConfig(); if ($credential == NULL) { // Try picking credentials from the config file $credMgr = PPCredentialManager::getInstance($config); $credValues = $credMgr->getCredentialObject(); if (!is_array($credValues)) { throw new PPMissingCredentialException("Empty or invalid credentials passed"); } $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']); } if ($credential == NULL || !$credential instanceof OAuthTokenCredential) { throw new PPInvalidCredentialException("Invalid credentials passed"); } $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : '')); if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { $httpConfig->addHeader("User-Agent", PPUserAgent::getValue(self::$sdkName, self::$sdkVersion)); } if (!is_null($credential) && $credential instanceof OAuthTokenCredential) { $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config)); } if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') { $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId()); } }
public function handle($httpConfig, $request, $options) { $config = $this->apiContext->getConfig(); $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : '')); if (!array_key_exists("Authorization", $httpConfig->getHeaders())) { $auth = base64_encode($config['acct1.ClientId'] . ':' . $config['acct1.ClientSecret']); $httpConfig->addHeader("Authorization", "Basic {$auth}"); } if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { $httpConfig->addHeader("User-Agent", PPUserAgent::getValue(self::$sdkName, self::$sdkVersion)); } }
public function handle($httpConfig, $request, $options) { $httpConfig->addHeader('X-PAYPAL-REQUEST-DATA-FORMAT', $request->getBindingType()); $httpConfig->addHeader('X-PAYPAL-RESPONSE-DATA-FORMAT', $request->getBindingType()); $httpConfig->addHeader('X-PAYPAL-DEVICE-IPADDRESS', PPUtils::getLocalIPAddress()); $httpConfig->addHeader('X-PAYPAL-REQUEST-SOURCE', $this->getRequestSource()); if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { $httpConfig->addHeader("User-Agent", PPUserAgent::getValue($this->sdkName, $this->sdkVersion)); } if (isset($options['config']['service.SandboxEmailAddress'])) { $httpConfig->addHeader('X-PAYPAL-SANDBOX-EMAIL-ADDRESS', $options['config']['service.SandboxEmailAddress']); } }
public function testGetValue() { $ua = PPUserAgent::getValue("name", "version"); list($id, $version, $features) = sscanf($ua, "PayPalSDK/%s %s (%s)"); // Check that we pass the useragent in the expected format $this->assertNotNull($id); $this->assertNotNull($version); $this->assertNotNull($features); $this->assertEquals("name", $id); $this->assertEquals("version", $version); // Check that we pass in these mininal features $this->assertThat($features, $this->stringContains("OS=")); $this->assertThat($features, $this->stringContains("Bit=")); $this->assertThat($features, $this->stringContains("Lang=")); $this->assertThat($features, $this->stringContains("V=")); $this->assertGreaterThan(5, count(explode(';', $features))); }
/** * Generates a new access token */ private function _generateAccessToken($config) { $base64ClientID = base64_encode($this->clientId . ":" . $this->clientSecret); $headers = array("User-Agent" => PPUserAgent::getValue(PPConstants::SDK_NAME, PPCONSTANTS::SDK_VERSION), "Authorization" => "Basic " . $base64ClientID, "Accept" => "*/*"); $httpConfiguration = $this->getOAuthHttpConfiguration($config); $httpConfiguration->setHeaders($headers); $connection = PPConnectionManager::getInstance()->getConnection($httpConfiguration, $config); $res = $connection->execute("grant_type=client_credentials"); $jsonResponse = json_decode($res, true); if ($jsonResponse == NULL || !isset($jsonResponse["access_token"]) || !isset($jsonResponse["expires_in"])) { $this->accessToken = NULL; $this->tokenExpiresIn = NULL; $this->logger->warning("Could not generate new Access token. Invalid response from server: " . $jsonResponse); } else { $this->accessToken = $jsonResponse["access_token"]; $this->tokenExpiresIn = $jsonResponse["expires_in"]; } $this->tokenCreateTime = time(); return $this->accessToken; }