Overview
  • Namespace
  • Class

Namespaces

  • PayPal
    • Api
    • Auth
    • Cache
    • Common
    • Converter
    • Core
    • Exception
    • Handler
    • Log
    • Rest
    • Security
    • Transport
    • Validation

Classes

  • PayPal\Api\Address
  • PayPal\Api\Agreement
  • PayPal\Api\AgreementDetails
  • PayPal\Api\AgreementStateDescriptor
  • PayPal\Api\AgreementTransaction
  • PayPal\Api\AgreementTransactions
  • PayPal\Api\AlternatePayment
  • PayPal\Api\Amount
  • PayPal\Api\Authorization
  • PayPal\Api\BankAccount
  • PayPal\Api\BankAccountsList
  • PayPal\Api\BankToken
  • PayPal\Api\BaseAddress
  • PayPal\Api\Billing
  • PayPal\Api\BillingAgreementToken
  • PayPal\Api\BillingInfo
  • PayPal\Api\CancelNotification
  • PayPal\Api\Capture
  • PayPal\Api\CarrierAccount
  • PayPal\Api\CarrierAccountToken
  • PayPal\Api\CartBase
  • PayPal\Api\ChargeModel
  • PayPal\Api\Cost
  • PayPal\Api\CountryCode
  • PayPal\Api\CreateProfileResponse
  • PayPal\Api\Credit
  • PayPal\Api\CreditCard
  • PayPal\Api\CreditCardHistory
  • PayPal\Api\CreditCardList
  • PayPal\Api\CreditCardToken
  • PayPal\Api\CreditFinancingOffered
  • PayPal\Api\Currency
  • PayPal\Api\CurrencyConversion
  • PayPal\Api\CustomAmount
  • PayPal\Api\DetailedRefund
  • PayPal\Api\Details
  • PayPal\Api\Error
  • PayPal\Api\ErrorDetails
  • PayPal\Api\ExtendedBankAccount
  • PayPal\Api\ExternalFunding
  • PayPal\Api\FileAttachment
  • PayPal\Api\FlowConfig
  • PayPal\Api\FmfDetails
  • PayPal\Api\FundingDetail
  • PayPal\Api\FundingInstrument
  • PayPal\Api\FundingOption
  • PayPal\Api\FundingSource
  • PayPal\Api\FuturePayment
  • PayPal\Api\HyperSchema
  • PayPal\Api\Image
  • PayPal\Api\Incentive
  • PayPal\Api\InputFields
  • PayPal\Api\InstallmentInfo
  • PayPal\Api\InstallmentOption
  • PayPal\Api\Invoice
  • PayPal\Api\InvoiceAddress
  • PayPal\Api\InvoiceItem
  • PayPal\Api\InvoiceNumber
  • PayPal\Api\InvoiceSearchResponse
  • PayPal\Api\Item
  • PayPal\Api\ItemList
  • PayPal\Api\Links
  • PayPal\Api\Measurement
  • PayPal\Api\MerchantInfo
  • PayPal\Api\MerchantPreferences
  • PayPal\Api\Metadata
  • PayPal\Api\NameValuePair
  • PayPal\Api\Notification
  • PayPal\Api\OpenIdAddress
  • PayPal\Api\OpenIdError
  • PayPal\Api\OpenIdSession
  • PayPal\Api\OpenIdTokeninfo
  • PayPal\Api\OpenIdUserinfo
  • PayPal\Api\Order
  • PayPal\Api\OverrideChargeModel
  • PayPal\Api\Participant
  • PayPal\Api\Patch
  • PayPal\Api\PatchRequest
  • PayPal\Api\Payee
  • PayPal\Api\Payer
  • PayPal\Api\PayerInfo
  • PayPal\Api\Payment
  • PayPal\Api\PaymentCard
  • PayPal\Api\PaymentCardToken
  • PayPal\Api\PaymentDefinition
  • PayPal\Api\PaymentDetail
  • PayPal\Api\PaymentExecution
  • PayPal\Api\PaymentHistory
  • PayPal\Api\PaymentInstruction
  • PayPal\Api\PaymentOptions
  • PayPal\Api\PaymentSummary
  • PayPal\Api\PaymentTerm
  • PayPal\Api\Payout
  • PayPal\Api\PayoutBatch
  • PayPal\Api\PayoutBatchHeader
  • PayPal\Api\PayoutItem
  • PayPal\Api\PayoutItemDetails
  • PayPal\Api\PayoutSenderBatchHeader
  • PayPal\Api\Phone
  • PayPal\Api\Plan
  • PayPal\Api\PlanList
  • PayPal\Api\PotentialPayerInfo
  • PayPal\Api\Presentation
  • PayPal\Api\PrivateLabelCard
  • PayPal\Api\ProcessorResponse
  • PayPal\Api\RecipientBankingInstruction
  • PayPal\Api\RedirectUrls
  • PayPal\Api\Refund
  • PayPal\Api\RefundDetail
  • PayPal\Api\RefundRequest
  • PayPal\Api\RelatedResources
  • PayPal\Api\Sale
  • PayPal\Api\Search
  • PayPal\Api\ShippingAddress
  • PayPal\Api\ShippingCost
  • PayPal\Api\ShippingInfo
  • PayPal\Api\Tax
  • PayPal\Api\Template
  • PayPal\Api\TemplateData
  • PayPal\Api\Templates
  • PayPal\Api\TemplateSettings
  • PayPal\Api\TemplateSettingsMetadata
  • PayPal\Api\Terms
  • PayPal\Api\Transaction
  • PayPal\Api\TransactionBase
  • PayPal\Api\Transactions
  • PayPal\Api\VerifyWebhookSignature
  • PayPal\Api\VerifyWebhookSignatureResponse
  • PayPal\Api\Webhook
  • PayPal\Api\WebhookEvent
  • PayPal\Api\WebhookEventList
  • PayPal\Api\WebhookEventType
  • PayPal\Api\WebhookEventTypeList
  • PayPal\Api\WebhookList
  • PayPal\Api\WebProfile
  • PayPal\Auth\OAuthTokenCredential
  • PayPal\Cache\AuthorizationCache
  • PayPal\Common\ArrayUtil
  • PayPal\Common\PayPalModel
  • PayPal\Common\PayPalResourceModel
  • PayPal\Common\PayPalUserAgent
  • PayPal\Common\ReflectionUtil
  • PayPal\Converter\FormatConverter
  • PayPal\Core\PayPalConfigManager
  • PayPal\Core\PayPalConstants
  • PayPal\Core\PayPalCredentialManager
  • PayPal\Core\PayPalHttpConfig
  • PayPal\Core\PayPalHttpConnection
  • PayPal\Core\PayPalLoggingManager
  • PayPal\Handler\OauthHandler
  • PayPal\Handler\RestHandler
  • PayPal\Log\PayPalDefaultLogFactory
  • PayPal\Log\PayPalLogger
  • PayPal\Rest\ApiContext
  • PayPal\Security\Cipher
  • PayPal\Transport\PayPalRestCall
  • PayPal\Validation\ArgumentValidator
  • PayPal\Validation\JsonValidator
  • PayPal\Validation\NumericValidator
  • PayPal\Validation\UrlValidator

