Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Report date validation #70

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should unit tests include examples of all three supported formats as noted by the validation exception?

$this->client->requestPaymentReport($reportRequest);
}

Expand Down