|
17 | 17 | use JouwWeb\Sendcloud\Model\ShippingMethod;
|
18 | 18 | use JouwWeb\Sendcloud\Model\User;
|
19 | 19 | use JouwWeb\Sendcloud\Model\WebhookEvent;
|
| 20 | +use JouwWeb\Sendcloud\Model\ServicePoint; |
20 | 21 | use Psr\Http\Message\RequestInterface;
|
21 | 22 |
|
22 | 23 | /**
|
@@ -226,7 +227,7 @@ public function createMultiParcel(
|
226 | 227 | ?ShippingMethod $shippingMethod = null,
|
227 | 228 | ?string $errors = null,
|
228 | 229 | int $quantity = 1
|
229 |
| - ) : array { |
| 230 | + ): array { |
230 | 231 | $parcelData = $this->getParcelData(
|
231 | 232 | null,
|
232 | 233 | $shippingAddress,
|
@@ -521,6 +522,135 @@ public function getReturnPortalUrl(Parcel|int $parcel): ?string
|
521 | 522 | }
|
522 | 523 | }
|
523 | 524 |
|
| 525 | + /** |
| 526 | + * Summary of searchServicePoints |
| 527 | + * |
| 528 | + * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Flist-service-points |
| 529 | + * @param string $country A country ISO 2 code (Example : 'NL') |
| 530 | + * @param string|null $address Address of the destination address. Can accept postal code instead of the street and the house number. (Example : 'Stadhuisplein 10') |
| 531 | + * @param string|null $carrier A comma-separated list of carrier codes (stringified) (Example : 'postnl,dpd') |
| 532 | + * @param string|null $city City of the destination address. (Example : 'Eindhoven') |
| 533 | + * @param string|null $houseNumber House number of the destination address. (Example : '10') |
| 534 | + * @param string|null $latitude Used as a reference point to calculate the distance of the service point to the provided location. |
| 535 | + * @param string|null $longitude Used as a reference point to calculate the distance of the service point to the provided location. |
| 536 | + * @param string|null $neLatitude Latitude of the northeast corner of the bounding box. |
| 537 | + * @param string|null $neLongitude Longitude of the northeast corner of the bounding box. |
| 538 | + * @param string|null $postalCode Postal code of the destination address. Using postal_code will return you service points located around that particular postal code. (Example : '5611 EM') |
| 539 | + * @param string|null $pudoId DPD-specific query parameter. (<= 7 characters) |
| 540 | + * @param int|null $radius Radius (in meter) of a bounding circle. Can be used instead of the ne_latitude, ne_longitude, sw_latitude, and sw_longitude parameters to define a bounding box. By default, it’s 100 meters. Minimum value: 100 meters. Maximum value: 50 000 meters. |
| 541 | + * @param string|null $shopType Filters results by their shop type. |
| 542 | + * @param string|null $swLatitude Latitude of the southwest corner of the bounding box. |
| 543 | + * @param string|null $swLongitude Longitude of the southwest corner of the bounding box. |
| 544 | + * @param float|null $weight Weight (in kg.) of the parcel to be shipped to the service points. Certain carriers impose limits for certain service points that cannot accept parcels above a certain weight limit. |
| 545 | + * @return ServicePoint[] |
| 546 | + */ |
| 547 | + public function searchServicePoints( |
| 548 | + string $country, |
| 549 | + ?string $address = null, |
| 550 | + ?string $carrier = null, |
| 551 | + ?string $city = null, |
| 552 | + ?string $houseNumber = null, |
| 553 | + ?string $latitude = null, |
| 554 | + ?string $longitude = null, |
| 555 | + ?string $neLatitude = null, |
| 556 | + ?string $neLongitude = null, |
| 557 | + ?string $postalCode = null, |
| 558 | + ?string $pudoId = null, |
| 559 | + ?int $radius = null, |
| 560 | + ?string $shopType = null, |
| 561 | + ?string $swLatitude = null, |
| 562 | + ?string $swLongitude = null, |
| 563 | + ?float $weight = null |
| 564 | + ): array { |
| 565 | + try { |
| 566 | + // Construct query array |
| 567 | + $query = []; |
| 568 | + $query['country_id'] = $country; |
| 569 | + |
| 570 | + if (isset($address)) { |
| 571 | + $query['address'] = $address; |
| 572 | + } |
| 573 | + if (isset($carrier)) { |
| 574 | + $query['carrier'] = $carrier; |
| 575 | + } |
| 576 | + if (isset($city)) { |
| 577 | + $query['city'] = $city; |
| 578 | + } |
| 579 | + if (isset($houseNumber)) { |
| 580 | + $query['house_number'] = $houseNumber; |
| 581 | + } |
| 582 | + if (isset($latitude)) { |
| 583 | + $query['latitude'] = $latitude; |
| 584 | + } |
| 585 | + if (isset($longitude)) { |
| 586 | + $query['longitude'] = $longitude; |
| 587 | + } |
| 588 | + if (isset($neLatitude)) { |
| 589 | + $query['ne_latitude'] = $neLatitude; |
| 590 | + } |
| 591 | + if (isset($neLongitude)) { |
| 592 | + $query['ne_longitude'] = $neLongitude; |
| 593 | + } |
| 594 | + if (isset($postalCode)) { |
| 595 | + $query['postal_code'] = $postalCode; |
| 596 | + } |
| 597 | + if (isset($pudoId)) { |
| 598 | + $query['pudo_id'] = $pudoId; |
| 599 | + } |
| 600 | + if (isset($radius)) { |
| 601 | + $query['radius'] = $radius; |
| 602 | + } |
| 603 | + if (isset($shopType)) { |
| 604 | + $query['shop_type'] = $shopType; |
| 605 | + } |
| 606 | + if (isset($swLatitude)) { |
| 607 | + $query['sw_latitude'] = $swLatitude; |
| 608 | + } |
| 609 | + if (isset($swLongitude)) { |
| 610 | + $query['sw_longitude'] = $swLongitude; |
| 611 | + } |
| 612 | + if (isset($weight)) { |
| 613 | + $query['weight'] = $weight; |
| 614 | + } |
| 615 | + |
| 616 | + // Send request |
| 617 | + $response = $this->guzzleClient->get('service-point', [ |
| 618 | + 'query' => $query, |
| 619 | + ]); |
| 620 | + |
| 621 | + // Decode and create ServicePoint objects |
| 622 | + $json = json_decode((string)$response->getBody(), true); |
| 623 | + |
| 624 | + $servicePoints = []; |
| 625 | + foreach ($json as $obj) { |
| 626 | + $servicePoints[] = ServicePoint::fromData($obj); |
| 627 | + } |
| 628 | + |
| 629 | + return $servicePoints; |
| 630 | + } catch (TransferException $exception) { |
| 631 | + throw $this->parseGuzzleException($exception, 'Could not retrieve service point.'); |
| 632 | + } |
| 633 | + } |
| 634 | + |
| 635 | + /** |
| 636 | + * Returns service point by ID. |
| 637 | + * |
| 638 | + * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Fget-a-service-point |
| 639 | + * @return ServicePoint |
| 640 | + * @throws SendcloudRequestException |
| 641 | + */ |
| 642 | + public function getServicePoint(ServicePoint|int $servicePoint): ServicePoint |
| 643 | + { |
| 644 | + $servicePointId = $servicePoint instanceof ServicePoint ? $servicePoint->getId() : $servicePoint; |
| 645 | + |
| 646 | + try { |
| 647 | + $response = $this->guzzleClient->get('service-point/' . $servicePointId); |
| 648 | + return ServicePoint::fromData(json_decode((string)$response->getBody(), true)); |
| 649 | + } catch (TransferException $exception) { |
| 650 | + throw $this->parseGuzzleException($exception, 'Could not retrieve service point.'); |
| 651 | + } |
| 652 | + } |
| 653 | + |
524 | 654 | /**
|
525 | 655 | * Returns the given arguments as data in Sendcloud parcel format.
|
526 | 656 | *
|
@@ -719,6 +849,7 @@ protected function parseGuzzleException(
|
719 | 849 | return new SendcloudRequestException($message, $code, $exception, $responseCode, $responseMessage);
|
720 | 850 | }
|
721 | 851 |
|
| 852 | + // TODO: Remove parseParcelArgument() now we use native unions. |
722 | 853 | protected function parseParcelArgument(Parcel|int $parcel): int
|
723 | 854 | {
|
724 | 855 | if (is_int($parcel)) {
|
|
0 commit comments