hitmeister / api-sdk-php

Kaufland.de Onlineshop (former real.de and Hitmeister) API SDK for PHP
https://www.real.de/api/v1/
MIT License
23 stars 28 forks source link

[tickets::find] Field id_buyer has invalid value "0" #124

Open cottton opened 2 years ago

cottton commented 2 years ago

Calling method $client->tickets()->find() without any params throws

Hitmeister\Component\Api\Exceptions\BadRequestException: Field id_buyer has invalid value "0" in /var/www/html/vendor/hitmeister/api-sdk/src/Middleware.php:94

Reason: the cast to int forces us to send a zero instead of null. See https://github.com/hitmeister/api-sdk-php/blob/1.50.0/src/Namespaces/TicketsNamespace.php#L51

Tested via https://www.kaufland.de/api/v1/?page=endpoints#!/tickets/getSellerTickets with no id (field empty): works with "0" id: "message": "Field id_buyer has invalid value \"0\""

Was there a reason for the cast to int?


Temp fix (tested):

// Create client.
$client = ClientBuilder::create()
    ->setClientKey('YOUR_CLIENT_KEY')
    ->setClientSecret('YOUR_CLIENT_SECRET')
    ->setUserAgent('YOUR_USER_AGENT')
    ->build();

// Instead of using
// $client->tickets()->find()
// we use the
// $client->tickets()->buildFind()

// Default values.
$status = null;
$openReason = null;
$topic = null;
$buyerId = null;
$createdFrom = null;
$updatedFrom = null;
$sort = 'ts_created:desc';
$limit = 30;
$offset = 0;

// Use of ::buildFind() to be able to use $buyerId with NULL.
$r = $client->tickets()->buildFind()
    ->addParam('status', $status)
    ->addParam('open_reason', $openReason)
    ->addParam('topic', $topic)
    ->addParam('id_buyer', $buyerId)// Removed (int) casting.
    ->addDateTimeParam('ts_created:from', $createdFrom)
    ->addDateTimeParam('ts_updated:from', $updatedFrom)
    ->setSort($sort)
    ->setLimit($limit)
    ->setOffset($offset)
    ->find();

// Output.
foreach ($r as $item) {
    echo var_export($item->toArray(), true) . PHP_EOL;
}