Skip to content

Commit

Permalink
Merge pull request #70 from paytrail/fixReportValidation
Browse files Browse the repository at this point in the history
Fix Report date validation
  • Loading branch information
loueranta-paytrail authored Mar 15, 2023
2 parents 721e156 + e47789c commit f9c3f04
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
13 changes: 5 additions & 8 deletions src/Request/ReportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 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');
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Util/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Paytrail\SDK\Util;

use Paytrail\SDK\Exception\ClientException;
use Paytrail\SDK\Response\CurlResponse;

class CurlClient
Expand Down Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit f9c3f04

Please # to comment.