forked from LiveCarta/PayPal-PHP-SDK
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:
@@ -3,16 +3,13 @@
|
|||||||
namespace PayPal\Auth;
|
namespace PayPal\Auth;
|
||||||
|
|
||||||
use PayPal\Cache\AuthorizationCache;
|
use PayPal\Cache\AuthorizationCache;
|
||||||
use PayPal\Common\PPModel;
|
|
||||||
use PayPal\Common\PPUserAgent;
|
|
||||||
use PayPal\Common\ResourceModel;
|
use PayPal\Common\ResourceModel;
|
||||||
use PayPal\Core\PPConstants;
|
|
||||||
use PayPal\Core\PPHttpConfig;
|
use PayPal\Core\PPHttpConfig;
|
||||||
use PayPal\Core\PPHttpConnection;
|
use PayPal\Core\PPHttpConnection;
|
||||||
use PayPal\Core\PPLoggingManager;
|
use PayPal\Core\PPLoggingManager;
|
||||||
use PayPal\Exception\PPConfigurationException;
|
use PayPal\Exception\PPConfigurationException;
|
||||||
use PayPal\Rest\RestHandler;
|
use PayPal\Handler\IPPHandler;
|
||||||
use PayPal\Validation\JsonValidator;
|
use PayPal\Rest\ApiContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OAuthTokenCredential
|
* Class OAuthTokenCredential
|
||||||
@@ -21,6 +18,12 @@ class OAuthTokenCredential extends ResourceModel
|
|||||||
{
|
{
|
||||||
|
|
||||||
public static $CACHE_PATH = '/../../../var/auth.cache';
|
public static $CACHE_PATH = '/../../../var/auth.cache';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string Default Auth Handler
|
||||||
|
*/
|
||||||
|
public static $AUTH_HANDLER = 'PayPal\Rest\OauthHandler';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private Variable
|
* Private Variable
|
||||||
*
|
*
|
||||||
@@ -192,6 +195,8 @@ class OAuthTokenCredential extends ResourceModel
|
|||||||
* Retrieves the token based on the input configuration
|
* Retrieves the token based on the input configuration
|
||||||
*
|
*
|
||||||
* @param array $config
|
* @param array $config
|
||||||
|
* @param string $clientId
|
||||||
|
* @param string $clientSecret
|
||||||
* @param string $payload
|
* @param string $payload
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws PPConfigurationException
|
* @throws PPConfigurationException
|
||||||
@@ -199,17 +204,20 @@ class OAuthTokenCredential extends ResourceModel
|
|||||||
*/
|
*/
|
||||||
private function getToken($config, $clientId, $clientSecret, $payload)
|
private function getToken($config, $clientId, $clientSecret, $payload)
|
||||||
{
|
{
|
||||||
$base64ClientID = base64_encode($clientId . ":" . $clientSecret);
|
$httpConfig = new PPHttpConfig(null, 'POST');
|
||||||
$headers = array(
|
|
||||||
"User-Agent" => PPUserAgent::getValue(PPConstants::SDK_NAME, PPConstants::SDK_VERSION),
|
|
||||||
"Authorization" => "Basic " . $base64ClientID,
|
|
||||||
"Accept" => "*/*"
|
|
||||||
);
|
|
||||||
|
|
||||||
$httpConfiguration = self::getOAuthHttpConfiguration($config);
|
$handlers = array(self::$AUTH_HANDLER);
|
||||||
$httpConfiguration->setHeaders($headers);
|
|
||||||
|
|
||||||
$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);
|
$res = $connection->execute($payload);
|
||||||
$response = json_decode($res, true);
|
$response = json_decode($res, true);
|
||||||
|
|
||||||
@@ -249,40 +257,4 @@ class OAuthTokenCredential extends ResourceModel
|
|||||||
|
|
||||||
return $this->accessToken;
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class PPConstants
|
|||||||
{
|
{
|
||||||
|
|
||||||
const SDK_NAME = 'PayPal-PHP-SDK';
|
const SDK_NAME = 'PayPal-PHP-SDK';
|
||||||
const SDK_VERSION = '0.16.0';
|
const SDK_VERSION = '0.16.1';
|
||||||
|
|
||||||
const REST_SANDBOX_ENDPOINT = "https://api.sandbox.paypal.com/";
|
const REST_SANDBOX_ENDPOINT = "https://api.sandbox.paypal.com/";
|
||||||
const OPENID_REDIRECT_SANDBOX_URL = "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect";
|
const OPENID_REDIRECT_SANDBOX_URL = "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect";
|
||||||
|
|||||||
@@ -58,6 +58,26 @@ class ApiContext
|
|||||||
return $this->credential;
|
return $this->credential;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getRequestHeaders()
|
||||||
|
{
|
||||||
|
$result = PPConfigManager::getInstance()->get('http.headers');
|
||||||
|
$headers = array();
|
||||||
|
foreach ($result as $header => $value) {
|
||||||
|
$headerName = ltrim($header, 'http.headers');
|
||||||
|
$headers[$headerName] = $value;
|
||||||
|
}
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRequestHeader($name, $value)
|
||||||
|
{
|
||||||
|
// Determine if the name already has a 'http.headers' prefix. If not, add one.
|
||||||
|
if (!(substr($name, 0, strlen('http.headers')) === 'http.headers')) {
|
||||||
|
$name = 'http.headers.' . $name;
|
||||||
|
}
|
||||||
|
PPConfigManager::getInstance()->addConfigs(array($name => $value));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Request ID
|
* Get Request ID
|
||||||
*
|
*
|
||||||
|
|||||||
105
lib/PayPal/Rest/OauthHandler.php
Normal file
105
lib/PayPal/Rest/OauthHandler.php
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* API handler for OAuth Token Request REST API calls
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace PayPal\Rest;
|
||||||
|
|
||||||
|
use PayPal\Common\PPUserAgent;
|
||||||
|
use PayPal\Core\PPConstants;
|
||||||
|
use PayPal\Core\PPHttpConfig;
|
||||||
|
use PayPal\Exception\PPConfigurationException;
|
||||||
|
use PayPal\Exception\PPInvalidCredentialException;
|
||||||
|
use PayPal\Exception\PPMissingCredentialException;
|
||||||
|
use PayPal\Handler\IPPHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class OauthHandler
|
||||||
|
*/
|
||||||
|
class OauthHandler implements IPPHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Private Variable
|
||||||
|
*
|
||||||
|
* @var \Paypal\Rest\ApiContext $apiContext
|
||||||
|
*/
|
||||||
|
private $apiContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct
|
||||||
|
*
|
||||||
|
* @param \Paypal\Rest\ApiContext $apiContext
|
||||||
|
*/
|
||||||
|
public function __construct($apiContext)
|
||||||
|
{
|
||||||
|
$this->apiContext = $apiContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param PPHttpConfig $httpConfig
|
||||||
|
* @param string $request
|
||||||
|
* @param mixed $options
|
||||||
|
* @return mixed|void
|
||||||
|
* @throws PPConfigurationException
|
||||||
|
* @throws PPInvalidCredentialException
|
||||||
|
* @throws PPMissingCredentialException
|
||||||
|
*/
|
||||||
|
public function handle($httpConfig, $request, $options)
|
||||||
|
{
|
||||||
|
$config = $this->apiContext->getConfig();
|
||||||
|
|
||||||
|
$httpConfig->setUrl(
|
||||||
|
rtrim(trim($this->_getEndpoint($config)), '/') .
|
||||||
|
(isset($options['path']) ? $options['path'] : '')
|
||||||
|
);
|
||||||
|
|
||||||
|
$headers = array(
|
||||||
|
"User-Agent" => PPUserAgent::getValue(PPConstants::SDK_NAME, PPConstants::SDK_VERSION),
|
||||||
|
"Authorization" => "Basic " . base64_encode($options['clientId'] . ":" . $options['clientSecret']),
|
||||||
|
"Accept" => "*/*"
|
||||||
|
);
|
||||||
|
$httpConfig->setHeaders($headers);
|
||||||
|
|
||||||
|
// Add any additional Headers that they may have provided
|
||||||
|
$headers = $this->apiContext->getRequestHeaders();
|
||||||
|
foreach ($headers as $key => $value) {
|
||||||
|
$httpConfig->addHeader($key, $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get HttpConfiguration object for OAuth API
|
||||||
|
*
|
||||||
|
* @param array $config
|
||||||
|
*
|
||||||
|
* @return PPHttpConfig
|
||||||
|
* @throws \PayPal\Exception\PPConfigurationException
|
||||||
|
*/
|
||||||
|
private static function _getEndpoint($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), '/') . "/v1/oauth2/token";
|
||||||
|
|
||||||
|
return $baseEndpoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -84,6 +84,11 @@ class RestHandler implements IPPHandler
|
|||||||
if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
|
if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') {
|
||||||
$httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
|
$httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
|
||||||
}
|
}
|
||||||
|
// Add any additional Headers that they may have provided
|
||||||
|
$headers = $this->apiContext->getRequestHeaders();
|
||||||
|
foreach ($headers as $key => $value) {
|
||||||
|
$httpConfig->addHeader($key, $value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
PayPal PHP SDK release notes
|
PayPal PHP SDK release notes
|
||||||
============================
|
============================
|
||||||
|
v0.16.1
|
||||||
|
----
|
||||||
|
* Configurable Headers for all requests to PayPal
|
||||||
|
* Allows adding additional headers to every call to PayPal APIs
|
||||||
|
* SDK Config to add headers with http.headers.* syntax
|
||||||
|
|
||||||
v0.16.0
|
v0.16.0
|
||||||
----
|
----
|
||||||
* Enabled Webhook Management Capabilities
|
* Enabled Webhook Management Capabilities
|
||||||
|
|||||||
@@ -62,6 +62,11 @@ function getApiContext($clientId, $clientSecret)
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Partner Attribution Id
|
||||||
|
// Use this header if you are a PayPal partner. Specify a unique BN Code to receive revenue attribution. To learn more or to request a BN Code, contact your Partner Manager or visit the PayPal Partner Portal .
|
||||||
|
|
||||||
|
$apiContext->addRequestHeader('PayPal-Partner-Attribution-Id', '123123123');
|
||||||
|
|
||||||
|
|
||||||
// #### SDK configuration
|
// #### SDK configuration
|
||||||
|
|
||||||
@@ -76,7 +81,8 @@ function getApiContext($clientId, $clientSecret)
|
|||||||
'log.FileName' => '../PayPal.log',
|
'log.FileName' => '../PayPal.log',
|
||||||
'log.LogLevel' => 'FINE',
|
'log.LogLevel' => 'FINE',
|
||||||
'validation.level' => 'log',
|
'validation.level' => 'log',
|
||||||
'cache.enabled' => 'true'
|
'cache.enabled' => 'true',
|
||||||
|
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ acct1.ClientSecret = EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRb
|
|||||||
[Http]
|
[Http]
|
||||||
http.ConnectionTimeOut = 30
|
http.ConnectionTimeOut = 30
|
||||||
http.Retry = 1
|
http.Retry = 1
|
||||||
|
|
||||||
|
; Adding HTTP Headers to each request sent to PayPal APIs
|
||||||
|
;http.headers.PayPal-Partner-Attribution-Id = 123123123
|
||||||
|
|
||||||
;http.Proxy=http://[username:password]@hostname[:port]
|
;http.Proxy=http://[username:password]@hostname[:port]
|
||||||
|
|
||||||
;Service Configuration
|
;Service Configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user