Initial Commit

This commit is contained in:
2024-02-08 12:07:49 -07:00
parent 5813b1109f
commit 43077b57ed
5471 changed files with 682195 additions and 0 deletions
@@ -0,0 +1,25 @@
<?php
namespace PayPalCheckoutSdk\Core;
class AccessToken
{
public $token;
public $tokenType;
public $expiresIn;
private $createDate;
public function __construct($token, $tokenType, $expiresIn)
{
$this->token = $token;
$this->tokenType = $tokenType;
$this->expiresIn = $expiresIn;
$this->createDate = time();
}
public function isExpired()
{
return time() >= $this->createDate + $this->expiresIn;
}
}
@@ -0,0 +1,27 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\HttpRequest;
class AccessTokenRequest extends HttpRequest
{
public function __construct(PayPalEnvironment $environment, $refreshToken = NULL)
{
parent::__construct("/v1/oauth2/token", "POST");
$this->headers["Authorization"] = "Basic " . $environment->authorizationString();
$body = [
"grant_type" => "client_credentials"
];
if (!is_null($refreshToken))
{
$body["grant_type"] = "refresh_token";
$body["refresh_token"] = $refreshToken;
}
$this->body = $body;
$this->headers["Content-Type"] = "application/x-www-form-urlencoded";
}
}
@@ -0,0 +1,51 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\HttpRequest;
use PayPalHttp\Injector;
use PayPalHttp\HttpClient;
class AuthorizationInjector implements Injector
{
private $client;
private $environment;
private $refreshToken;
public $accessToken;
public function __construct(HttpClient $client, PayPalEnvironment $environment, $refreshToken)
{
$this->client = $client;
$this->environment = $environment;
$this->refreshToken = $refreshToken;
}
public function inject($request)
{
if (!$this->hasAuthHeader($request) && !$this->isAuthRequest($request))
{
if (is_null($this->accessToken) || $this->accessToken->isExpired())
{
$this->accessToken = $this->fetchAccessToken();
}
$request->headers['Authorization'] = 'Bearer ' . $this->accessToken->token;
}
}
private function fetchAccessToken()
{
$accessTokenResponse = $this->client->execute(new AccessTokenRequest($this->environment, $this->refreshToken));
$accessToken = $accessTokenResponse->result;
return new AccessToken($accessToken->access_token, $accessToken->token_type, $accessToken->expires_in);
}
private function isAuthRequest($request)
{
return $request instanceof AccessTokenRequest || $request instanceof RefreshTokenRequest;
}
private function hasAuthHeader(HttpRequest $request)
{
return array_key_exists("Authorization", $request->headers);
}
}
@@ -0,0 +1,16 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\Injector;
class FPTIInstrumentationInjector implements Injector
{
public function inject($request)
{
$request->headers["sdk_name"] = "Checkout SDK";
$request->headers["sdk_version"] = "1.0.1";
$request->headers["sdk_tech_stack"] = "PHP " . PHP_VERSION;
$request->headers["api_integration_type"] = "PAYPALSDK";
}
}
@@ -0,0 +1,14 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\Injector;
class GzipInjector implements Injector
{
public function inject($httpRequest)
{
$httpRequest->headers["Accept-Encoding"] = "gzip";
}
}
@@ -0,0 +1,23 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\Environment;
abstract class PayPalEnvironment implements Environment
{
private $clientId;
private $clientSecret;
public function __construct($clientId, $clientSecret)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
}
public function authorizationString()
{
return base64_encode($this->clientId . ":" . $this->clientSecret);
}
}
@@ -0,0 +1,27 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\HttpClient;
class PayPalHttpClient extends HttpClient
{
private $refreshToken;
public $authInjector;
public function __construct(PayPalEnvironment $environment, $refreshToken = NULL)
{
parent::__construct($environment);
$this->refreshToken = $refreshToken;
$this->authInjector = new AuthorizationInjector($this, $environment, $refreshToken);
$this->addInjector($this->authInjector);
$this->addInjector(new GzipInjector());
$this->addInjector(new FPTIInstrumentationInjector());
}
public function userAgent()
{
return UserAgent::getValue();
}
}
@@ -0,0 +1,16 @@
<?php
namespace PayPalCheckoutSdk\Core;
class ProductionEnvironment extends PayPalEnvironment
{
public function __construct($clientId, $clientSecret)
{
parent::__construct($clientId, $clientSecret);
}
public function baseUrl()
{
return "https://api.paypal.com";
}
}
@@ -0,0 +1,19 @@
<?php
namespace PayPalCheckoutSdk\Core;
use PayPalHttp\HttpRequest;
class RefreshTokenRequest extends HttpRequest
{
public function __construct(PayPalEnvironment $environment, $authorizationCode)
{
parent::__construct("/v1/identity/openidconnect/tokenservice", "POST");
$this->headers["Authorization"] = "Basic " . $environment->authorizationString();
$this->headers["Content-Type"] = "application/x-www-form-urlencoded";
$this->body = [
"grant_type" => "authorization_code",
"code" => $authorizationCode
];
}
}
@@ -0,0 +1,16 @@
<?php
namespace PayPalCheckoutSdk\Core;
class SandboxEnvironment extends PayPalEnvironment
{
public function __construct($clientId, $clientSecret)
{
parent::__construct($clientId, $clientSecret);
}
public function baseUrl()
{
return "https://api.sandbox.paypal.com";
}
}
@@ -0,0 +1,52 @@
<?php
namespace PayPalCheckoutSdk\Core;
/**
* Class PayPalUserAgent
* PayPalUserAgent generates User Agent for curl requests
*
* @package PayPal\Core
*/
class UserAgent
{
/**
* Returns the value of the User-Agent header
* Add environment values and php version numbers
*
* @return string
*/
public static function getValue()
{
$featureList = array(
'platform-ver=' . PHP_VERSION,
'bit=' . self::_getPHPBit(),
'os=' . str_replace(' ', '_', php_uname('s') . ' ' . php_uname('r')),
'machine=' . php_uname('m')
);
if (defined('OPENSSL_VERSION_TEXT')) {
$opensslVersion = explode(' ', OPENSSL_VERSION_TEXT);
$featureList[] = 'crypto-lib-ver=' . $opensslVersion[1];
}
if (function_exists('curl_version')) {
$curlVersion = curl_version();
$featureList[] = 'curl=' . $curlVersion['version'];
}
return sprintf("PayPalSDK/%s %s (%s)", "Checkout-PHP-SDK", Version::VERSION, implode('; ', $featureList));
}
/**
* Gets PHP Bit version
*
* @return int|string
*/
private static function _getPHPBit()
{
switch (PHP_INT_SIZE) {
case 4:
return '32';
case 8:
return '64';
default:
return PHP_INT_SIZE;
}
}
}
@@ -0,0 +1,8 @@
<?php
namespace PayPalCheckoutSdk\Core;
class Version
{
const VERSION = "1.0.1";
}