forked from LiveCarta/PayPal-PHP-SDK
Cleaned up Code Comments and added Type-Hinting to all Class/Functions
This commit is contained in:
@@ -1,75 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Call level parameters such as request id, credentials etc
|
||||
*/
|
||||
|
||||
namespace PayPal\Rest;
|
||||
|
||||
use PayPal\Common\PPApiContext;
|
||||
|
||||
/**
|
||||
*
|
||||
* Call level parameters such as
|
||||
* request id, credentials etc
|
||||
* Class ApiContext
|
||||
*/
|
||||
class ApiContext extends PPApiContext {
|
||||
|
||||
/**
|
||||
* OAuth Credentials to use for this call
|
||||
* @var PayPal/Api/OAuthTokenCredential
|
||||
*/
|
||||
private $credential;
|
||||
|
||||
/**
|
||||
* Unique request id to be used for this call
|
||||
* The user can either generate one as per application
|
||||
* needs or let the SDK generate one
|
||||
* @var string
|
||||
*/
|
||||
private $requestId;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function getCredential() {
|
||||
return $this->credential;
|
||||
}
|
||||
|
||||
public function getrequestId() {
|
||||
if($this->requestId == null) {
|
||||
$this->requestId = $this->generateRequestId();
|
||||
}
|
||||
return $this->requestId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param PayPal/Api/OAuthTokenCredential $credential
|
||||
* @param string $requestId
|
||||
*/
|
||||
public function __construct($credential, $requestId=null) {
|
||||
$this->credential = $credential;
|
||||
$this->requestId = $requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique per request id that
|
||||
* can be used to set the PayPal-Request-Id header
|
||||
* that is used for idemptency
|
||||
* @return string
|
||||
*/
|
||||
private function generateRequestId() {
|
||||
|
||||
static $pid = -1;
|
||||
static $addr = -1;
|
||||
|
||||
if ($pid == -1) {
|
||||
$pid = getmypid();
|
||||
}
|
||||
if ($addr == -1) {
|
||||
if(array_key_exists('SERVER_ADDR', $_SERVER)) {
|
||||
$addr = ip2long($_SERVER['SERVER_ADDR']);
|
||||
} else {
|
||||
$addr = php_uname('n');
|
||||
}
|
||||
}
|
||||
|
||||
return $addr . $pid . $_SERVER['REQUEST_TIME'] . mt_rand(0, 0xffff);
|
||||
}
|
||||
class ApiContext extends PPApiContext
|
||||
{
|
||||
/**
|
||||
* OAuth Credentials to use for this call
|
||||
*
|
||||
* @var \PayPal\Auth\OAuthTokenCredential $credential
|
||||
*/
|
||||
private $credential;
|
||||
|
||||
/**
|
||||
* Unique request id to be used for this call
|
||||
* The user can either generate one as per application
|
||||
* needs or let the SDK generate one
|
||||
*
|
||||
* @var null|string $requestId
|
||||
*/
|
||||
private $requestId;
|
||||
|
||||
/**
|
||||
* Get Credential
|
||||
*
|
||||
* @return \PayPal\Auth\OAuthTokenCredential
|
||||
*/
|
||||
public function getCredential()
|
||||
{
|
||||
return $this->credential;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Request ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getrequestId()
|
||||
{
|
||||
if ($this->requestId == null) {
|
||||
$this->requestId = $this->generateRequestId();
|
||||
}
|
||||
|
||||
return $this->requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param \PayPal\Auth\OAuthTokenCredential $credential
|
||||
* @param string|null $requestId
|
||||
*/
|
||||
public function __construct($credential, $requestId = null)
|
||||
{
|
||||
$this->credential = $credential;
|
||||
$this->requestId = $requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique per request id that
|
||||
* can be used to set the PayPal-Request-Id header
|
||||
* that is used for idemptency
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function generateRequestId()
|
||||
{
|
||||
static $pid = -1;
|
||||
static $addr = -1;
|
||||
|
||||
if ($pid == -1) {
|
||||
$pid = getmypid();
|
||||
}
|
||||
|
||||
if ($addr == -1) {
|
||||
if (array_key_exists('SERVER_ADDR', $_SERVER)) {
|
||||
$addr = ip2long($_SERVER['SERVER_ADDR']);
|
||||
} else {
|
||||
$addr = php_uname('n');
|
||||
}
|
||||
}
|
||||
|
||||
return $addr . $pid . $_SERVER['REQUEST_TIME'] . mt_rand(0, 0xffff);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace PayPal\Rest;
|
||||
|
||||
interface IResource {
|
||||
|
||||
}
|
||||
/**
|
||||
* Interface IResource
|
||||
*/
|
||||
interface IResource
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,84 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* API handler for all REST API calls
|
||||
*/
|
||||
|
||||
namespace PayPal\Rest;
|
||||
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
use PayPal\Handler\IPPHandler;
|
||||
use PayPal\Core\PPCredentialManager;
|
||||
use PayPal\Core\PPConstants;
|
||||
use PayPal\Exception\PPMissingCredentialException;
|
||||
use PayPal\Exception\PPInvalidCredentialException;
|
||||
use PayPal\Exception\PPConfigurationException;
|
||||
use PayPal\Common\PPUserAgent;
|
||||
use PayPal\Core\PPConstants;
|
||||
use PayPal\Core\PPCredentialManager;
|
||||
use PayPal\Exception\PPConfigurationException;
|
||||
use PayPal\Exception\PPInvalidCredentialException;
|
||||
use PayPal\Exception\PPMissingCredentialException;
|
||||
use PayPal\Handler\IPPHandler;
|
||||
|
||||
/**
|
||||
*
|
||||
* API handler for all REST API calls
|
||||
* Class RestHandler
|
||||
*/
|
||||
class RestHandler implements IPPHandler {
|
||||
class RestHandler implements IPPHandler
|
||||
{
|
||||
/**
|
||||
* Private Variable
|
||||
*
|
||||
* @var \Paypal\Rest\ApiContext $apiContext
|
||||
*/
|
||||
private $apiContext;
|
||||
|
||||
private $apiContext;
|
||||
|
||||
public static $sdkName = "rest-sdk-php";
|
||||
public static $sdkVersion = "0.6.0";
|
||||
|
||||
public function __construct($apiContext) {
|
||||
$this->apiContext = $apiContext;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
/**
|
||||
* Public Variable
|
||||
*
|
||||
* @var string $sdkName
|
||||
*/
|
||||
public static $sdkName = "rest-sdk-php";
|
||||
|
||||
|
||||
$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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function _getEndpoint($config) {
|
||||
if (isset($config['service.EndPoint'])) {
|
||||
return $config['service.EndPoint'];
|
||||
} else if (isset($config['mode'])) {
|
||||
switch (strtoupper($config['mode'])) {
|
||||
case 'SANDBOX':
|
||||
return PPConstants::REST_SANDBOX_ENDPOINT;
|
||||
break;
|
||||
case 'LIVE':
|
||||
return PPConstants::REST_LIVE_ENDPOINT;
|
||||
break;
|
||||
default:
|
||||
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
throw new PPConfigurationException('You must set one of service.endpoint or mode parameters in your configuration');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Public Variable
|
||||
*
|
||||
* @var string $sdkVersion
|
||||
*/
|
||||
public static $sdkVersion = "0.6.0";
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param \Paypal\Rest\ApiContext $apiContext
|
||||
*/
|
||||
public function __construct($apiContext)
|
||||
{
|
||||
$this->apiContext = $apiContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle
|
||||
*
|
||||
* @param \PayPal\Core\PPHttpConfig $httpConfig
|
||||
* @param \PayPal\Core\PPRequest $request
|
||||
* @param array $options
|
||||
*
|
||||
* @throws \PayPal\Exception\PPInvalidCredentialException
|
||||
* @throws \PayPal\Exception\PPMissingCredentialException
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* End Point
|
||||
*
|
||||
* @param array $config
|
||||
*
|
||||
* @return string
|
||||
* @throws \PayPal\Exception\PPConfigurationException
|
||||
*/
|
||||
private function _getEndpoint($config)
|
||||
{
|
||||
if (isset($config['service.EndPoint'])) {
|
||||
return $config['service.EndPoint'];
|
||||
} else if (isset($config['mode'])) {
|
||||
switch (strtoupper($config['mode'])) {
|
||||
case 'SANDBOX':
|
||||
return PPConstants::REST_SANDBOX_ENDPOINT;
|
||||
break;
|
||||
case 'LIVE':
|
||||
return PPConstants::REST_LIVE_ENDPOINT;
|
||||
break;
|
||||
default:
|
||||
throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
throw new PPConfigurationException('You must set one of service.endpoint or mode parameters in your configuration');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user