From 47ea6afd7f40e0babd4f2fe0b4e8b90e8993ddf6 Mon Sep 17 00:00:00 2001 From: Daniel V <44167963+rdvetromilla@users.noreply.github.com> Date: Fri, 9 Apr 2021 12:03:34 -0300 Subject: [PATCH] Fix: Add support for DELETE methods (#3) * Fix: Add support for DELETE methods * Bail early since we're not returning a JSON string --- src/Getnet/API/Request.php | 67 +++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/src/Getnet/API/Request.php b/src/Getnet/API/Request.php index b06c5ea..b7e47d8 100644 --- a/src/Getnet/API/Request.php +++ b/src/Getnet/API/Request.php @@ -16,11 +16,12 @@ class Request { * @var string */ private $baseUrl = ''; - - const CURL_TYPE_AUTH = "AUTH"; - const CURL_TYPE_POST = "POST"; - const CURL_TYPE_PUT = "PUT"; - const CURL_TYPE_GET = "GET"; + + const CURL_TYPE_AUTH = "AUTH"; + const CURL_TYPE_POST = "POST"; + const CURL_TYPE_PUT = "PUT"; + const CURL_TYPE_GET = "GET"; + const CURL_TYPE_DELETE = "DELETE"; /** * Request constructor. @@ -29,7 +30,7 @@ class Request { */ public function __construct(Getnet $credentials) { $this->baseUrl = $credentials->getEnvironment()->getApiUrl(); - + if (!$credentials->getAuthorizationToken()) { $this->auth($credentials); } @@ -42,11 +43,11 @@ public function __construct(Getnet $credentials) { * @throws Exception */ public function auth(Getnet $credentials) { - + if ($this->verifyAuthSession($credentials)) { return $credentials; } - + $url_path = "/auth/oauth/v2/token"; $params = [ @@ -63,42 +64,42 @@ public function auth(Getnet $credentials) { } $credentials->setAuthorizationToken($response["access_token"]); - + //Save auth session if ($credentials->getKeySession()) { $response['generated'] = microtime(true); $_SESSION[$credentials->getKeySession()] = $response; } - + return $credentials; } - + /** * start session for use - * + * * @param Getnet $credentials * @return boolean */ private function verifyAuthSession(Getnet $credentials){ - + if ($credentials->getKeySession() && isset($_SESSION[$credentials->getKeySession()]) && $_SESSION[$credentials->getKeySession()]["access_token"]) { - + $auth = $_SESSION[$credentials->getKeySession()]; $now = microtime(true); $init = $auth["generated"]; - + if (($now - $init) < $auth["expires_in"]) { $credentials->setAuthorizationToken($auth["access_token"]); - + return true; } } - + return false; } - + /** - * + * * @param Getnet $credentials * @param mixed $url_path * @param mixed $method @@ -127,11 +128,14 @@ private function send(Getnet $credentials, $url_path, $method, $json = NULL) { curl_setopt($curl, CURLOPT_POSTFIELDS, $json); } elseif ($method == self::CURL_TYPE_GET) { $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); + } elseif ($method == self::CURL_TYPE_DELETE) { + $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::CURL_TYPE_DELETE); } elseif ($method == self::CURL_TYPE_PUT) { $defaultCurlOptions[CURLOPT_HTTPHEADER][] = 'Authorization: Bearer ' . $credentials->getAuthorizationToken(); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::CURL_TYPE_PUT); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); - + } elseif ($method == self::CURL_TYPE_AUTH) { $defaultCurlOptions[CURLOPT_HTTPHEADER][0] = 'application/x-www-form-urlencoded'; curl_setopt($curl, CURLOPT_USERPWD, $credentials->getClientId() . ":" . $credentials->getClientSecret()); @@ -154,6 +158,14 @@ private function send(Getnet $credentials, $url_path, $method, $json = NULL) { if (curl_getinfo($curl, CURLINFO_HTTP_CODE) >= 400) { throw new Exception($response, 100); } + + // Status code 204 don't have content. That means $response will be always false + // Provides a custom content for $response to avoid error in the next if logic + if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 204) { + curl_close($curl); + return ['status_code' => 204]; + } + if (! $response) { print "ERROR"; EXIT(); @@ -209,7 +221,7 @@ public function post(Getnet $credentials, $url_path, $params) { } /** - * + * * @param Getnet $credentials * @param mixed $url_path * @param mixed $params @@ -219,5 +231,16 @@ public function post(Getnet $credentials, $url_path, $params) { public function put(Getnet $credentials, $url_path, $params) { return $this->send($credentials, $url_path, self::CURL_TYPE_PUT, $params); } - + + /** + * + * @param Getnet $credentials + * @param mixed $url_path + * @return mixed + * * @throws Exception + */ + public function delete(Getnet $credentials, $url_path) { + return $this->send($credentials, $url_path, self::CURL_TYPE_DELETE); + } + }