Ejemplo n.º 1
0
 /**
  * @param array $credentials
  * @return Response
  */
 public function authenticate(array $credentials)
 {
     $request = new Request();
     // Set URL
     $request->setUrl(Gateway::getOauth2TokenUrl());
     // Prepare data
     $args = ['grant_type' => 'client_credentials', 'scope' => $credentials['scope']];
     $data = http_build_query($args);
     // Set-up headers
     $headers = ['Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded'];
     $request->setHeaders($headers);
     // Set-up opts
     $opts = [CURLOPT_SSL_VERIFYPEER => FALSE, CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_USERPWD => $this->client->getClientId() . ':' . $this->client->getClientSecret(), CURLOPT_POSTFIELDS => $data];
     $request->setOpts($opts);
     // Make request
     $response = $this->http->doRequest($request);
     if (!$response || !$response->getData()) {
         // cURL errors
         throw new AuthorizationException('Authorization failed', $response->getCode());
     } else {
         if (isset($response->getData()->errors)) {
             // GoPay errors
             $error = $response->getData()->errors[0];
             throw new AuthorizationException(AuthorizationException::format($error), $error->error_code);
         }
     }
     return $response;
 }
Ejemplo n.º 2
0
 /**
  * Build request and execute him
  *
  * @param string $method
  * @param string $uri
  * @param array $data
  * @param string $contentType
  * @return Response
  */
 protected function makeRequest($method, $uri, array $data = NULL, $contentType = Http::CONTENT_JSON)
 {
     // Invoke events
     $this->trigger('onRequest', [$method, $uri, $data]);
     // Verify that client is authenticated
     if (!$this->client->hasToken()) {
         // Do authorization
         $this->doAuthorization();
     }
     $request = new Request();
     // Set-up URL
     $request->setUrl(Gateway::getFullApiUrl($uri));
     // Set-up headers
     $headers = ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->client->getToken()->accessToken, 'Content-Type' => $contentType];
     $request->setHeaders($headers);
     // Set-up opts
     $request->setOpts($this->options);
     // Set-up method
     switch ($method) {
         // GET =========================================
         case HttpClient::METHOD_GET:
             $request->appendOpts([CURLOPT_HTTPGET => TRUE]);
             break;
             // POST ========================================
         // POST ========================================
         case HttpClient::METHOD_POST:
             $request->appendOpts([CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $contentType === Http::CONTENT_FORM ? http_build_query($data) : json_encode($data)]);
             break;
         default:
             throw new InvalidStateException('Unsupported http method');
     }
     return $this->client->call($request);
 }
Ejemplo n.º 3
0
 /**
  * @param string $mode
  */
 public function setMode($mode)
 {
     if ($mode === self::PROD) {
         Gateway::init(Gateway::PROD);
         $this->mode = self::PROD;
     } else {
         Gateway::init(Gateway::TEST);
         $this->mode = self::TEST;
     }
 }