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\Core;
  4 
  5 use PayPal\Auth\OAuthTokenCredential;
  6 use PayPal\Exception\PayPalInvalidCredentialException;
  7 
  8 /**
  9  * Class PayPalCredentialManager
 10  *
 11  * PayPalCredentialManager holds all the credential information in one place.
 12  *
 13  * @package PayPal\Core
 14  */
 15 class PayPalCredentialManager
 16 {
 17     /**
 18      * Singleton Object
 19      *
 20      * @var PayPalCredentialManager
 21      */
 22     private static $instance;
 23 
 24     /**
 25      * Hashmap to contain credentials for accounts.
 26      *
 27      * @var array
 28      */
 29     private $credentialHashmap = array();
 30 
 31     /**
 32      * Contains the API username of the default account to use
 33      * when authenticating API calls
 34      *
 35      * @var string
 36      */
 37     private $defaultAccountName;
 38 
 39     /**
 40      * Constructor initialize credential for multiple accounts specified in property file
 41      *
 42      * @param $config
 43      * @throws \Exception
 44      */
 45     private function __construct($config)
 46     {
 47         try {
 48             $this->initCredential($config);
 49         } catch (\Exception $e) {
 50             $this->credentialHashmap = array();
 51             throw $e;
 52         }
 53     }
 54 
 55     /**
 56      * Create singleton instance for this class.
 57      *
 58      * @param array|null $config
 59      * @return PayPalCredentialManager
 60      */
 61     public static function getInstance($config = null)
 62     {
 63         if (!self::$instance) {
 64             self::$instance = new self($config == null ? PayPalConfigManager::getInstance()->getConfigHashmap() : $config);
 65         }
 66         return self::$instance;
 67     }
 68 
 69     /**
 70      * Load credentials for multiple accounts, with priority given to Signature credential.
 71      *
 72      * @param array $config
 73      */
 74     private function initCredential($config)
 75     {
 76         $suffix = 1;
 77         $prefix = "acct";
 78 
 79         $arr = array();
 80         foreach ($config as $k => $v) {
 81             if (strstr($k, $prefix)) {
 82                 $arr[$k] = $v;
 83             }
 84         }
 85         $credArr = $arr;
 86 
 87         $arr = array();
 88         foreach ($config as $key => $value) {
 89             $pos = strpos($key, '.');
 90             if (strstr($key, "acct")) {
 91                 $arr[] = substr($key, 0, $pos);
 92             }
 93         }
 94         $arrayPartKeys = array_unique($arr);
 95 
 96         $key = $prefix . $suffix;
 97         $userName = null;
 98         while (in_array($key, $arrayPartKeys)) {
 99             if (isset($credArr[$key . ".ClientId"]) && isset($credArr[$key . ".ClientSecret"])) {
100                 $userName = $key;
101                 $this->credentialHashmap[$userName] = new OAuthTokenCredential(
102                     $credArr[$key . ".ClientId"],
103                     $credArr[$key . ".ClientSecret"]
104                 );
105             }
106             if ($userName && $this->defaultAccountName == null) {
107                 if (array_key_exists($key . '.UserName', $credArr)) {
108                     $this->defaultAccountName = $credArr[$key . '.UserName'];
109                 } else {
110                     $this->defaultAccountName = $key;
111                 }
112             }
113             $suffix++;
114             $key = $prefix . $suffix;
115         }
116     }
117 
118     /**
119      * Sets credential object for users
120      *
121      * @param \PayPal\Auth\OAuthTokenCredential $credential
122      * @param string|null   $userId  User Id associated with the account
123      * @param bool $default If set, it would make it as a default credential for all requests
124      *
125      * @return $this
126      */
127     public function setCredentialObject(OAuthTokenCredential $credential, $userId = null, $default = true)
128     {
129         $key = $userId == null ? 'default' : $userId;
130         $this->credentialHashmap[$key] = $credential;
131         if ($default) {
132             $this->defaultAccountName = $key;
133         }
134         return $this;
135     }
136 
137     /**
138      * Obtain Credential Object based on UserId provided.
139      *
140      * @param null $userId
141      * @return OAuthTokenCredential
142      * @throws PayPalInvalidCredentialException
143      */
144     public function getCredentialObject($userId = null)
145     {
146         if ($userId == null && array_key_exists($this->defaultAccountName, $this->credentialHashmap)) {
147             $credObj = $this->credentialHashmap[$this->defaultAccountName];
148         } elseif (array_key_exists($userId, $this->credentialHashmap)) {
149             $credObj = $this->credentialHashmap[$userId];
150         }
151 
152         if (empty($credObj)) {
153             throw new PayPalInvalidCredentialException("Credential not found for " .  ($userId ? $userId : " default user") .
154             ". Please make sure your configuration/APIContext has credential information");
155         }
156         return $credObj;
157     }
158 
159     /**
160      * Disabling __clone call
161      */
162     public function __clone()
163     {
164         trigger_error('Clone is not allowed.', E_USER_ERROR);
165     }
166 }
167 
API documentation generated by ApiGen