From c7c1b17dfe1a0f90307455d8c38af2bfff7a7f4f Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Thu, 3 Oct 2024 10:50:30 +0300 Subject: [PATCH 1/2] Added timeout and connection timeout config options. Defaults to 0 (indefinitely) for backwards compatibility. --- src/Nuvei/Api/Config.php | 44 ++++++++++++++++++++++++++++++++++++ src/Nuvei/Api/HttpClient.php | 3 +++ 2 files changed, 47 insertions(+) diff --git a/src/Nuvei/Api/Config.php b/src/Nuvei/Api/Config.php index 79f84cf..ab3c6c9 100644 --- a/src/Nuvei/Api/Config.php +++ b/src/Nuvei/Api/Config.php @@ -247,6 +247,50 @@ public function sslVerifyPeer() return true; } + /** + * @param int $timeout + * + * @return $this + */ + public function setTimeout($timeout) + { + $this->configData['timeout'] = (int)$timeout; + return $this; + } + + /** + * @return int + */ + public function getTimeout() + { + if (!isset($this->configData['timeout'])) { + return 0; + } + return (int)$this->configData['timeout']; + } + + /** + * @param int $timeout + * + * @return $this + */ + public function setConnectionTimeout($timeout) + { + $this->configData['connectionTimeout'] = (int)$timeout; + return $this; + } + + /** + * @return int + */ + public function getConnectionTimeout() + { + if (!isset($this->configData['connectionTimeout'])) { + return 0; + } + return (int)$this->configData['connectionTimeout']; + } + /** * @return boolean */ diff --git a/src/Nuvei/Api/HttpClient.php b/src/Nuvei/Api/HttpClient.php index 4bc27af..ed3eab6 100644 --- a/src/Nuvei/Api/HttpClient.php +++ b/src/Nuvei/Api/HttpClient.php @@ -48,6 +48,9 @@ public function requestJson(ServiceInterface $service, $requestUrl, $params) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); } + // set timeouts + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $config->getConnectionTimeout()); + curl_setopt($ch, CURLOPT_TIMEOUT, $config->getTimeout()); //Tell cURL that we want to send a POST request. curl_setopt($ch, CURLOPT_POST, 1); // set authorisation From f4de390703583fa1915fc8e81bc28591b9b68c30 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Thu, 3 Oct 2024 11:28:07 +0300 Subject: [PATCH 2/2] Added timeouts to the tests with some liberal (but still finite) defaults. --- tests/TestCaseHelper.php | 4 +++- tests/config.ini | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/TestCaseHelper.php b/tests/TestCaseHelper.php index 62229e3..7bd54c5 100644 --- a/tests/TestCaseHelper.php +++ b/tests/TestCaseHelper.php @@ -49,6 +49,8 @@ public static function getClient() 'merchantSiteId' => $config['merchantSiteId'], 'merchantSecretKey' => $config['merchantSecretKey'], 'hashAlgorithm' => $config['hashAlgorithm'], + 'timeout' => $config['timeout'], + 'connectionTimeout' => $config['connectionTimeout'], 'debugMode' => $config['debugMode'], ]); @@ -335,4 +337,4 @@ public static function generateMinimalWithdrawalOrder($amount = 0.01) : array $service = new Processing(self::getClient()); return $service->placeWithdrawalOrder($params); } -} \ No newline at end of file +} diff --git a/tests/config.ini b/tests/config.ini index f9284f9..7b9903e 100644 --- a/tests/config.ini +++ b/tests/config.ini @@ -4,4 +4,6 @@ merchantId = '5078248497400694938' merchantSiteId = '142163' merchantSecretKey = 'F0EpuOTjZPIKw5SGcNGyISClL1zaVnArABS65EkfUIwVmzgNbEiiQeesGp4N79Rg' hashAlgorithm = 'sha256' -debugMode = false \ No newline at end of file +timeout = 15 +connectionTimeout = 5 +debugMode = false