PayPalCorePayPalCredentialManager

Class PayPalCredentialManager.

Defined (1)

The class is defined in the following location(s).

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