Configurable Headers for All Requests to PayPal

- Allows adding additional headers to every call to PayPal APIs.
- Refactored OAuthTokenCredential to share code for making Rest Calls
- SDK Config to add headers with http.headers.* syntax
- Fixes #193
This commit is contained in:
japatel
2014-12-17 15:36:41 -06:00
parent 515b747223
commit 20038e7525
8 changed files with 170 additions and 52 deletions

View File

@@ -3,16 +3,13 @@
namespace PayPal\Auth;
use PayPal\Cache\AuthorizationCache;
use PayPal\Common\PPModel;
use PayPal\Common\PPUserAgent;
use PayPal\Common\ResourceModel;
use PayPal\Core\PPConstants;
use PayPal\Core\PPHttpConfig;
use PayPal\Core\PPHttpConnection;
use PayPal\Core\PPLoggingManager;
use PayPal\Exception\PPConfigurationException;
use PayPal\Rest\RestHandler;
use PayPal\Validation\JsonValidator;
use PayPal\Handler\IPPHandler;
use PayPal\Rest\ApiContext;
/**
* Class OAuthTokenCredential
@@ -21,6 +18,12 @@ class OAuthTokenCredential extends ResourceModel
{
public static $CACHE_PATH = '/../../../var/auth.cache';
/**
* @var string Default Auth Handler
*/
public static $AUTH_HANDLER = 'PayPal\Rest\OauthHandler';
/**
* Private Variable
*
@@ -192,6 +195,8 @@ class OAuthTokenCredential extends ResourceModel
* Retrieves the token based on the input configuration
*
* @param array $config
* @param string $clientId
* @param string $clientSecret
* @param string $payload
* @return mixed
* @throws PPConfigurationException
@@ -199,17 +204,20 @@ class OAuthTokenCredential extends ResourceModel
*/
private function getToken($config, $clientId, $clientSecret, $payload)
{
$base64ClientID = base64_encode($clientId . ":" . $clientSecret);
$headers = array(
"User-Agent" => PPUserAgent::getValue(PPConstants::SDK_NAME, PPConstants::SDK_VERSION),
"Authorization" => "Basic " . $base64ClientID,
"Accept" => "*/*"
);
$httpConfig = new PPHttpConfig(null, 'POST');
$httpConfiguration = self::getOAuthHttpConfiguration($config);
$httpConfiguration->setHeaders($headers);
$handlers = array(self::$AUTH_HANDLER);
$connection = new PPHttpConnection($httpConfiguration, $config);
/** @var IPPHandler $handler */
foreach ($handlers as $handler) {
if (!is_object($handler)) {
$fullHandler = "\\" . (string)$handler;
$handler = new $fullHandler(new ApiContext($this));
}
$handler->handle($httpConfig, $payload, array('clientId' => $clientId, 'clientSecret' => $clientSecret));
}
$connection = new PPHttpConnection($httpConfig, $config);
$res = $connection->execute($payload);
$response = json_decode($res, true);
@@ -249,40 +257,4 @@ class OAuthTokenCredential extends ResourceModel
return $this->accessToken;
}
/**
* Get HttpConfiguration object for OAuth API
*
* @param array $config
*
* @return PPHttpConfig
* @throws \PayPal\Exception\PPConfigurationException
*/
private static function getOAuthHttpConfiguration($config)
{
if (isset($config['oauth.EndPoint'])) {
$baseEndpoint = $config['oauth.EndPoint'];
} else if (isset($config['service.EndPoint'])) {
$baseEndpoint = $config['service.EndPoint'];
} else if (isset($config['mode'])) {
switch (strtoupper($config['mode'])) {
case 'SANDBOX':
$baseEndpoint = PPConstants::REST_SANDBOX_ENDPOINT;
break;
case 'LIVE':
$baseEndpoint = PPConstants::REST_LIVE_ENDPOINT;
break;
default:
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
}
} else {
throw new PPConfigurationException(
'You must set one of service.endpoint or mode parameters in your configuration'
);
}
$baseEndpoint = rtrim(trim($baseEndpoint), '/');
return new PPHttpConfig($baseEndpoint . "/v1/oauth2/token", "POST");
}
}