Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "square/square",
"version": "43.1.1.20250924",
"version": "43.2.0.20251016",
"description": "Use Square APIs to manage and run business including payment, customer, product, inventory, and employee management.",
"keywords": [
"square",
Expand Down
272 changes: 272 additions & 0 deletions src/Channels/ChannelsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?php

namespace Square\Channels;

use GuzzleHttp\ClientInterface;
use Square\Core\Client\RawClient;
use Square\Channels\Requests\ListChannelsRequest;
use Square\Core\Pagination\Pager;
use Square\Types\Channel;
use Square\Core\Pagination\CursorPager;
use Square\Types\ListChannelsResponse;
use Square\Channels\Requests\BulkRetrieveChannelsRequest;
use Square\Types\BulkRetrieveChannelsResponse;
use Square\Exceptions\SquareException;
use Square\Exceptions\SquareApiException;
use Square\Core\Json\JsonApiRequest;
use Square\Environments;
use Square\Core\Client\HttpMethod;
use JsonException;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Client\ClientExceptionInterface;
use Square\Channels\Requests\GetChannelsRequest;
use Square\Types\RetrieveChannelResponse;

class ChannelsClient
{
/**
* @var array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options
*/
private array $options;

/**
* @var RawClient $client
*/
private RawClient $client;

/**
* @param RawClient $client
* @param ?array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options
*/
public function __construct(
RawClient $client,
?array $options = null,
) {
$this->client = $client;
$this->options = $options ?? [];
}

/**
*
*
* @param ListChannelsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return Pager<Channel>
*/
public function list(ListChannelsRequest $request = new ListChannelsRequest(), ?array $options = null): Pager
{
return new CursorPager(
request: $request,
getNextPage: fn (ListChannelsRequest $request) => $this->_list($request, $options),
setCursor: function (ListChannelsRequest $request, ?string $cursor) {
$request->setCursor($cursor);
},
/* @phpstan-ignore-next-line */
getNextCursor: fn (ListChannelsResponse $response) => $response?->getCursor() ?? null,
/* @phpstan-ignore-next-line */
getItems: fn (ListChannelsResponse $response) => $response?->getChannels() ?? [],
);
}

/**
*
*
* @param BulkRetrieveChannelsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return BulkRetrieveChannelsResponse
* @throws SquareException
* @throws SquareApiException
*/
public function bulkRetrieve(BulkRetrieveChannelsRequest $request, ?array $options = null): BulkRetrieveChannelsResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/channels/bulk-retrieve",
method: HttpMethod::POST,
body: $request,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return BulkRetrieveChannelsResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (RequestException $e) {
$response = $e->getResponse();
if ($response === null) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: "API request failed",
statusCode: $response->getStatusCode(),
body: $response->getBody()->getContents(),
);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}

/**
*
*
* @param GetChannelsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return RetrieveChannelResponse
* @throws SquareException
* @throws SquareApiException
*/
public function get(GetChannelsRequest $request, ?array $options = null): RetrieveChannelResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/channels/{$request->getChannelId()}",
method: HttpMethod::GET,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return RetrieveChannelResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (RequestException $e) {
$response = $e->getResponse();
if ($response === null) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: "API request failed",
statusCode: $response->getStatusCode(),
body: $response->getBody()->getContents(),
);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}

/**
*
*
* @param ListChannelsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return ListChannelsResponse
* @throws SquareException
* @throws SquareApiException
*/
private function _list(ListChannelsRequest $request = new ListChannelsRequest(), ?array $options = null): ListChannelsResponse
{
$options = array_merge($this->options, $options ?? []);
$query = [];
if ($request->getReferenceType() != null) {
$query['reference_type'] = $request->getReferenceType();
}
if ($request->getReferenceId() != null) {
$query['reference_id'] = $request->getReferenceId();
}
if ($request->getStatus() != null) {
$query['status'] = $request->getStatus();
}
if ($request->getCursor() != null) {
$query['cursor'] = $request->getCursor();
}
if ($request->getLimit() != null) {
$query['limit'] = $request->getLimit();
}
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/channels",
method: HttpMethod::GET,
query: $query,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return ListChannelsResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (RequestException $e) {
$response = $e->getResponse();
if ($response === null) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: "API request failed",
statusCode: $response->getStatusCode(),
body: $response->getBody()->getContents(),
);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
}
44 changes: 44 additions & 0 deletions src/Channels/Requests/BulkRetrieveChannelsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Square\Channels\Requests;

use Square\Core\Json\JsonSerializableType;
use Square\Core\Json\JsonProperty;
use Square\Core\Types\ArrayType;

class BulkRetrieveChannelsRequest extends JsonSerializableType
{
/**
* @var array<string> $channelIds
*/
#[JsonProperty('channel_ids'), ArrayType(['string'])]
private array $channelIds;

/**
* @param array{
* channelIds: array<string>,
* } $values
*/
public function __construct(
array $values,
) {
$this->channelIds = $values['channelIds'];
}

/**
* @return array<string>
*/
public function getChannelIds(): array
{
return $this->channelIds;
}

/**
* @param array<string> $value
*/
public function setChannelIds(array $value): self
{
$this->channelIds = $value;
return $this;
}
}
41 changes: 41 additions & 0 deletions src/Channels/Requests/GetChannelsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Square\Channels\Requests;

use Square\Core\Json\JsonSerializableType;

class GetChannelsRequest extends JsonSerializableType
{
/**
* @var string $channelId A channel id
*/
private string $channelId;

/**
* @param array{
* channelId: string,
* } $values
*/
public function __construct(
array $values,
) {
$this->channelId = $values['channelId'];
}

/**
* @return string
*/
public function getChannelId(): string
{
return $this->channelId;
}

/**
* @param string $value
*/
public function setChannelId(string $value): self
{
$this->channelId = $value;
return $this;
}
}
Loading
Loading