From 7cecb8509fa3f4b30879c97d1b484960ea539f2b Mon Sep 17 00:00:00 2001 From: Vladimir Sobolev Date: Fri, 31 Mar 2023 16:15:04 +0300 Subject: [PATCH] Update ApiClient.php New method added: showOrder() Documentation: https://docs.api.utrust.com/#tag/Store-Order/operation/showOrderDetails --- src/ApiClient.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/ApiClient.php b/src/ApiClient.php index 3d77ea7..eedc37e 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -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. * @@ -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; + } }