diff --git a/src/Transaction/Client.php b/src/Transaction/Client.php index b3116fac..a1c4d878 100644 --- a/src/Transaction/Client.php +++ b/src/Transaction/Client.php @@ -31,7 +31,7 @@ use Buckaroo\Services\TransactionHeaders\DefaultHeader; use Buckaroo\Services\TransactionHeaders\HmacHeader; use Buckaroo\Services\TransactionHeaders\SoftwareHeader; -use Buckaroo\Transaction\Request\HttpClient\HttpClientGuzzle; +use Buckaroo\Transaction\Request\HttpClient\HttpClientFactory; use Buckaroo\Transaction\Request\HttpClient\HttpClientInterface; use Buckaroo\Transaction\Request\Request; use Buckaroo\Transaction\Response\Response; @@ -43,7 +43,7 @@ class Client private const METHOD_POST = 'POST'; /** - * @var HttpClientInterface|HttpClientGuzzle + * @var HttpClientInterface */ protected HttpClientInterface $httpClient; /** @@ -61,7 +61,7 @@ class Client public function __construct(?Config $config) { $this->config = $config; - $this->httpClient = new HttpClientGuzzle($config->getLogger()); + $this->httpClient = HttpClientFactory::createClient($config->getLogger()); } /** diff --git a/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php new file mode 100644 index 00000000..acb11dc5 --- /dev/null +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php @@ -0,0 +1,70 @@ +logger = $logger; + + $this->httpClient = new Client([ + 'timeout' => self::TIMEOUT, + 'connect_timeout' => self::CONNECT_TIMEOUT, + ]); + } + + /** + * @param string $url + * @param array $headers + * @param string $method + * @param string|null $data + * @return array|mixed + * @throws TransferException + * @throws BuckarooException|GuzzleException + */ + + public function call(string $url, array $headers, string $method, string $data = null) + { + $headers = $this->convertHeadersFormat($headers); + + $request = $this->httpClient->createRequest($method, $url, [ + 'headers' => $headers, + 'body' => $data, + ]); + + try + { + $response = $this->httpClient->send($request); + + $result = (string) $response->getBody(); + + $this->logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders())); + $this->logger->info('RESPONSE BODY: ' . $response->getBody()); + } catch (RequestException $e) { + throw new TransferException($this->logger, "Transfer failed", 0, $e); + } + + $result = $this->getDecodedResult($response, $result); + + return [ + $response, + $result, + ]; + } +} diff --git a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php similarity index 67% rename from src/Transaction/Request/HttpClient/HttpClientGuzzle.php rename to src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php index d15a2fd7..015b4e09 100644 --- a/src/Transaction/Request/HttpClient/HttpClientGuzzle.php +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php @@ -1,27 +1,8 @@ logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders())); $this->logger->info('RESPONSE BODY: ' . $response->getBody()); - } catch (GuzzleException $e) - { + } catch (GuzzleException $e) { throw new TransferException($this->logger, "Transfer failed", 0, $e); } diff --git a/src/Transaction/Request/HttpClient/HttpClientFactory.php b/src/Transaction/Request/HttpClient/HttpClientFactory.php new file mode 100644 index 00000000..1c7a1220 --- /dev/null +++ b/src/Transaction/Request/HttpClient/HttpClientFactory.php @@ -0,0 +1,23 @@ +