Skip to content

Commit

Permalink
Fix: Add support for DELETE methods (#3)
Browse files Browse the repository at this point in the history
* Fix: Add support for DELETE methods

* Bail early since we're not returning a JSON string
  • Loading branch information
rdvetromilla authored Apr 9, 2021
1 parent 17b9e0b commit 47ea6af
Showing 1 changed file with 45 additions and 22 deletions.
67 changes: 45 additions & 22 deletions src/Getnet/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,7 +30,7 @@ class Request {
*/
public function __construct(Getnet $credentials) {
$this->baseUrl = $credentials->getEnvironment()->getApiUrl();

if (!$credentials->getAuthorizationToken()) {
$this->auth($credentials);
}
Expand All @@ -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 = [
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand Down Expand Up @@ -209,7 +221,7 @@ public function post(Getnet $credentials, $url_path, $params) {
}

/**
*
*
* @param Getnet $credentials
* @param mixed $url_path
* @param mixed $params
Expand All @@ -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);
}

}

0 comments on commit 47ea6af

Please sign in to comment.