Skip to content

Update ApiClient.php #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,60 @@ public function __destruct()
}
}

/**
* Executes a GET cURL request to the Utrust API.
*
* @param string $method The API method to call.
*
* @return array Result with the api response.
*/
private function get($endpoint, array $body = [])
{
// Check the cURL handle has not already been initiated
if ($this->curlHandle === null) {
// Initiate cURL
$this->curlHandle = curl_init();

// Set options
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curlHandle, CURLOPT_MAXREDIRS, 10);
curl_setopt($this->curlHandle, CURLOPT_TIMEOUT, 30);
curl_setopt($this->curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->curlHandle, CURLOPT_POST, 0);
}

// Set headers
$headers = array();
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers);

// Set URL
curl_setopt($this->curlHandle, CURLOPT_URL, $this->apiUrl . $endpoint);

// Execute cURL
$response = curl_exec($this->curlHandle);

// Check the response of the cURL session
if ($response !== false) {
$result = false;

// Prepare JSON result to object stdClass
$decoded = json_decode($response);

// Check the json decoding and set an error in the result if it failed
if (!empty($decoded)) {
$result = $decoded;
} else {
$result = ['error' => 'Unable to parse JSON result (' . json_last_error() . ')'];
}
} else {
// Returns the error if the response of the cURL session is false
$result = ['errors' => 'cURL error: ' . curl_error($this->curlHandle)];
}

return $result;
}

/**
* Executes a POST cURL request to the Utrust API.
*
Expand Down Expand Up @@ -115,4 +169,23 @@ public function createOrder($orderData, $customerData)

return $response->data;
}

/**
* Show Order Details.
*
* @param string $orderId The Order Id.
*
* @return string|object Response data.
* @throws Exception
*/
public function showOrder($orderId)
{
$response = $this->get('stores/orders/'.$orderId.'/?include=payments');

if (isset($response->errors)) {
throw new \Exception('Exception: Request Error! ' . print_r($response->errors, true));
}

return $response->data;
}
}