Interfaces

  • PayPal\Handler\IPayPalHandler
  • PayPal\Log\PayPalLogFactory
  • PayPal\Rest\IResource

Exceptions

  • PayPal\Exception\PayPalConfigurationException
  • PayPal\Exception\PayPalConnectionException
  • PayPal\Exception\PayPalInvalidCredentialException
  • PayPal\Exception\PayPalMissingCredentialException
  1 <?php
  2 
  3 namespace PayPal\Cache;
  4 
  5 use PayPal\Core\PayPalConfigManager;
  6 use PayPal\Validation\JsonValidator;
  7 
  8 abstract class AuthorizationCache
  9 {
 10     public static $CACHE_PATH = '/../../../var/auth.cache';
 11 
 12     /**
 13      * A pull method which would read the persisted data based on clientId.
 14      * If clientId is not provided, an array with all the tokens would be passed.
 15      *
 16      * @param array|null $config
 17      * @param string $clientId
 18      * @return mixed|null
 19      */
 20     public static function pull($config = null, $clientId = null)
 21     {
 22         // Return if not enabled
 23         if (!self::isEnabled($config)) {
 24             return null;
 25         }
 26 
 27         $tokens = null;
 28         $cachePath = self::cachePath($config);
 29         if (file_exists($cachePath)) {
 30             // Read from the file
 31             $cachedToken = file_get_contents($cachePath);
 32             if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
 33                 $tokens = json_decode($cachedToken, true);
 34                 if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
 35                     // If client Id is found, just send in that data only
 36                     return $tokens[$clientId];
 37                 } elseif ($clientId) {
 38                     // If client Id is provided, but no key in persisted data found matching it.
 39                     return null;
 40                 }
 41             }
 42         }
 43         return $tokens;
 44     }
 45 
 46     /**
 47      * Persists the data into a cache file provided in $CACHE_PATH
 48      *
 49      * @param array|null $config
 50      * @param      $clientId
 51      * @param      $accessToken
 52      * @param      $tokenCreateTime
 53      * @param      $tokenExpiresIn
 54      * @throws \Exception
 55      */
 56     public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
 57     {
 58         // Return if not enabled
 59         if (!self::isEnabled($config)) {
 60             return;
 61         }
 62 
 63         $cachePath = self::cachePath($config);
 64         if (!is_dir(dirname($cachePath))) {
 65             if (mkdir(dirname($cachePath), 0755, true) == false) {
 66                 throw new \Exception("Failed to create directory at $cachePath");
 67             }
 68         }
 69 
 70         // Reads all the existing persisted data
 71         $tokens = self::pull();
 72         $tokens = $tokens ? $tokens : array();
 73         if (is_array($tokens)) {
 74             $tokens[$clientId] = array(
 75                 'clientId' => $clientId,
 76                 'accessTokenEncrypted' => $accessToken,
 77                 'tokenCreateTime' => $tokenCreateTime,
 78                 'tokenExpiresIn' => $tokenExpiresIn
 79             );
 80         }
 81         if (!file_put_contents($cachePath, json_encode($tokens))) {
 82             throw new \Exception("Failed to write cache");
 83         };
 84     }
 85 
 86     /**
 87      * Determines from the Configuration if caching is currently enabled/disabled
 88      *
 89      * @param $config
 90      * @return bool
 91      */
 92     public static function isEnabled($config)
 93     {
 94         $value = self::getConfigValue('cache.enabled', $config);
 95         return empty($value) ? false : ((trim($value) == true || trim($value) == 'true'));
 96     }
 97     
 98     /**
 99      * Returns the cache file path
100      *
101      * @param $config
102      * @return string
103      */
104     public static function cachePath($config)
105     {
106         $cachePath = self::getConfigValue('cache.FileName', $config);
107         return empty($cachePath) ? __DIR__ . self::$CACHE_PATH : $cachePath;
108     }
109 
110     /**
111      * Returns the Value of the key if found in given config, or from PayPal Config Manager
112      * Returns null if not found
113      *
114      * @param $key
115      * @param $config
116      * @return null|string
117      */
118     private static function getConfigValue($key, $config)
119     {
120         $config = ($config && is_array($config)) ? $config : PayPalConfigManager::getInstance()->getConfigHashmap();
121         return (array_key_exists($key, $config)) ? trim($config[$key]) : null;
122     }
123 }
124 
API documentation generated by ApiGen