/** * Get Credential * * @return \BitPagos\Auth\OAuthTokenCredential */ public function getCredential() { if ($this->credential == null) { return CredentialManager::getInstance()->getCredentialObject(); } return $this->credential; }
/** * * @param HttpConfig $httpConfig * @param string $request * @param mixed $options * @return mixed|void * @throws BitPagosConfigurationException * @throws BitPagosInvalidCredentialException * @throws BitPagosMissingCredentialException */ public function handle($httpConfig, $request, $options) { $credential = $this->apiContext->getCredential(); $config = $this->apiContext->getConfig(); if (is_null($credential) and !empty($this->apiContext->getApiKey())) { $credential = false; } if (is_null($credential)) { // Try picking credentials from the config file $credMgr = CredentialManager::getInstance($config); $credValues = $credMgr->getCredentialObject(); if (!is_array($credValues)) { throw new BitPagosMissingCredentialException("Empty or invalid credentials passed"); } $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']); } if ((is_null($credential) or !$credential instanceof OAuthTokenCredential) and empty($this->apiContext->getApiKey())) { throw new BitPagosInvalidCredentialException("Invalid credentials passed"); } $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : '')); if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { $httpConfig->addHeader("User-Agent", BitPagosUserAgent::getValue(BitPagosConstants::SDK_NAME, BitPagosConstants::SDK_VERSION)); } if (!is_null($credential) && $credential instanceof OAuthTokenCredential and is_null($httpConfig->getHeader('Authorization'))) { $httpConfig->addHeader('Authorization', "OAuth " . $credential->getAccessToken($config), false); } if ($credential === false and !empty($this->apiContext->getApiKey()) and is_null($httpConfig->getHeader('Authorization'))) { $httpConfig->addHeader('Authorization', "ApiKey " . $this->apiContext->getApiKey(), false); } // Add any additional Headers that they may have provided $headers = $this->apiContext->getRequestHeaders(); foreach ($headers as $key => $value) { $httpConfig->addHeader($key, $value); } }