forked from LiveCarta/PayPal-PHP-SDK
Initial commit
This commit is contained in:
74
lib/PayPal/Rest/ApiContext.php
Normal file
74
lib/PayPal/Rest/ApiContext.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace PayPal\Rest;
|
||||
|
||||
/**
|
||||
*
|
||||
* Call level parameters such as
|
||||
* request id, credentials etc
|
||||
*/
|
||||
class ApiContext {
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
59
lib/PayPal/Rest/Call.php
Normal file
59
lib/PayPal/Rest/Call.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
namespace PayPal\Rest;
|
||||
|
||||
use PayPal\Auth\OAuthTokenCredential;
|
||||
|
||||
use PayPal\Common\UserAgent;
|
||||
|
||||
class Call {
|
||||
|
||||
private $logger;
|
||||
|
||||
public function __construct() {
|
||||
$this->logger = new \PPLoggingManager(__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $data
|
||||
* @param PayPal/Rest/ApiContext $apiContext
|
||||
* @param array $headers
|
||||
*/
|
||||
public function execute($path, $method, $data='', $apiContext, $headers=array()) {
|
||||
$configMgr = \PPConfigManager::getInstance();
|
||||
|
||||
$credential = $apiContext->getCredential();
|
||||
if($credential == NULL) {
|
||||
// Try picking credentials from the config file
|
||||
$credMgr = \PPCredentialManager::getInstance();
|
||||
$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");
|
||||
}
|
||||
|
||||
$resourceUrl = rtrim( trim($configMgr->get('service.EndPoint')), '/') . $path;
|
||||
$config = new \PPHttpConfig($resourceUrl, $method);
|
||||
$headers += array(
|
||||
'Content-Type' => 'application/json',
|
||||
'User-Agent' => UserAgent::getValue()
|
||||
);
|
||||
if(!is_null($credential) && $credential instanceof OAuthTokenCredential) {
|
||||
$headers['Authorization'] = "Bearer " . $credential->getAccessToken();
|
||||
}
|
||||
if($method == 'POST' || $method == 'PUT') {
|
||||
$headers['PayPal-Request-Id'] = $apiContext->getRequestId();
|
||||
}
|
||||
$config->setHeaders($headers);
|
||||
$connection = new \PPHttpConnection($config);
|
||||
$response = $connection->execute($data);
|
||||
$this->logger->fine($response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
7
lib/PayPal/Rest/IResource.php
Normal file
7
lib/PayPal/Rest/IResource.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Rest;
|
||||
|
||||
interface IResource {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user