From 7fee94eef574709ead1731df983a8cce901a37c5 Mon Sep 17 00:00:00 2001 From: juhopekkavuorela Date: Thu, 9 Mar 2023 13:33:40 +0200 Subject: [PATCH 1/2] Fix Report date validation, throw exception in status 400 when not using guzzle --- src/Request/ReportRequest.php | 13 +++++-------- src/Util/CurlClient.php | 10 ++++++---- tests/ClientTest.php | 4 ++-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Request/ReportRequest.php b/src/Request/ReportRequest.php index d7c5853..0b46428 100644 --- a/src/Request/ReportRequest.php +++ b/src/Request/ReportRequest.php @@ -60,22 +60,19 @@ public function validate() } if (!empty($props['startDate'])) { - if (!(new \DateTime())->createFromFormat('Y-m-d', $props['startDate'])) { - throw new ValidationException('startDate must be in Y-m-d format'); + if (!preg_match('/^\d{4}(-\d{2}){2}T\d{2}(:\d{2}){2}(\.\d+)?\+\d{2}:\d{2}/', $props['startDate'])) { + throw new ValidationException('startDate must be in DateTimeInterface::ATOM, ISO8601 or RFC3339 format'); } } if (!empty($props['endDate'])) { - if (!(new \DateTime())->createFromFormat('Y-m-d', $props['endDate'])) { - throw new ValidationException('endDate must be in Y-m-d format'); + if (!preg_match('/^\d{4}(-\d{2}){2}T\d{2}(:\d{2}){2}(\.\d+)?\+\d{2}:\d{2}/', $props['endDate'])) { + throw new ValidationException('endDate must be in DateTimeInterface::ATOM, ISO8601 or RFC3339 format'); } } if (!empty($props['startDate']) && !empty($props['endDate'])) { - if ( - (new \DateTime())->createFromFormat('Y-m-d', $props['startDate']) - > (new \DateTime())->createFromFormat('Y-m-d', $props['endDate']) - ) { + if (substr($props['startDate'], 0, 10) > substr($props['endDate'], 0, 10)) { throw new ValidationException('startDate cannot be lower than endDate'); } } diff --git a/src/Util/CurlClient.php b/src/Util/CurlClient.php index 83abef5..3faadf4 100644 --- a/src/Util/CurlClient.php +++ b/src/Util/CurlClient.php @@ -2,6 +2,7 @@ namespace Paytrail\SDK\Util; +use Paytrail\SDK\Exception\ClientException; use Paytrail\SDK\Response\CurlResponse; class CurlClient @@ -47,12 +48,13 @@ public function request(string $method, string $uri, array $options) $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $headers = rtrim(substr($response, 0, $header_size)); $body = substr($response, $header_size); - - $curlResponse = new CurlResponse($headers, $body, $statusCode); - curl_close($curl); - return $curlResponse; + if ($statusCode == 400) { + throw new ClientException($body, $statusCode); + } + + return new CurlResponse($headers, $body, $statusCode); } /** diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 9fc5b64..51a1507 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -570,8 +570,8 @@ public function testRequestPaymentReportThrowsExceptionWhenEndDateIsLowerThanSta $reportRequest = (new ReportRequest()) ->setRequestType('json') ->setCallbackUrl('https://nourl.test') - ->setStartDate('2023-01-20') - ->setEndDate('2023-01-01'); + ->setStartDate('2023-01-20T12:00:00+02:00') + ->setEndDate('2023-01-01T23:59:50+02:00'); $this->client->requestPaymentReport($reportRequest); } From e47789c4a8053e2bf26469b3fb3e0aabeb3f0d73 Mon Sep 17 00:00:00 2001 From: juhopekkavuorela Date: Thu, 9 Mar 2023 13:37:23 +0200 Subject: [PATCH 2/2] Shorten error message on report date valiadtion --- src/Request/ReportRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Request/ReportRequest.php b/src/Request/ReportRequest.php index 0b46428..7c120bb 100644 --- a/src/Request/ReportRequest.php +++ b/src/Request/ReportRequest.php @@ -61,7 +61,7 @@ public function validate() if (!empty($props['startDate'])) { if (!preg_match('/^\d{4}(-\d{2}){2}T\d{2}(:\d{2}){2}(\.\d+)?\+\d{2}:\d{2}/', $props['startDate'])) { - throw new ValidationException('startDate must be in DateTimeInterface::ATOM, ISO8601 or RFC3339 format'); + throw new ValidationException('startDate must be in ATOM, ISO8601 or RFC3339 format'); } }