/** * * @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); } }
/** * * @param array $handlers * Array of handlers * @param string $path * Resource path relative to base service endpoint * @param string $method * HTTP method - one of GET, POST, PUT, DELETE, PATCH etc * @param string $data * Request payload * @param array $headers * HTTP headers * @return mixed * @throws \BitPagos\Exception\BitPagosConnectionException */ public function execute($handlers = array(), $path, $method, $data = '', $headers = array()) { $config = $this->apiContext->getConfig(); $httpConfig = new HttpConfig(null, $method, $config); $headers = $headers ? $headers : array(); $httpConfig->setHeaders($headers + array('Content-Type' => 'application/json')); /** * * @var \BitPagos\Handler\IBitPagosHandler $handler */ foreach ($handlers as $handler) { if (!is_object($handler)) { $fullHandler = "\\" . (string) $handler; $handler = new $fullHandler($this->apiContext); } $handler->handle($httpConfig, $data, array('path' => $path, 'apiContext' => $this->apiContext)); } $this->httpConnection = new HttpConnection($httpConfig, $config); $response = $this->httpConnection->execute($data); return $response; }