3 use PayPal\Core\PPCredentialManager;
4 use PayPal\Auth\PPSignatureCredential;
5 use PayPal\Auth\PPCertificateCredential;
6 use PayPal\Auth\PPSubjectAuthorization;
7 use PayPal\Exception\PPInvalidCredentialException;
8 use PayPal\Exception\PPMissingCredentialException;
10 class PPCredentialManager
12 private static $instance;
13 //hashmap to contain credentials for accounts.
14 private $credentialHashmap = array();
16 * Contains the API username of the default account to use
17 * when authenticating API calls.
20 private $defaultAccountName;
23 * Constructor initialize credential for multiple accounts specified in property file.
25 private function __construct($config){
27 $this->initCredential($config);
28 } catch (Exception $e) {
29 $this->credentialHashmap = array();
35 * Create singleton instance for this class.
37 public static function getInstance($config)
40 return self::$instance = new PPCredentialManager($config);
45 * Load credentials for multiple accounts, with priority given to Signature credential.
47 private function initCredential($config){
48 // $configMgr = PPConfigManager::getInstance();
52 // $credArr = $configMgr->get($prefix);
53 // $arrayPartKeys = $configMgr->getIniPrefix();
55 if(array_key_exists($prefix, $config))
57 $credArr = $this->config[$searchKey];
61 foreach ($config as $k => $v){
62 if(strstr($k, $prefix)){
71 foreach ($config as $key => $value) {
72 $pos = strpos($key, '.');
73 if(strstr($key, "acct")){
74 $arr[] = substr($key, 0, $pos);
77 $arrayPartKeys = array_unique($arr);
79 if(count($arrayPartKeys) == 0)
80 throw new PPMissingCredentialException("No valid API accounts have been configured");
82 $key = $prefix.$suffix;
83 while (in_array($key, $arrayPartKeys)){
85 if(isset($credArr[$key.".Signature"])
86 && $credArr[$key.".Signature"] != null && $credArr[$key.".Signature"] != ""){
88 $userName = isset($credArr[$key.'.UserName']) ? $credArr[$key.'.UserName'] : "";
89 $password = isset($credArr[$key.'.Password']) ? $credArr[$key.'.Password'] : "";
90 $signature = isset($credArr[$key.'.Signature']) ? $credArr[$key.'.Signature'] : "";
92 $this->credentialHashmap[$userName] = new PPSignatureCredential($userName, $password, $signature);
93 if (isset($credArr[$key.'.AppId'])) {
94 $this->credentialHashmap[$userName]->setApplicationId($credArr[$key.'.AppId']);
97 } elseif (isset($credArr[$key.".CertPath"])
98 && $credArr[$key.".CertPath"] != null && $credArr[$key.".CertPath"] != ""){
100 $userName = isset($credArr[$key.'.UserName']) ? $credArr[$key.'.UserName'] : "";
101 $password = isset($credArr[$key.'.Password']) ? $credArr[$key.'.Password'] : "";
102 $certPassPhrase = isset($credArr[$key.'.CertKey']) ? $credArr[$key.'.CertKey'] : "";
103 $certPath = isset($credArr[$key.'.CertPath']) ? $credArr[$key.'.CertPath'] : "";
105 $this->credentialHashmap[$userName] = new PPCertificateCredential($userName, $password, $certPath, $certPassPhrase);
106 if (isset($credArr[$key.'.AppId'])) {
107 $this->credentialHashmap[$userName]->setApplicationId($credArr[$key.'.AppId']);
109 } elseif (isset($credArr[$key.".ClientId"]) && isset($credArr[$key.".ClientId"]) ){
111 $this->credentialHashmap[$userName] = array('clientId' => $credArr[$key.".ClientId"],
112 'clientSecret' => $credArr[$key.".ClientSecret"]);
114 if($userName && isset($credArr[$key . ".Subject"]) && trim($credArr[$key . ".Subject"]) != "" ) {
115 $this->credentialHashmap[$userName]->setThirdPartyAuthorization(
116 new PPSubjectAuthorization($credArr[$key . ".Subject"]));
119 if ($userName && $this->defaultAccountName == null) {
120 if(array_key_exists($key. '.UserName', $credArr)) {
121 $this->defaultAccountName = $credArr[$key . '.UserName'];
123 $this->defaultAccountName = $key;
127 $key = $prefix.$suffix;
133 * Obtain Credential Object based on UserId provided.
135 public function getCredentialObject($userId = null){
137 if($userId == null) {
138 $credObj = $this->credentialHashmap[$this->defaultAccountName];
139 } else if (array_key_exists($userId, $this->credentialHashmap)) {
140 $credObj = $this->credentialHashmap[$userId];
143 if (empty($credObj)) {
144 throw new PPInvalidCredentialException("Invalid userId $userId");
150 public function __clone()
152 trigger_error('Clone is not allowed.', E_USER_ERROR);