fiskalyClient = FiskalyClient::createUsingCredentials( $smaEndpoint, $apiKey, $apiSecret, $endpoint ); } catch (Exception $e) { if(strpos($e->getMessage(), '404') === 0) { throw new SmaEndpointNotFoundException($e->getMessage()); } if($e->getMessage() === 'Undefined variable: http_response_header') { throw new SmaEndpointNotReachableException($e->getMessage()); } throw $e; } $this->apiKey = $apiKey; $this->apiSecret = $apiSecret; } /** * @param string $apiKey * @param string $apiSecret * * @throws ClientErrorException * * @return string */ protected function generateAccessToken(string $apiKey, string $apiSecret): string { $result = $this->callApiPost( 'auth', json_encode( [ 'api_key' => $apiKey, 'api_secret' => $apiSecret, ] ), false ); return $result->access_token; } /** * @param $endpoint * @param null $body * @param null $query * * @throws FiskalyClientException * @throws FiskalyHttpException * @throws FiskalyHttpTimeoutException * @return mixed */ protected function callApiGet($endpoint, $body = null, $query = null) { return $this->callApi('GET', $endpoint, $body, $query); } /** * @param $endpoint * @param null $body * @param null $query * * @throws FiskalyClientException * @throws FiskalyHttpException * @throws FiskalyHttpTimeoutException * @return mixed */ protected function callApiPost($endpoint, $body = null, $query = null) { return $this->callApi('POST', $endpoint, $body, $query); } /** * @param $endpoint * @param null $body * @param null $query * * @throws FiskalyClientException * @throws FiskalyHttpException * @throws FiskalyHttpTimeoutException * @return mixed */ protected function callApiPut($endpoint, $body = null, $query = null) { return $this->callApi('PUT', $endpoint, $body, $query); } /** * @param $method * @param $endpoint * @param null $body * @param null $query * * @throws FiskalyClientException * @throws FiskalyHttpTimeoutException * @return mixed */ private function callApi($method, $endpoint, $body = null, $query = null) { if (!empty($body)) { $body = base64_encode($body); } try { $response = $this->fiskalyClient->request( $method, $endpoint, $query, null, $body ); return json_decode(base64_decode($response->getResponse()['body'])); } catch (ClientErrorException | FiskalyHttpException $e) { $this->handleClientException($e); } } /** * @throws FiskalyClientException * @throws FiskalyHttpException * @throws FiskalyHttpTimeoutException * @return SelfTestResponse */ public function selfTest(): SelfTestResponse { return $this->fiskalyClient->selfTest(); } /** * @param Exception $e * * @throws Exception * @return void */ private function handleClientException(Exception $e): void { if ($e->getStatus() === 401 || $e->getCode() == 401) { throw new InvalidCredentialsException('Falsche Zugangsdaten'); } if ($e->getStatus() === 403 || $e->getCode() == 403) { throw new InvalidCredentialsException('Nutzer nicht berechtigt'); } throw $e; } }