-
Notifications
You must be signed in to change notification settings - Fork 14
/
Client.php
717 lines (636 loc) · 27.9 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
namespace JouwWeb\Sendcloud;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Utils;
use JouwWeb\Sendcloud\Exception\SendcloudClientException;
use JouwWeb\Sendcloud\Exception\SendcloudRequestException;
use JouwWeb\Sendcloud\Exception\SendcloudStateException;
use JouwWeb\Sendcloud\Exception\SendcloudWebhookException;
use JouwWeb\Sendcloud\Model\Address;
use JouwWeb\Sendcloud\Model\Parcel;
use JouwWeb\Sendcloud\Model\ParcelItem;
use JouwWeb\Sendcloud\Model\SenderAddress;
use JouwWeb\Sendcloud\Model\ShippingMethod;
use JouwWeb\Sendcloud\Model\User;
use JouwWeb\Sendcloud\Model\WebhookEvent;
use Psr\Http\Message\RequestInterface;
/**
* Client to perform calls on the Sendcloud API.
*/
class Client
{
protected const API_BASE_URL = 'https://panel.sendcloud.sc/api/v2/';
protected \GuzzleHttp\Client $guzzleClient;
public function __construct(
protected string $publicKey,
protected string $secretKey,
protected ?string $partnerId = null,
?string $apiBaseUrl = null
) {
$clientConfig = [
'base_uri' => $apiBaseUrl ?: self::API_BASE_URL,
'timeout' => 60, // Mainly because the shipping methods endpoint can take a very long time to respond.
'auth' => [
$publicKey,
$secretKey,
],
'headers' => [
'User-Agent' => 'jouwweb/sendcloud ' . Utils::defaultUserAgent(),
],
];
if ($this->partnerId) {
$clientConfig['headers']['Sendcloud-Partner-Id'] = $this->partnerId;
}
$this->guzzleClient = new \GuzzleHttp\Client($clientConfig);
}
/**
* Fetches basic details about the Sendcloud account.
*
* @throws SendcloudRequestException
*/
public function getUser(): User
{
try {
return User::fromData(json_decode((string)$this->guzzleClient->get('user')->getBody(), true)['user']);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'An error occurred while fetching the Sendcloud user.');
}
}
/**
* Fetches available Sendcloud shipping methods.
*
* @param int|null $servicePointId When passed, only methods able to ship to the service point will be returned.
* @param SenderAddress|int|null $senderAddress The sender address to ship from. Methods available to all of your
* account's sender addresses will be retrieved when null.
* @param bool $returnMethodsOnly When true, methods for making a return are returned instead.
* @return ShippingMethod[]
* @throws SendcloudClientException
* @see https://sendcloud.dev/docs/shipping/shipping-methods/
*/
public function getShippingMethods(
?int $servicePointId = null,
SenderAddress|int|null $senderAddress = null,
bool $returnMethodsOnly = false
): array {
try {
$queryData = [];
if ($servicePointId !== null) {
$queryData['service_point_id'] = $servicePointId;
}
if ($senderAddress !== null) {
if ($senderAddress instanceof SenderAddress) {
$senderAddress = $senderAddress->getId();
}
if (!is_int($senderAddress)) {
throw new \InvalidArgumentException(
'$senderAddress must be an integer or SenderAddress when passed.'
);
}
} else {
$senderAddress = 'all';
}
$queryData['sender_address'] = $senderAddress;
if ($returnMethodsOnly) {
$queryData['is_return'] = 'true';
}
$response = $this->guzzleClient->get('shipping_methods', [
RequestOptions::QUERY => $queryData,
]);
$shippingMethodsData = json_decode((string)$response->getBody(), true)['shipping_methods'];
$shippingMethods = array_map(fn (array $shippingMethodData) => (
ShippingMethod::fromData($shippingMethodData)
), $shippingMethodsData);
// Sort by carrier and name
usort($shippingMethods, function (ShippingMethod $method1, ShippingMethod $method2) {
if ($method1->getCarrier() !== $method2->getCarrier()) {
return strcasecmp($method1->getCarrier(), $method2->getCarrier());
}
return strcasecmp($method1->getName(), $method2->getName());
});
return $shippingMethods;
} catch (TransferException $exception) {
throw Utility::parseGuzzleException(
$exception,
'An error occurred while fetching shipping methods from the Sendcloud API.'
);
}
}
/**
* Creates a parcel in Sendcloud.
*
* @param Address $shippingAddress Address to be shipped to.
* @param int|null $servicePointId The order will be shipped to the service point if supplied. $shippingAddress is
* still required as it will be printed on the label.
* @param string|null $orderNumber
* @param int|null $weight Weight of the parcel in grams. The default set in Sendcloud will be used if null or zero.
* @param string|null $customsInvoiceNumber
* @param int|null $customsShipmentType One of {@see Parcel::CUSTOMS_SHIPMENT_TYPES}.
* @param ParcelItem[]|null $items Items contained in the parcel.
* @param string|null $postNumber Number that may be required to send to a service point.
* @param ?ShippingMethod $shippingMethod
* @param string|null $errors One of {@see Parcel::ERRORS_VERBOSE}.
* @return Parcel
* @throws SendcloudRequestException
* @see https://sendcloud.dev/docs/shipping/create-a-parcel/
*/
public function createParcel(
Address $shippingAddress,
?int $servicePointId,
?string $orderNumber = null,
?int $weight = null,
?string $customsInvoiceNumber = null,
?int $customsShipmentType = null,
?array $items = null,
?string $postNumber = null,
?ShippingMethod $shippingMethod = null,
?string $errors = null,
?string $shippingMethodCheckoutName = null,
): Parcel {
$parcelData = $this->createParcelData(
shippingAddress: $shippingAddress,
servicePointId: $servicePointId,
orderNumber: $orderNumber,
weight: $weight,
shippingMethod: $shippingMethod,
customsInvoiceNumber: $customsInvoiceNumber,
customsShipmentType: $customsShipmentType,
items: $items,
postNumber: $postNumber,
shippingMethodCheckoutName: $shippingMethodCheckoutName,
);
try {
$requestOptions = [
RequestOptions::JSON => [
'parcel' => $parcelData,
],
];
if (isset($errors)){
$requestOptions[RequestOptions::QUERY] = ['errors' => $errors];
}
$response = $this->guzzleClient->post('parcels', $requestOptions);
return Parcel::fromData(json_decode((string)$response->getBody(), true)['parcel']);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not create parcel in Sendcloud.');
}
}
/**
* Creates a multi-collo parcel in Sendcloud.
*
* @param Address $shippingAddress Address to be shipped to.
* @param int|null $servicePointId The order will be shipped to the service point if supplied. $shippingAddress is
* still required as it will be printed on the label.
* @param int|null $weight Weight of the parcel in grams. The default set in Sendcloud will be used if null or zero.
* @param int|null $customsShipmentType One of {@see Parcel::CUSTOMS_SHIPMENT_TYPES}.
* @param ParcelItem[]|null $items Items contained in the parcel.
* @param string|null $postNumber Number that may be required to send to a service point.
* @param int $quantity Number of parcels to generate for multi-collo shipment.
* @param string|null $errors One of {@see Parcel::ERRORS_VERBOSE}.
* @return Parcel[]
* @throws SendcloudRequestException
* @see https://sendcloud.dev/docs/shipping/multicollo/
*/
public function createMultiParcel(
Address $shippingAddress,
?int $servicePointId,
?string $orderNumber = null,
?int $weight = null,
?string $customsInvoiceNumber = null,
?int $customsShipmentType = null,
?array $items = null,
?string $postNumber = null,
?ShippingMethod $shippingMethod = null,
?string $errors = null,
int $quantity = 1
): array {
$parcelData = $this->createParcelData(
shippingAddress: $shippingAddress,
servicePointId: $servicePointId,
orderNumber: $orderNumber,
weight: $weight,
requestLabel: true,
shippingMethod: $shippingMethod,
customsInvoiceNumber: $customsInvoiceNumber,
customsShipmentType: $customsShipmentType,
items: $items,
postNumber: $postNumber,
);
$parcelData['quantity'] = $quantity;
try {
$parcels = [];
$requestOptions = [
RequestOptions::JSON => [
'parcels' => [$parcelData],
],
];
if (isset($errors)){
$requestOptions[RequestOptions::QUERY] = ['errors' => $errors];
}
$response = $this->guzzleClient->post('parcels', $requestOptions);
$json = json_decode((string)$response->getBody(), true);
// Retrieve successfully created parcels
foreach ($json['parcels'] as $parcel) {
$parcels[] = Parcel::fromData($parcel);
}
// Retrieve failed parcels
/*
if (isset($json['failed_parcels'])) {
foreach ($json['failed_parcels'] as $parcel) {
$parcels[] = Parcel::fromData($parcel);
}
}
*/
return $parcels;
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not create parcel in Sendcloud.');
}
}
/**
* Update details of an existing parcel.
*
* @throws SendcloudRequestException
*/
public function updateParcel(Parcel|int $parcel, Address $shippingAddress): Parcel
{
$parcelData = $this->createParcelData(
is_int($parcel) ? $parcel : $parcel->getId(),
shippingAddress: $shippingAddress,
);
try {
$response = $this->guzzleClient->put('parcels', [
RequestOptions::JSON => [
'parcel' => $parcelData,
],
]);
return Parcel::fromData(json_decode((string)$response->getBody(), true)['parcel']);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not update parcel in Sendcloud.');
}
}
/**
* Request a label for an existing parcel.
* @param SenderAddress|Address|int|null $senderAddress Passing null will pick Sendcloud's default. An Address will
* use undocumented behavior that will disable branding personalizations.
* @param bool $applyShippingRules shipping rules can override the given shippingMethod and can be configured in the
* sendcloud control panel
* @throws SendcloudRequestException
*/
public function createLabel(Parcel|int $parcel, ShippingMethod|int $shippingMethod, SenderAddress|Address|int|null $senderAddress, bool $applyShippingRules = false): Parcel
{
$parcelData = $this->createParcelData(
parcelId: is_int($parcel) ? $parcel : $parcel->getId(),
requestLabel: true,
shippingMethod: $shippingMethod,
senderAddress: $senderAddress,
applyShippingRules: $applyShippingRules
);
try {
$response = $this->guzzleClient->put('parcels', [
RequestOptions::JSON => [
'parcel' => $parcelData,
],
]);
return Parcel::fromData(json_decode((string)$response->getBody(), true)['parcel']);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not create parcel with Sendcloud.');
}
}
/**
* Cancels or deletes a parcel (depending on status). Returns whether the parcel was successfully cancelled.
*
* @throws SendcloudRequestException
*/
public function cancelParcel(Parcel|int $parcel): bool
{
try {
$parcelId = is_int($parcel) ? $parcel : $parcel->getId();
$this->guzzleClient->post(sprintf('parcels/%s/cancel', $parcelId));
return true;
} catch (TransferException $exception) {
$statusCode = ($exception instanceof RequestException && $exception->hasResponse()
? $exception->getResponse()->getStatusCode()
: 0
);
// Handle documented rejections
if (in_array($statusCode, [400, 410])) {
return false;
}
throw Utility::parseGuzzleException($exception, 'An error occurred while cancelling the parcel.');
}
}
/**
* Fetches the PDF label for the given parcel. The parcel must already have a label created.
*
* @param int $format `Parcel::LABEL_FORMATS`
* @return string PDF data.
* @throws SendcloudClientException
*/
public function getLabelPdf(Parcel|int $parcel, int $format): string
{
if (!in_array($format, Parcel::LABEL_FORMATS)) {
throw new \InvalidArgumentException('Invalid label format given.');
}
if (is_int($parcel)) {
$parcel = $this->getParcel($parcel);
} elseif (!($parcel instanceof Parcel)) {
throw new \InvalidArgumentException('parcel must be an integer or a Parcel.');
}
$labelUrl = $parcel->getLabelUrl($format);
if (!$labelUrl) {
throw new SendcloudStateException('Sendcloud parcel does not have any labels.');
}
try {
return (string)$this->guzzleClient->get($labelUrl)->getBody();
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not retrieve label.');
}
}
/**
* Fetches a single PDF containing labels for all the given parcels. Parcels that do not have a label available will
* not be contained in the PDF. If only parcels without a label have been requested it will result in an exception.
*
* @param array<Parcel|int> $parcels
* @param int $format One of `Parcel::LABEL_FORMATS`. The A4 formats will contain up to 4 labels per page.
* @return string PDF data.
* @throws SendcloudClientException
*/
public function getBulkLabelPdf(array $parcels, int $format): string
{
$parcelIds = [];
foreach ($parcels as $parcel) {
if (is_int($parcel)) {
$parcelIds[] = $parcel;
} elseif ($parcel instanceof Parcel) {
$parcelIds[] = $parcel->getId();
} else {
throw new \InvalidArgumentException('Parcels must be Parcel instances or IDs.');
}
}
try {
$response = $this->guzzleClient->post('labels', [
RequestOptions::JSON => [
'label' => [
'parcels' => $parcelIds,
]
],
]);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not retrieve label information.');
}
$labelData = json_decode((string)$response->getBody(), true);
$labelUrl = Utility::getLabelUrlFromData($labelData, $format);
if (!$labelUrl) {
throw new SendcloudStateException('No label URL could be obtained from the response.');
}
try {
return (string)$this->guzzleClient->get($labelUrl)->getBody();
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not retrieve label PDF data.');
}
}
/**
* Fetches a single parcel document based on requested document type, content type, and dpi.
*
* If the parcel hasn't had its labels created through {@see self::createLabel()} it will result in an exception.
*
* @param string $documentType One of {@see Parcel::DOCUMENT_TYPES}
* @param string $contentType One of {@see Parcel::DOCUMENT_CONTENT_TYPES}
* @param int $dpi One of {@see Parcel::DOCUMENT_DPI_VALUES} limited by the selected `$contentType`
* @return string The contents of the requested document
* @throws SendcloudClientException
*/
public function getParcelDocument(Parcel|int $parcel, string $documentType, string $contentType = Parcel::DOCUMENT_CONTENT_TYPE_PDF, int $dpi = Parcel::DOCUMENT_DPI_72): string
{
if (!in_array($documentType, Parcel::DOCUMENT_TYPES, true)) {
throw new \InvalidArgumentException(sprintf('Document type "%s" is not accepted. Valid types: %s.', $documentType, implode(', ', Parcel::DOCUMENT_TYPES)));
}
if (!in_array($contentType, Parcel::DOCUMENT_CONTENT_TYPES, true)) {
throw new \InvalidArgumentException(sprintf('Content type "%s" is not accepted. Valid types: %s.', $contentType, implode(', ', Parcel::DOCUMENT_CONTENT_TYPES)));
}
if (!in_array($dpi, Parcel::DOCUMENT_DPI_VALUES[$contentType] ?? [], true)) {
throw new \InvalidArgumentException(sprintf('DPI "%d" is not accepted for "%s". Valid values: %s.', $dpi, $contentType, implode(', ', Parcel::DOCUMENT_DPI_VALUES[$contentType])));
}
try {
$parcelId = is_int($parcel) ? $parcel : $parcel->getId();
return (string)$this->guzzleClient->get(sprintf('parcels/%s/documents/%s', $parcelId, $documentType), [
RequestOptions::QUERY => ['dpi' => $dpi],
RequestOptions::HEADERS => ['Accept' => $contentType],
])->getBody();
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, sprintf('Could not retrieve parcel document "%s" for parcel id "%d".', $documentType, $parcelId));
}
}
/**
* Fetches the sender addresses configured in Sendcloud.
*
* @return SenderAddress[]
* @throws SendcloudRequestException
*/
public function getSenderAddresses(): array
{
try {
$response = $this->guzzleClient->get('user/addresses/sender');
$senderAddressesData = json_decode((string)$response->getBody(), true)['sender_addresses'];
return array_map(function (array $senderAddressData) {
return SenderAddress::fromData($senderAddressData);
}, $senderAddressesData);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not retrieve sender addresses.');
}
}
/**
* Retrieves current parcel data from Sendcloud.
*
* @throws SendcloudClientException
*/
public function getParcel(Parcel|int $parcel): Parcel
{
try {
$parcelId = is_int($parcel) ? $parcel : $parcel->getId();
$response = $this->guzzleClient->get(sprintf('parcels/%s', $parcelId));
return Parcel::fromData(json_decode((string)$response->getBody(), true)['parcel']);
} catch (TransferException $exception) {
throw Utility::parseGuzzleException($exception, 'Could not retrieve parcel.');
}
}
/**
* Parse a webhook event using the client's secret key. See {@see Utility::parseWebhookRequest()} for specifics.
*
* @throws SendcloudWebhookException
*/
public function parseWebhookRequest(RequestInterface $request): WebhookEvent
{
return Utility::parseWebhookRequest($request, $this->secretKey);
}
/**
* Returns the return portal URL for the given parcel. Returns `null` when no return portal is configured or the
* parcel is not associated with a brand.
*/
public function getReturnPortalUrl(Parcel|int $parcel): ?string
{
try {
$parcelId = is_int($parcel) ? $parcel : $parcel->getId();
$response = $this->guzzleClient->get(sprintf(
'parcels/%s/return_portal_url',
$parcelId
));
return (string)json_decode($response->getBody(), true)['url'];
} catch (RequestException $exception) {
if ($exception->getResponse() && $exception->getResponse()->getStatusCode() === 404) {
return null;
}
throw $exception;
}
}
/**
* Returns the given arguments as data in Sendcloud parcel format.
*
* @param ShippingMethod|int|null $shippingMethod Required if requesting a label.
* @param SenderAddress|Address|int|null $senderAddress Passing null will pick Sendcloud's default. An Address will
* use undocumented behavior that will disable branding personalizations.
* @param int|null $customsShipmentType One of {@see Parcel::CUSTOMS_SHIPMENT_TYPES}.
*/
protected function createParcelData(
?int $parcelId = null,
?Address $shippingAddress = null,
?string $servicePointId = null,
?string $orderNumber = null,
?int $weight = null,
bool $requestLabel = false,
ShippingMethod|int|null $shippingMethod = null,
SenderAddress|Address|int|null $senderAddress = null,
?string $customsInvoiceNumber = null,
?int $customsShipmentType = null,
?array $items = null,
?string $postNumber = null,
bool $applyShippingRules = false,
?string $shippingMethodCheckoutName = null,
): array {
$parcelData = [];
if ($parcelId) {
$parcelData['id'] = $parcelId;
}
if ($shippingAddress) {
$parcelData = array_merge($parcelData, [
'name' => $shippingAddress->getName(),
'company_name' => $shippingAddress->getCompanyName() ?? '',
'address' => $shippingAddress->getAddressLine1(),
'address_2' => $shippingAddress->getAddressLine2() ?? '',
'house_number' => $shippingAddress->getHouseNumber() ?? '',
'city' => $shippingAddress->getCity(),
'postal_code' => $shippingAddress->getPostalCode(),
'country' => $shippingAddress->getCountryCode(),
'email' => $shippingAddress->getEmailAddress(),
'telephone' => $shippingAddress->getPhoneNumber() ?? '',
'country_state' => $shippingAddress->getCountryStateCode() ?? '',
]);
}
if ($servicePointId) {
$parcelData['to_service_point'] = $servicePointId;
}
if ($postNumber) {
$parcelData['to_post_number'] = $postNumber;
}
if ($orderNumber) {
$parcelData['order_number'] = $orderNumber;
}
if ($weight) {
$parcelData['weight'] = number_format($weight / 1000, 3);
}
if ($customsInvoiceNumber) {
$parcelData['customs_invoice_nr'] = $customsInvoiceNumber;
}
if ($customsShipmentType !== null) {
if (!in_array($customsShipmentType, Parcel::CUSTOMS_SHIPMENT_TYPES)) {
throw new \InvalidArgumentException(sprintf('Invalid customs shipment type %s.', $customsShipmentType));
}
$parcelData['customs_shipment_type'] = $customsShipmentType;
}
if ($items) {
$itemsData = [];
foreach (array_values($items) as $index => $item) {
if (!($item instanceof ParcelItem)) {
throw new \InvalidArgumentException(sprintf(
'Parcel item at index %s is not an instance of ParcelItem.',
$index
));
}
$itemData = [
'description' => $item->getDescription(),
'quantity' => $item->getQuantity(),
'weight' => number_format($item->getWeight() / 1000, 3),
'value' => $item->getValue(),
];
if ($item->getHarmonizedSystemCode()) {
$itemData['hs_code'] = $item->getHarmonizedSystemCode();
}
if ($item->getOriginCountryCode()) {
$itemData['origin_country'] = $item->getOriginCountryCode();
}
if ($item->getSku()) {
$itemData['sku'] = $item->getSku();
}
if ($item->getProductId()) {
$itemData['product_id'] = $item->getProductId();
}
if ($item->getProperties()) {
$itemData['properties'] = $item->getProperties();
}
$itemsData[] = $itemData;
}
$parcelData['parcel_items'] = $itemsData;
}
if ($applyShippingRules) {
$parcelData['apply_shipping_rules'] = true;
}
if ($shippingMethodCheckoutName) {
$parcelData['shipping_method_checkout_name'] = $shippingMethodCheckoutName;
}
// Additional fields are only added when requesting a label
if ($requestLabel) {
$parcelData['request_label'] = true;
// Sender address
if ($senderAddress instanceof SenderAddress) {
$parcelData['sender_address'] = $senderAddress->getId();
} elseif ($senderAddress instanceof Address) {
// API will assert that house number is passed separately. See
// https://github.com/Webador/sendcloud/issues/25.
if (!$senderAddress->getHouseNumber()) {
throw new \InvalidArgumentException(
'House number must be passed separately on Address instance passed as sender address.'
);
}
$parcelData = array_merge($parcelData, [
'from_name' => $senderAddress->getName(),
'from_company_name' => $senderAddress->getCompanyName() ?? '',
'from_address_1' => $senderAddress->getAddressLine1(),
'from_address_2' => $senderAddress->getAddressLine2() ?? '',
'from_house_number' => $senderAddress->getHouseNumber() ?? '',
'from_city' => $senderAddress->getCity(),
'from_postal_code' => $senderAddress->getPostalCode(),
'from_country' => $senderAddress->getCountryCode(),
'from_telephone' => $senderAddress->getPhoneNumber() ?? '',
'from_email' => $senderAddress->getEmailAddress(),
]);
} elseif (is_int($senderAddress)) {
$parcelData['sender_address'] = $senderAddress;
}
// Shipping method
if ($shippingMethod instanceof ShippingMethod) {
$parcelData['shipment'] = [
'id' => $shippingMethod->getId(),
];
} elseif (is_int($shippingMethod)) {
$parcelData['shipment'] = [
'id' => $shippingMethod,
];
} else {
throw new \InvalidArgumentException(
'Shipping method must be passed when requesting a label.'
);
}
}
return $parcelData;
}
}