| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Customers; |
| 4 |
|
| 5 |
use Square\Customers\CustomAttributeDefinitions\CustomAttributeDefinitionsClient; |
| 6 |
use Square\Customers\Groups\GroupsClient; |
| 7 |
use Square\Customers\Segments\SegmentsClient; |
| 8 |
use Square\Customers\Cards\CardsClient; |
| 9 |
use Square\Customers\CustomAttributes\CustomAttributesClient; |
| 10 |
use GuzzleHttp\ClientInterface; |
| 11 |
use Square\Core\Client\RawClient; |
| 12 |
use Square\Customers\Requests\ListCustomersRequest; |
| 13 |
use Square\Core\Pagination\Pager; |
| 14 |
use Square\Types\Customer; |
| 15 |
use Square\Core\Pagination\CursorPager; |
| 16 |
use Square\Types\ListCustomersResponse; |
| 17 |
use Square\Customers\Requests\CreateCustomerRequest; |
| 18 |
use Square\Types\CreateCustomerResponse; |
| 19 |
use Square\Exceptions\SquareException; |
| 20 |
use Square\Exceptions\SquareApiException; |
| 21 |
use Square\Core\Json\JsonApiRequest; |
| 22 |
use Square\Environments; |
| 23 |
use Square\Core\Client\HttpMethod; |
| 24 |
use JsonException; |
| 25 |
use GuzzleHttp\Exception\RequestException; |
| 26 |
use Psr\Http\Client\ClientExceptionInterface; |
| 27 |
use Square\Customers\Requests\BulkCreateCustomersRequest; |
| 28 |
use Square\Types\BulkCreateCustomersResponse; |
| 29 |
use Square\Customers\Requests\BulkDeleteCustomersRequest; |
| 30 |
use Square\Types\BulkDeleteCustomersResponse; |
| 31 |
use Square\Customers\Requests\BulkRetrieveCustomersRequest; |
| 32 |
use Square\Types\BulkRetrieveCustomersResponse; |
| 33 |
use Square\Customers\Requests\BulkUpdateCustomersRequest; |
| 34 |
use Square\Types\BulkUpdateCustomersResponse; |
| 35 |
use Square\Customers\Requests\SearchCustomersRequest; |
| 36 |
use Square\Types\SearchCustomersResponse; |
| 37 |
use Square\Customers\Requests\GetCustomersRequest; |
| 38 |
use Square\Types\GetCustomerResponse; |
| 39 |
use Square\Customers\Requests\UpdateCustomerRequest; |
| 40 |
use Square\Types\UpdateCustomerResponse; |
| 41 |
use Square\Customers\Requests\DeleteCustomersRequest; |
| 42 |
use Square\Types\DeleteCustomerResponse; |
| 43 |
|
| 44 |
class CustomersClient |
| 45 |
{ |
| 46 |
/** |
| 47 |
* @var CustomAttributeDefinitionsClient $customAttributeDefinitions |
| 48 |
*/ |
| 49 |
public CustomAttributeDefinitionsClient $customAttributeDefinitions; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var GroupsClient $groups |
| 53 |
*/ |
| 54 |
public GroupsClient $groups; |
| 55 |
|
| 56 |
/** |
| 57 |
* @var SegmentsClient $segments |
| 58 |
*/ |
| 59 |
public SegmentsClient $segments; |
| 60 |
|
| 61 |
/** |
| 62 |
* @var CardsClient $cards |
| 63 |
*/ |
| 64 |
public CardsClient $cards; |
| 65 |
|
| 66 |
/** |
| 67 |
* @var CustomAttributesClient $customAttributes |
| 68 |
*/ |
| 69 |
public CustomAttributesClient $customAttributes; |
| 70 |
|
| 71 |
/** |
| 72 |
* @var array{ |
| 73 |
* baseUrl?: string, |
| 74 |
* client?: ClientInterface, |
| 75 |
* maxRetries?: int, |
| 76 |
* timeout?: float, |
| 77 |
* headers?: array<string, string>, |
| 78 |
* } $options |
| 79 |
*/ |
| 80 |
private array $options; |
| 81 |
|
| 82 |
/** |
| 83 |
* @var RawClient $client |
| 84 |
*/ |
| 85 |
private RawClient $client; |
| 86 |
|
| 87 |
/** |
| 88 |
* @param RawClient $client |
| 89 |
* @param ?array{ |
| 90 |
* baseUrl?: string, |
| 91 |
* client?: ClientInterface, |
| 92 |
* maxRetries?: int, |
| 93 |
* timeout?: float, |
| 94 |
* headers?: array<string, string>, |
| 95 |
* } $options |
| 96 |
*/ |
| 97 |
public function __construct( |
| 98 |
RawClient $client, |
| 99 |
?array $options = null, |
| 100 |
) { |
| 101 |
$this->client = $client; |
| 102 |
$this->options = $options ?? []; |
| 103 |
$this->customAttributeDefinitions = new CustomAttributeDefinitionsClient($this->client, $this->options); |
| 104 |
$this->groups = new GroupsClient($this->client, $this->options); |
| 105 |
$this->segments = new SegmentsClient($this->client, $this->options); |
| 106 |
$this->cards = new CardsClient($this->client, $this->options); |
| 107 |
$this->customAttributes = new CustomAttributesClient($this->client, $this->options); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Lists customer profiles associated with a Square account. |
| 112 |
* |
| 113 |
* Under normal operating conditions, newly created or updated customer profiles become available |
| 114 |
* for the listing operation in well under 30 seconds. Occasionally, propagation of the new or updated |
| 115 |
* profiles can take closer to one minute or longer, especially during network incidents and outages. |
| 116 |
* |
| 117 |
* @param ListCustomersRequest $request |
| 118 |
* @param ?array{ |
| 119 |
* baseUrl?: string, |
| 120 |
* maxRetries?: int, |
| 121 |
* timeout?: float, |
| 122 |
* headers?: array<string, string>, |
| 123 |
* queryParameters?: array<string, mixed>, |
| 124 |
* bodyProperties?: array<string, mixed>, |
| 125 |
* } $options |
| 126 |
* @return Pager<Customer> |
| 127 |
*/ |
| 128 |
public function list(ListCustomersRequest $request = new ListCustomersRequest(), ?array $options = null): Pager |
| 129 |
{ |
| 130 |
return new CursorPager( |
| 131 |
request: $request, |
| 132 |
getNextPage: fn (ListCustomersRequest $request) => $this->_list($request, $options), |
| 133 |
setCursor: function (ListCustomersRequest $request, ?string $cursor) { |
| 134 |
$request->setCursor($cursor); |
| 135 |
}, |
| 136 |
/* @phpstan-ignore-next-line */ |
| 137 |
getNextCursor: fn (ListCustomersResponse $response) => $response?->getCursor() ?? null, |
| 138 |
/* @phpstan-ignore-next-line */ |
| 139 |
getItems: fn (ListCustomersResponse $response) => $response?->getCustomers() ?? [], |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Creates a new customer for a business. |
| 145 |
* |
| 146 |
* You must provide at least one of the following values in your request to this |
| 147 |
* endpoint: |
| 148 |
* |
| 149 |
* - `given_name` |
| 150 |
* - `family_name` |
| 151 |
* - `company_name` |
| 152 |
* - `email_address` |
| 153 |
* - `phone_number` |
| 154 |
* |
| 155 |
* @param CreateCustomerRequest $request |
| 156 |
* @param ?array{ |
| 157 |
* baseUrl?: string, |
| 158 |
* maxRetries?: int, |
| 159 |
* timeout?: float, |
| 160 |
* headers?: array<string, string>, |
| 161 |
* queryParameters?: array<string, mixed>, |
| 162 |
* bodyProperties?: array<string, mixed>, |
| 163 |
* } $options |
| 164 |
* @return CreateCustomerResponse |
| 165 |
* @throws SquareException |
| 166 |
* @throws SquareApiException |
| 167 |
*/ |
| 168 |
public function create(CreateCustomerRequest $request = new CreateCustomerRequest(), ?array $options = null): CreateCustomerResponse |
| 169 |
{ |
| 170 |
$options = array_merge($this->options, $options ?? []); |
| 171 |
try { |
| 172 |
$response = $this->client->sendRequest( |
| 173 |
new JsonApiRequest( |
| 174 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 175 |
path: "v2/customers", |
| 176 |
method: HttpMethod::POST, |
| 177 |
body: $request, |
| 178 |
), |
| 179 |
$options, |
| 180 |
); |
| 181 |
$statusCode = $response->getStatusCode(); |
| 182 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 183 |
$json = $response->getBody()->getContents(); |
| 184 |
return CreateCustomerResponse::fromJson($json); |
| 185 |
} |
| 186 |
} catch (JsonException $e) { |
| 187 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 188 |
} catch (RequestException $e) { |
| 189 |
$response = $e->getResponse(); |
| 190 |
if ($response === null) { |
| 191 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 192 |
} |
| 193 |
throw new SquareApiException( |
| 194 |
message: "API request failed", |
| 195 |
statusCode: $response->getStatusCode(), |
| 196 |
body: $response->getBody()->getContents(), |
| 197 |
); |
| 198 |
} catch (ClientExceptionInterface $e) { |
| 199 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 200 |
} |
| 201 |
throw new SquareApiException( |
| 202 |
message: 'API request failed', |
| 203 |
statusCode: $statusCode, |
| 204 |
body: $response->getBody()->getContents(), |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Creates multiple [customer profiles](entity:Customer) for a business. |
| 210 |
* |
| 211 |
* This endpoint takes a map of individual create requests and returns a map of responses. |
| 212 |
* |
| 213 |
* You must provide at least one of the following values in each create request: |
| 214 |
* |
| 215 |
* - `given_name` |
| 216 |
* - `family_name` |
| 217 |
* - `company_name` |
| 218 |
* - `email_address` |
| 219 |
* - `phone_number` |
| 220 |
* |
| 221 |
* @param BulkCreateCustomersRequest $request |
| 222 |
* @param ?array{ |
| 223 |
* baseUrl?: string, |
| 224 |
* maxRetries?: int, |
| 225 |
* timeout?: float, |
| 226 |
* headers?: array<string, string>, |
| 227 |
* queryParameters?: array<string, mixed>, |
| 228 |
* bodyProperties?: array<string, mixed>, |
| 229 |
* } $options |
| 230 |
* @return BulkCreateCustomersResponse |
| 231 |
* @throws SquareException |
| 232 |
* @throws SquareApiException |
| 233 |
*/ |
| 234 |
public function batchCreate(BulkCreateCustomersRequest $request, ?array $options = null): BulkCreateCustomersResponse |
| 235 |
{ |
| 236 |
$options = array_merge($this->options, $options ?? []); |
| 237 |
try { |
| 238 |
$response = $this->client->sendRequest( |
| 239 |
new JsonApiRequest( |
| 240 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 241 |
path: "v2/customers/bulk-create", |
| 242 |
method: HttpMethod::POST, |
| 243 |
body: $request, |
| 244 |
), |
| 245 |
$options, |
| 246 |
); |
| 247 |
$statusCode = $response->getStatusCode(); |
| 248 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 249 |
$json = $response->getBody()->getContents(); |
| 250 |
return BulkCreateCustomersResponse::fromJson($json); |
| 251 |
} |
| 252 |
} catch (JsonException $e) { |
| 253 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 254 |
} catch (RequestException $e) { |
| 255 |
$response = $e->getResponse(); |
| 256 |
if ($response === null) { |
| 257 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 258 |
} |
| 259 |
throw new SquareApiException( |
| 260 |
message: "API request failed", |
| 261 |
statusCode: $response->getStatusCode(), |
| 262 |
body: $response->getBody()->getContents(), |
| 263 |
); |
| 264 |
} catch (ClientExceptionInterface $e) { |
| 265 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 266 |
} |
| 267 |
throw new SquareApiException( |
| 268 |
message: 'API request failed', |
| 269 |
statusCode: $statusCode, |
| 270 |
body: $response->getBody()->getContents(), |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Deletes multiple customer profiles. |
| 276 |
* |
| 277 |
* The endpoint takes a list of customer IDs and returns a map of responses. |
| 278 |
* |
| 279 |
* @param BulkDeleteCustomersRequest $request |
| 280 |
* @param ?array{ |
| 281 |
* baseUrl?: string, |
| 282 |
* maxRetries?: int, |
| 283 |
* timeout?: float, |
| 284 |
* headers?: array<string, string>, |
| 285 |
* queryParameters?: array<string, mixed>, |
| 286 |
* bodyProperties?: array<string, mixed>, |
| 287 |
* } $options |
| 288 |
* @return BulkDeleteCustomersResponse |
| 289 |
* @throws SquareException |
| 290 |
* @throws SquareApiException |
| 291 |
*/ |
| 292 |
public function bulkDeleteCustomers(BulkDeleteCustomersRequest $request, ?array $options = null): BulkDeleteCustomersResponse |
| 293 |
{ |
| 294 |
$options = array_merge($this->options, $options ?? []); |
| 295 |
try { |
| 296 |
$response = $this->client->sendRequest( |
| 297 |
new JsonApiRequest( |
| 298 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 299 |
path: "v2/customers/bulk-delete", |
| 300 |
method: HttpMethod::POST, |
| 301 |
body: $request, |
| 302 |
), |
| 303 |
$options, |
| 304 |
); |
| 305 |
$statusCode = $response->getStatusCode(); |
| 306 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 307 |
$json = $response->getBody()->getContents(); |
| 308 |
return BulkDeleteCustomersResponse::fromJson($json); |
| 309 |
} |
| 310 |
} catch (JsonException $e) { |
| 311 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 312 |
} catch (RequestException $e) { |
| 313 |
$response = $e->getResponse(); |
| 314 |
if ($response === null) { |
| 315 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 316 |
} |
| 317 |
throw new SquareApiException( |
| 318 |
message: "API request failed", |
| 319 |
statusCode: $response->getStatusCode(), |
| 320 |
body: $response->getBody()->getContents(), |
| 321 |
); |
| 322 |
} catch (ClientExceptionInterface $e) { |
| 323 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 324 |
} |
| 325 |
throw new SquareApiException( |
| 326 |
message: 'API request failed', |
| 327 |
statusCode: $statusCode, |
| 328 |
body: $response->getBody()->getContents(), |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Retrieves multiple customer profiles. |
| 334 |
* |
| 335 |
* This endpoint takes a list of customer IDs and returns a map of responses. |
| 336 |
* |
| 337 |
* @param BulkRetrieveCustomersRequest $request |
| 338 |
* @param ?array{ |
| 339 |
* baseUrl?: string, |
| 340 |
* maxRetries?: int, |
| 341 |
* timeout?: float, |
| 342 |
* headers?: array<string, string>, |
| 343 |
* queryParameters?: array<string, mixed>, |
| 344 |
* bodyProperties?: array<string, mixed>, |
| 345 |
* } $options |
| 346 |
* @return BulkRetrieveCustomersResponse |
| 347 |
* @throws SquareException |
| 348 |
* @throws SquareApiException |
| 349 |
*/ |
| 350 |
public function bulkRetrieveCustomers(BulkRetrieveCustomersRequest $request, ?array $options = null): BulkRetrieveCustomersResponse |
| 351 |
{ |
| 352 |
$options = array_merge($this->options, $options ?? []); |
| 353 |
try { |
| 354 |
$response = $this->client->sendRequest( |
| 355 |
new JsonApiRequest( |
| 356 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 357 |
path: "v2/customers/bulk-retrieve", |
| 358 |
method: HttpMethod::POST, |
| 359 |
body: $request, |
| 360 |
), |
| 361 |
$options, |
| 362 |
); |
| 363 |
$statusCode = $response->getStatusCode(); |
| 364 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 365 |
$json = $response->getBody()->getContents(); |
| 366 |
return BulkRetrieveCustomersResponse::fromJson($json); |
| 367 |
} |
| 368 |
} catch (JsonException $e) { |
| 369 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 370 |
} catch (RequestException $e) { |
| 371 |
$response = $e->getResponse(); |
| 372 |
if ($response === null) { |
| 373 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 374 |
} |
| 375 |
throw new SquareApiException( |
| 376 |
message: "API request failed", |
| 377 |
statusCode: $response->getStatusCode(), |
| 378 |
body: $response->getBody()->getContents(), |
| 379 |
); |
| 380 |
} catch (ClientExceptionInterface $e) { |
| 381 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 382 |
} |
| 383 |
throw new SquareApiException( |
| 384 |
message: 'API request failed', |
| 385 |
statusCode: $statusCode, |
| 386 |
body: $response->getBody()->getContents(), |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Updates multiple customer profiles. |
| 392 |
* |
| 393 |
* This endpoint takes a map of individual update requests and returns a map of responses. |
| 394 |
* |
| 395 |
* @param BulkUpdateCustomersRequest $request |
| 396 |
* @param ?array{ |
| 397 |
* baseUrl?: string, |
| 398 |
* maxRetries?: int, |
| 399 |
* timeout?: float, |
| 400 |
* headers?: array<string, string>, |
| 401 |
* queryParameters?: array<string, mixed>, |
| 402 |
* bodyProperties?: array<string, mixed>, |
| 403 |
* } $options |
| 404 |
* @return BulkUpdateCustomersResponse |
| 405 |
* @throws SquareException |
| 406 |
* @throws SquareApiException |
| 407 |
*/ |
| 408 |
public function bulkUpdateCustomers(BulkUpdateCustomersRequest $request, ?array $options = null): BulkUpdateCustomersResponse |
| 409 |
{ |
| 410 |
$options = array_merge($this->options, $options ?? []); |
| 411 |
try { |
| 412 |
$response = $this->client->sendRequest( |
| 413 |
new JsonApiRequest( |
| 414 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 415 |
path: "v2/customers/bulk-update", |
| 416 |
method: HttpMethod::POST, |
| 417 |
body: $request, |
| 418 |
), |
| 419 |
$options, |
| 420 |
); |
| 421 |
$statusCode = $response->getStatusCode(); |
| 422 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 423 |
$json = $response->getBody()->getContents(); |
| 424 |
return BulkUpdateCustomersResponse::fromJson($json); |
| 425 |
} |
| 426 |
} catch (JsonException $e) { |
| 427 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 428 |
} catch (RequestException $e) { |
| 429 |
$response = $e->getResponse(); |
| 430 |
if ($response === null) { |
| 431 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 432 |
} |
| 433 |
throw new SquareApiException( |
| 434 |
message: "API request failed", |
| 435 |
statusCode: $response->getStatusCode(), |
| 436 |
body: $response->getBody()->getContents(), |
| 437 |
); |
| 438 |
} catch (ClientExceptionInterface $e) { |
| 439 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 440 |
} |
| 441 |
throw new SquareApiException( |
| 442 |
message: 'API request failed', |
| 443 |
statusCode: $statusCode, |
| 444 |
body: $response->getBody()->getContents(), |
| 445 |
); |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Searches the customer profiles associated with a Square account using one or more supported query filters. |
| 450 |
* |
| 451 |
* Calling `SearchCustomers` without any explicit query filter returns all |
| 452 |
* customer profiles ordered alphabetically based on `given_name` and |
| 453 |
* `family_name`. |
| 454 |
* |
| 455 |
* Under normal operating conditions, newly created or updated customer profiles become available |
| 456 |
* for the search operation in well under 30 seconds. Occasionally, propagation of the new or updated |
| 457 |
* profiles can take closer to one minute or longer, especially during network incidents and outages. |
| 458 |
* |
| 459 |
* @param SearchCustomersRequest $request |
| 460 |
* @param ?array{ |
| 461 |
* baseUrl?: string, |
| 462 |
* maxRetries?: int, |
| 463 |
* timeout?: float, |
| 464 |
* headers?: array<string, string>, |
| 465 |
* queryParameters?: array<string, mixed>, |
| 466 |
* bodyProperties?: array<string, mixed>, |
| 467 |
* } $options |
| 468 |
* @return SearchCustomersResponse |
| 469 |
* @throws SquareException |
| 470 |
* @throws SquareApiException |
| 471 |
*/ |
| 472 |
public function search(SearchCustomersRequest $request = new SearchCustomersRequest(), ?array $options = null): SearchCustomersResponse |
| 473 |
{ |
| 474 |
$options = array_merge($this->options, $options ?? []); |
| 475 |
try { |
| 476 |
$response = $this->client->sendRequest( |
| 477 |
new JsonApiRequest( |
| 478 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 479 |
path: "v2/customers/search", |
| 480 |
method: HttpMethod::POST, |
| 481 |
body: $request, |
| 482 |
), |
| 483 |
$options, |
| 484 |
); |
| 485 |
$statusCode = $response->getStatusCode(); |
| 486 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 487 |
$json = $response->getBody()->getContents(); |
| 488 |
return SearchCustomersResponse::fromJson($json); |
| 489 |
} |
| 490 |
} catch (JsonException $e) { |
| 491 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 492 |
} catch (RequestException $e) { |
| 493 |
$response = $e->getResponse(); |
| 494 |
if ($response === null) { |
| 495 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 496 |
} |
| 497 |
throw new SquareApiException( |
| 498 |
message: "API request failed", |
| 499 |
statusCode: $response->getStatusCode(), |
| 500 |
body: $response->getBody()->getContents(), |
| 501 |
); |
| 502 |
} catch (ClientExceptionInterface $e) { |
| 503 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 504 |
} |
| 505 |
throw new SquareApiException( |
| 506 |
message: 'API request failed', |
| 507 |
statusCode: $statusCode, |
| 508 |
body: $response->getBody()->getContents(), |
| 509 |
); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Returns details for a single customer. |
| 514 |
* |
| 515 |
* @param GetCustomersRequest $request |
| 516 |
* @param ?array{ |
| 517 |
* baseUrl?: string, |
| 518 |
* maxRetries?: int, |
| 519 |
* timeout?: float, |
| 520 |
* headers?: array<string, string>, |
| 521 |
* queryParameters?: array<string, mixed>, |
| 522 |
* bodyProperties?: array<string, mixed>, |
| 523 |
* } $options |
| 524 |
* @return GetCustomerResponse |
| 525 |
* @throws SquareException |
| 526 |
* @throws SquareApiException |
| 527 |
*/ |
| 528 |
public function get(GetCustomersRequest $request, ?array $options = null): GetCustomerResponse |
| 529 |
{ |
| 530 |
$options = array_merge($this->options, $options ?? []); |
| 531 |
try { |
| 532 |
$response = $this->client->sendRequest( |
| 533 |
new JsonApiRequest( |
| 534 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 535 |
path: "v2/customers/{$request->getCustomerId()}", |
| 536 |
method: HttpMethod::GET, |
| 537 |
), |
| 538 |
$options, |
| 539 |
); |
| 540 |
$statusCode = $response->getStatusCode(); |
| 541 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 542 |
$json = $response->getBody()->getContents(); |
| 543 |
return GetCustomerResponse::fromJson($json); |
| 544 |
} |
| 545 |
} catch (JsonException $e) { |
| 546 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 547 |
} catch (RequestException $e) { |
| 548 |
$response = $e->getResponse(); |
| 549 |
if ($response === null) { |
| 550 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 551 |
} |
| 552 |
throw new SquareApiException( |
| 553 |
message: "API request failed", |
| 554 |
statusCode: $response->getStatusCode(), |
| 555 |
body: $response->getBody()->getContents(), |
| 556 |
); |
| 557 |
} catch (ClientExceptionInterface $e) { |
| 558 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 559 |
} |
| 560 |
throw new SquareApiException( |
| 561 |
message: 'API request failed', |
| 562 |
statusCode: $statusCode, |
| 563 |
body: $response->getBody()->getContents(), |
| 564 |
); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Updates a customer profile. This endpoint supports sparse updates, so only new or changed fields are required in the request. |
| 569 |
* To add or update a field, specify the new value. To remove a field, specify `null`. |
| 570 |
* |
| 571 |
* To update a customer profile that was created by merging existing profiles, you must use the ID of the newly created profile. |
| 572 |
* |
| 573 |
* @param UpdateCustomerRequest $request |
| 574 |
* @param ?array{ |
| 575 |
* baseUrl?: string, |
| 576 |
* maxRetries?: int, |
| 577 |
* timeout?: float, |
| 578 |
* headers?: array<string, string>, |
| 579 |
* queryParameters?: array<string, mixed>, |
| 580 |
* bodyProperties?: array<string, mixed>, |
| 581 |
* } $options |
| 582 |
* @return UpdateCustomerResponse |
| 583 |
* @throws SquareException |
| 584 |
* @throws SquareApiException |
| 585 |
*/ |
| 586 |
public function update(UpdateCustomerRequest $request, ?array $options = null): UpdateCustomerResponse |
| 587 |
{ |
| 588 |
$options = array_merge($this->options, $options ?? []); |
| 589 |
try { |
| 590 |
$response = $this->client->sendRequest( |
| 591 |
new JsonApiRequest( |
| 592 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 593 |
path: "v2/customers/{$request->getCustomerId()}", |
| 594 |
method: HttpMethod::PUT, |
| 595 |
body: $request, |
| 596 |
), |
| 597 |
$options, |
| 598 |
); |
| 599 |
$statusCode = $response->getStatusCode(); |
| 600 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 601 |
$json = $response->getBody()->getContents(); |
| 602 |
return UpdateCustomerResponse::fromJson($json); |
| 603 |
} |
| 604 |
} catch (JsonException $e) { |
| 605 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 606 |
} catch (RequestException $e) { |
| 607 |
$response = $e->getResponse(); |
| 608 |
if ($response === null) { |
| 609 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 610 |
} |
| 611 |
throw new SquareApiException( |
| 612 |
message: "API request failed", |
| 613 |
statusCode: $response->getStatusCode(), |
| 614 |
body: $response->getBody()->getContents(), |
| 615 |
); |
| 616 |
} catch (ClientExceptionInterface $e) { |
| 617 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 618 |
} |
| 619 |
throw new SquareApiException( |
| 620 |
message: 'API request failed', |
| 621 |
statusCode: $statusCode, |
| 622 |
body: $response->getBody()->getContents(), |
| 623 |
); |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Deletes a customer profile from a business. |
| 628 |
* |
| 629 |
* To delete a customer profile that was created by merging existing profiles, you must use the ID of the newly created profile. |
| 630 |
* |
| 631 |
* @param DeleteCustomersRequest $request |
| 632 |
* @param ?array{ |
| 633 |
* baseUrl?: string, |
| 634 |
* maxRetries?: int, |
| 635 |
* timeout?: float, |
| 636 |
* headers?: array<string, string>, |
| 637 |
* queryParameters?: array<string, mixed>, |
| 638 |
* bodyProperties?: array<string, mixed>, |
| 639 |
* } $options |
| 640 |
* @return DeleteCustomerResponse |
| 641 |
* @throws SquareException |
| 642 |
* @throws SquareApiException |
| 643 |
*/ |
| 644 |
public function delete(DeleteCustomersRequest $request, ?array $options = null): DeleteCustomerResponse |
| 645 |
{ |
| 646 |
$options = array_merge($this->options, $options ?? []); |
| 647 |
$query = []; |
| 648 |
if ($request->getVersion() != null) { |
| 649 |
$query['version'] = $request->getVersion(); |
| 650 |
} |
| 651 |
try { |
| 652 |
$response = $this->client->sendRequest( |
| 653 |
new JsonApiRequest( |
| 654 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 655 |
path: "v2/customers/{$request->getCustomerId()}", |
| 656 |
method: HttpMethod::DELETE, |
| 657 |
query: $query, |
| 658 |
), |
| 659 |
$options, |
| 660 |
); |
| 661 |
$statusCode = $response->getStatusCode(); |
| 662 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 663 |
$json = $response->getBody()->getContents(); |
| 664 |
return DeleteCustomerResponse::fromJson($json); |
| 665 |
} |
| 666 |
} catch (JsonException $e) { |
| 667 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 668 |
} catch (RequestException $e) { |
| 669 |
$response = $e->getResponse(); |
| 670 |
if ($response === null) { |
| 671 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 672 |
} |
| 673 |
throw new SquareApiException( |
| 674 |
message: "API request failed", |
| 675 |
statusCode: $response->getStatusCode(), |
| 676 |
body: $response->getBody()->getContents(), |
| 677 |
); |
| 678 |
} catch (ClientExceptionInterface $e) { |
| 679 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 680 |
} |
| 681 |
throw new SquareApiException( |
| 682 |
message: 'API request failed', |
| 683 |
statusCode: $statusCode, |
| 684 |
body: $response->getBody()->getContents(), |
| 685 |
); |
| 686 |
} |
| 687 |
|
| 688 |
/** |
| 689 |
* Lists customer profiles associated with a Square account. |
| 690 |
* |
| 691 |
* Under normal operating conditions, newly created or updated customer profiles become available |
| 692 |
* for the listing operation in well under 30 seconds. Occasionally, propagation of the new or updated |
| 693 |
* profiles can take closer to one minute or longer, especially during network incidents and outages. |
| 694 |
* |
| 695 |
* @param ListCustomersRequest $request |
| 696 |
* @param ?array{ |
| 697 |
* baseUrl?: string, |
| 698 |
* maxRetries?: int, |
| 699 |
* timeout?: float, |
| 700 |
* headers?: array<string, string>, |
| 701 |
* queryParameters?: array<string, mixed>, |
| 702 |
* bodyProperties?: array<string, mixed>, |
| 703 |
* } $options |
| 704 |
* @return ListCustomersResponse |
| 705 |
* @throws SquareException |
| 706 |
* @throws SquareApiException |
| 707 |
*/ |
| 708 |
private function _list(ListCustomersRequest $request = new ListCustomersRequest(), ?array $options = null): ListCustomersResponse |
| 709 |
{ |
| 710 |
$options = array_merge($this->options, $options ?? []); |
| 711 |
$query = []; |
| 712 |
if ($request->getCursor() != null) { |
| 713 |
$query['cursor'] = $request->getCursor(); |
| 714 |
} |
| 715 |
if ($request->getLimit() != null) { |
| 716 |
$query['limit'] = $request->getLimit(); |
| 717 |
} |
| 718 |
if ($request->getSortField() != null) { |
| 719 |
$query['sort_field'] = $request->getSortField(); |
| 720 |
} |
| 721 |
if ($request->getSortOrder() != null) { |
| 722 |
$query['sort_order'] = $request->getSortOrder(); |
| 723 |
} |
| 724 |
if ($request->getCount() != null) { |
| 725 |
$query['count'] = $request->getCount(); |
| 726 |
} |
| 727 |
try { |
| 728 |
$response = $this->client->sendRequest( |
| 729 |
new JsonApiRequest( |
| 730 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 731 |
path: "v2/customers", |
| 732 |
method: HttpMethod::GET, |
| 733 |
query: $query, |
| 734 |
), |
| 735 |
$options, |
| 736 |
); |
| 737 |
$statusCode = $response->getStatusCode(); |
| 738 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 739 |
$json = $response->getBody()->getContents(); |
| 740 |
return ListCustomersResponse::fromJson($json); |
| 741 |
} |
| 742 |
} catch (JsonException $e) { |
| 743 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 744 |
} catch (RequestException $e) { |
| 745 |
$response = $e->getResponse(); |
| 746 |
if ($response === null) { |
| 747 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 748 |
} |
| 749 |
throw new SquareApiException( |
| 750 |
message: "API request failed", |
| 751 |
statusCode: $response->getStatusCode(), |
| 752 |
body: $response->getBody()->getContents(), |
| 753 |
); |
| 754 |
} catch (ClientExceptionInterface $e) { |
| 755 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 756 |
} |
| 757 |
throw new SquareApiException( |
| 758 |
message: 'API request failed', |
| 759 |
statusCode: $statusCode, |
| 760 |
body: $response->getBody()->getContents(), |
| 761 |
); |
| 762 |
} |
| 763 |
} |
| 764 |
|