PluginProbe
MBE eShip / trunk
MBE eShip vtrunk
2.8.1 trunk 1.0.0 1.1.0 1.1.3 1.2.1 1.2.2 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.7.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.2.1 All 37 releases
mail-boxes-etc / lib / Mbe / MbeWs.php

MbeWs.php in MBE eShip trunk, at lib/Mbe/MbeWs.php

1,708 lines 62.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MbeExceptions\HttpRequestException as HttpRequestException;
4 use MbeExceptions\ShippingDocumentException;
5
6 class MbeWs {
7 private $_log;
8 protected $helper;
9 protected $pluginVersionMessage;
10 protected $wsUrl;
11 protected $apiToken;
12 protected $apiCustomer;
13
14 private const EXPECTED_STATUS = "OK";
15
16 public function __construct( $log = false ) {
17 $this->_log = $log;
18 $this->helper = new Mbe_Shipping_Helper_Data();
19 $this->pluginVersionMessage = MBE_ESHIP_PLUGIN_NAME . ' version ' . MBE_ESHIP_PLUGIN_VERSION . ' :';
20 $this->wsUrl = $this->helper->getWsUrl();
21 $this->apiToken = null;
22 $this->apiCustomer = null;
23 }
24
25 private function log( $message ) {
26 if ( $this->_log ) {
27 $row = date_format( new DateTime(), 'Y-m-d\TH:i:s\Z' );
28 $row .= " - ";
29 $row .= $this->pluginVersionMessage . $message . "\n\r";
30 file_put_contents( $this->helper->getLogWsPath(), $row, FILE_APPEND );
31 }
32 }
33
34 public function logVar( $var, $message = null ) {
35 if ( $this->_log ) {
36 if ( $message ) {
37 $this->log( $message );
38 }
39 $this->log( print_r( $var, true ) );
40 }
41 }
42
43 public function logVarNoCredentials( $var, $message = null ) {
44 $logArgs = json_decode( json_encode( $var ), true );
45 $logArgs['RequestContainer']['Credentials'] = null;
46
47 if ( $this->_log ) {
48 if ( $message ) {
49 $this->log( $message );
50 }
51 $this->log( print_r( $logArgs, true ) );
52 }
53 }
54
55
56 function generateRandomString( $length = 10 ) {
57 $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
58 $charactersLength = strlen( $characters );
59 $randomString = '';
60 for ( $i = 0; $i < $length; $i ++ ) {
61 $randomString .= $characters[ rand( 0, $charactersLength - 1 ) ];
62 }
63
64 return $randomString;
65 }
66
67 public function getCustomer( $ws, $username, $password, $system ) {
68 $this->log( 'GET CUSTOMER' );
69 $result = false;
70
71 try {
72 $soapClient = new MbeSoapClient( $ws, array(
73 'encoding' => 'utf-8',
74 'trace' => 1
75 ), $username, $password, false );
76 $internalReferenceID = $this->generateRandomString();
77
78 //WS ARGS
79 $args = new stdClass;
80 $args->RequestContainer = new stdClass;
81
82 $args->RequestContainer->Action = "GET";
83
84 $args->RequestContainer->SystemType = $system;
85
86 $args->RequestContainer->Customer = new stdClass;
87 $args->RequestContainer->Customer->Login = "";
88
89 $args->RequestContainer->Credentials = new stdClass;
90 $args->RequestContainer->Credentials->Username = $username;
91 $args->RequestContainer->Credentials->Passphrase = $password;
92
93 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
94
95 $args->RequestContainer->Action = "GET";
96
97 $this->logVarNoCredentials( $args, 'GET CUSTOMER ARGS' );
98 $soapResult = $soapClient->__soapCall( "ManageCustomerRequest", array( $args ) );
99
100 $lastResponse = $soapClient->__getLastResponse();
101 $this->logVar( $lastResponse, 'GET CUSTOMER RESPONSE' );
102
103 if ( isset( $soapResult->RequestContainer->Errors ) ) {
104 $this->logVar( $soapResult->RequestContainer->Errors, 'GET CUSTOMER ERRORS' );
105 }
106
107 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
108 //if (isset($soapResult->RequestContainer->InternalReferenceID) && $soapResult->RequestContainer->InternalReferenceID == $internalReferenceID) {
109 $result = $soapResult->RequestContainer->Customer;
110 //}
111 }
112 } catch ( Exception $e ) {
113 $this->log( 'GET CUSTOMER EXCEPTION' );
114 $this->log( $e->getMessage() );
115 }
116 $this->logVar( $result, 'GET CUSTOMER RESULT' );
117
118 return $result;
119 }
120
121 public function estimateShipping(
122 $ws, $username, $password, $shipmentType, $system, $country, $region, $city, $postCode, $items, $products, $insurance = false, $insuranceValue = 0.00, $insuranceCode = null
123 ) {
124 $messageTitle = 'ESTIMATE SHIPPING';
125 $this->log( $messageTitle );
126 $result = false;
127
128 try {
129 $soapClient = new MbeSoapClient( $ws, array(
130 'encoding' => 'utf-8',
131 'trace' => 1
132 ), $username, $password );
133 $internalReferenceID = $this->generateRandomString();
134
135 //WS ARGS
136 $args = $this->setBaseClass( $system, $username, $password );
137
138 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
139
140
141 $args->RequestContainer->ShippingParameters = new stdClass;
142
143 $args->RequestContainer->ShippingParameters->DestinationInfo = new stdClass;
144
145 $args->RequestContainer->ShippingParameters->DestinationInfo->ZipCode = $postCode;
146 $args->RequestContainer->ShippingParameters->DestinationInfo->City = $city;
147 $args->RequestContainer->ShippingParameters->DestinationInfo->State = $region;
148 $args->RequestContainer->ShippingParameters->DestinationInfo->Country = $country;
149 //$args->RequestContainer->ShippingParameters->DestinationInfo->idSubzone = "";
150
151 $args->RequestContainer->ShippingParameters->ShipType = "EXPORT";
152
153 $args->RequestContainer->ShippingParameters->PackageType = $shipmentType;
154
155 $args->RequestContainer->ShippingParameters->Items = $items;
156 // $itemsa = new stdClass();
157 // $itemsa->Item = $items[0];
158 // $args->RequestContainer->ShippingParameters->Items = $itemsa;
159
160 $this->insuranceParameters( $insurance, $insuranceCode, $insuranceValue, $args->RequestContainer->ShippingParameters );
161
162 // if ($insurance) {
163 // switch ($insuranceCode) {
164 // case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_INSURANCE_CODE_SUFFIX:
165 // $args->RequestContainer->ShippingParameters->Insurance = $insurance;
166 // $args->RequestContainer->ShippingParameters->InsuranceValue = $insuranceValue;
167 // break;
168 // case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_CODE_SUFFIX:
169 // $args->RequestContainer->ShippingParameters->MBESafeValue = $insurance;
170 // $args->RequestContainer->ShippingParameters->MBESafeValueValue = $insuranceValue;
171 // break;
172 // case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_ART_CODE_SUFFIX:
173 // $args->RequestContainer->ShippingParameters->MBESafeValue = $insurance;
174 // $args->RequestContainer->ShippingParameters->MBESafeValueValue = $insuranceValue;
175 // $args->RequestContainer->ShippingParameters->GoodType = 'ART';
176 // break;
177 // case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_4B_CODE_SUFFIX:
178 // $args->RequestContainer->ShippingParameters->MBESafeValue4Business = $insurance;
179 // $args->RequestContainer->ShippingParameters->MBESafeValue4BusinessValue = $insuranceValue;
180 // break;
181 // default:
182 // //
183 // break;
184 // }
185 // }
186
187 if ( $this->helper->getPickupRequestEnabled()
188 && $this->helper->getShipmentsCreationMode() === Mbe_Shipping_Helper_Data::MBE_CREATION_MODE_AUTOMATICALLY
189 && $this->helper->getPickupRequestMode() === Mbe_Shipping_Helper_Data::MBE_PICKUP_REQUEST_AUTOMATIC
190 ) {
191 $defaultPickupAddress = $this->getDefaultPickupAddress( $ws, $username, $password, $system );
192
193 if ( empty( $defaultPickupAddress ) ) {
194 throw new Exception( 'Default pickup address is missing' );
195 }
196
197 $args->RequestContainer->ShippingParameters->SenderInfo = new stdClass();
198 $args->RequestContainer->ShippingParameters->SenderInfo->ZipCode = $defaultPickupAddress[0]['ZipCode'];
199 $args->RequestContainer->ShippingParameters->SenderInfo->City = $defaultPickupAddress[0]['City'];
200 $args->RequestContainer->ShippingParameters->SenderInfo->State = $defaultPickupAddress[0]['Province'];
201 $args->RequestContainer->ShippingParameters->SenderInfo->Country = $defaultPickupAddress[0]['Country'];
202 }
203
204 if ( $this->helper->isEnabledTaxAndDuties() ) {
205 $args->RequestContainer->ShippingParameters->LanguageCode = $this->helper->getCountry();
206 $args->RequestContainer->ShippingParameters->TaxAndDutyPluginAct = true;
207 $args->RequestContainer->ShippingParameters->ProformaIncoterms = $this->helper->getTaxAndDutiesModeName();
208 $args->RequestContainer->ShippingParameters->ProformaInvoice = $this->generateProforma( $products );
209 }
210
211 $soapResult = $soapClient->__soapCall( "ShippingOptionsRequest", array( $args ) );
212
213 $logArgs = json_decode( json_encode( $args ) );
214 $logArgs->RequestContainer->Credentials = null;
215
216 $this->logVar( $logArgs, $messageTitle . ' ARGS' );
217 $this->logVar( $soapClient->__getLastRequest(), $messageTitle . ' XML REQUEST' );
218
219 $lastResponse = $soapClient->__getLastResponse();
220 $this->logVar( $lastResponse, $messageTitle . ' XML RESPONSE' );
221
222 if ( isset( $soapResult->RequestContainer->Errors ) ) {
223 $this->logVar( $soapResult->RequestContainer->Errors, $messageTitle . ' ERRORS' );
224 }
225
226 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
227 if ( isset( $soapResult->RequestContainer->InternalReferenceID ) && $soapResult->RequestContainer->InternalReferenceID == $internalReferenceID ) {
228 if ( isset( $soapResult->RequestContainer->ShippingOptions->ShippingOption ) ) {
229 if ( is_array( $soapResult->RequestContainer->ShippingOptions->ShippingOption ) ) {
230 $result = $soapResult->RequestContainer->ShippingOptions->ShippingOption;
231 } else {
232 $result = array( $soapResult->RequestContainer->ShippingOptions->ShippingOption );
233 }
234 }
235 }
236 }
237
238 } catch ( Exception $e ) {
239 $this->log( $messageTitle . ' EXCEPTION' );
240 $this->log( $e->getMessage() );
241 }
242 $this->logVar( $result, $messageTitle . ' RESULT' );
243
244 return $result;
245 }
246
247 /**
248 * @throws \MbeExceptions\ApiRequestException
249 */
250 public function createShipping(
251 $ws, $username, $password, $shipmentType, $service, $subZone, $system,
252 $notes, $firstName, $lastName, $companyName, $address, $phone, $city,
253 $state, $country, $postCode, $email, $items, $products, $shipperType = 'MBE',
254 $goodsValue = 0.0, $reference = "", $isCod = false, $codValue = 0.0,
255 $insurance = false, $insuranceValue = 0.0, $insuranceCode = null
256 ) {
257 $messageTitle = 'CREATE SHIPPING';
258 // $this->log($messageTitle);
259 // $this->logVar(func_get_args(), $messageTitle);
260
261 $result = false;
262
263
264 try {
265 $soapClient = new MbeSoapClient( $ws, array(
266 'encoding' => 'utf-8',
267 'trace' => 1
268 ), $username, $password );
269 $internalReferenceID = $this->generateRandomString();
270
271 //WS ARGS
272 $args = $this->setShipmentContainer( $system, $username, $password, $internalReferenceID, $firstName, $lastName, $companyName, $address, $phone, $postCode, $city, $state, $country, $email, $subZone, $shipperType, $isCod, $codValue, $insurance, $insuranceValue, $service, $shipmentType, $reference, $items, $products, $goodsValue, $notes, $insuranceCode );
273
274 $this->logVarNoCredentials( $args, $messageTitle . ' ARGS' );
275
276 $soapResult = $soapClient->__soapCall( "ShipmentRequest", array( $args ) );
277
278 $lastResponse = $soapClient->__getLastResponse();
279 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
280
281 if ( isset( $soapResult->RequestContainer->Errors ) ) {
282 $this->logVar( $soapResult->RequestContainer->Errors, $messageTitle . ' ERRORS' );
283 }
284 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
285 if ( isset( $soapResult->RequestContainer->InternalReferenceID ) && $soapResult->RequestContainer->InternalReferenceID == $internalReferenceID ) {
286 $result = $soapResult->RequestContainer;
287 }
288 }
289
290 } catch ( Exception $e ) {
291 $this->log( $messageTitle . ' EXCEPTION' );
292 $this->log( $e->getMessage() );
293 }
294 $this->logVar( $result, $messageTitle . ' RESULT' );
295
296 return $result;
297 }
298
299 public function closeShipping( $ws, $username, $password, $system, $trackings ) {
300 $this->log( 'CLOSE SHIPPING' );
301
302
303 $result = false;
304
305
306 try {
307 $soapClient = new MbeSoapClient( $ws, array(
308 'encoding' => 'utf-8',
309 'trace' => 1
310 ), $username, $password );
311 $internalReferenceID = $this->generateRandomString();
312
313 //WS ARGS
314 $args = new stdClass;
315 $args->RequestContainer = new stdClass;
316 $args->RequestContainer->SystemType = $system;
317
318 $args->RequestContainer->Credentials = new stdClass;
319 $args->RequestContainer->Credentials->Username = $username;
320 $args->RequestContainer->Credentials->Passphrase = $password;
321
322 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
323
324 $masterTrackingsMBE = array();
325 foreach ( $trackings as $track ) {
326 array_push( $masterTrackingsMBE, $track );
327 }
328
329
330 $args->RequestContainer->MasterTrackingsMBE = $masterTrackingsMBE;
331
332 $this->logVarNoCredentials( $args, 'CLOSE SHIPPING ARGS' );
333
334 $soapResult = $soapClient->__soapCall( "CloseShipmentsRequest", array( $args ) );
335
336 $lastResponse = $soapClient->__getLastResponse();
337
338 $this->logVar( $lastResponse, 'CLOSE SHIPPING RESPONSE' );
339
340 if ( isset( $soapResult->RequestContainer->Errors ) ) {
341 $this->logVar( $soapResult->RequestContainer->Errors, 'CLOSE SHIPPING ERRORS' );
342 }
343
344 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
345 $result = $soapResult->RequestContainer;
346 }
347
348 } catch ( Exception $e ) {
349 $this->log( 'CLOSE SHIPPING EXCEPTION' );
350 $this->log( $e->getMessage() );
351 }
352 $this->logVar( $result, 'CLOSE SHIPPING RESULT' );
353
354 return $result;
355 }
356
357 public function returnShipping( $ws, $username, $password, $system, $tracking ) {
358 $this->log( 'RETURN SHIPPING - ' . $tracking );
359
360 $result = false;
361
362 try {
363 $soapClient = new MbeSoapClient( $ws, array(
364 'encoding' => 'utf-8',
365 'trace' => 1
366 ), $username, $password, false );
367 $internalReferenceID = 'RETURN-SHIPPING-' . $tracking;
368
369 //WS ARGS
370 $args = $this->setBaseClass( $system, $username, $password );
371
372 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
373
374 $args->RequestContainer->MbeTracking = $tracking;
375 // $args->RequestContainer->CustomerAsReceiver = true;
376 $args->RequestContainer->ShipmentOrigin = MBE_ESHIP_PLUGIN_NAME . " WooCommerce " . MBE_ESHIP_PLUGIN_VERSION;
377 $args->RequestContainer->Referring = '';
378
379 $this->logVarNoCredentials( $args, 'RETURN SHIPPING ARGS' );
380
381 $soapResult = $soapClient->__soapCall( "ShipmentReturnRequest", array( $args ) );
382
383 $lastResponse = $soapClient->__getLastResponse();
384
385 $this->logVar( $lastResponse, 'RETURN SHIPPING RESPONSE' );
386
387 if ( isset( $soapResult->RequestContainer->Errors ) ) {
388 $this->logVar( $soapResult->RequestContainer->Errors, 'RETURN SHIPPING ERRORS' );
389 }
390
391 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
392 $result = $soapResult->RequestContainer;
393 }
394 } catch ( Exception $e ) {
395 $this->log( 'RETURN SHIPPING EXCEPTION' );
396 $this->log( $e->getMessage() );
397 }
398 $this->logVar( $result, 'RETURN SHIPPING RESULT' );
399
400 return $result;
401 }
402
403 /**
404 * @throws SoapFault
405 * @throws \MbeExceptions\ValidationException
406 * @throws \MbeExceptions\ApiRequestException
407 */
408 public function advancedReturnShipping( $orderId, $wsUrl, $wsUsername, $wsPassword, $system, $shipperType, $returnData ) {
409 $tracking = $this->helper->getTrackings( $orderId )[0];
410 $order = wc_get_order( $orderId );
411
412 if ( $order ) {
413 $this->log( 'ADVANCED RETURN SHIPPING - ' . $tracking );
414
415 $result = false;
416 try {
417 if ( $this->helper->canAdvancedReturn() ) {
418 $shipmentInfo = $this->getShipmentItems( $wsUrl, $wsUsername, $wsPassword, $system, $tracking );
419 $shipmentInfo = $shipmentInfo->ShipmentsFullInfo->ShipmentFullInfo->ShipmentInfo;
420
421 $service = 'SSE';
422 $subzone = '';
423 $system = $this->helper->getCountry();
424 $notes = '';
425 $firstName = $returnData['receiver_reference'];
426 $lastName = '';
427 $companyName = empty( $returnData['receiver_trade_name'] ) ? $returnData['receiver_reference'] : $returnData['receiver_trade_name'];
428 $address = $returnData['receiver_address_1'] . ', ' . $returnData['receiver_address_2'] . ', ' . $returnData['receiver_address_3'];
429 $phone = $returnData['receiver_telephone_1'] ?? $returnData['receiver_telephone_2'];
430 $city = $returnData['receiver_city'];
431 $region = $returnData['receiver_province'];
432 $country = $returnData['receiver_country'];
433 $postCode = $returnData['receiver_postcode'];
434 $email = $returnData['receiver_email_1'] ?? $returnData['receiver_email_2'];
435 $items = $shipmentInfo->Items;
436 $products = $shipmentInfo->Products;
437 $goodsValue = 0.0;
438 $shipmentType = $this->helper->getDefaultShipmentType();
439 $reference = $orderId;
440 $isCod = null;
441 $codValue = null;
442 $insurance = null;
443 $insuranceValue = null;
444 $insuranceCode = null;
445 $senderInfo = [
446 'company-name' => empty( $returnData['sender_trade_name'] ) ? $returnData['sender_reference'] : $returnData['sender_trade_name'],
447 'address' => $returnData['sender_address_1'],
448 'address2' => $returnData['sender_address_2'],
449 'address3' => $returnData['sender_address_3'],
450 'zipcode' => $returnData['sender_postcode'],
451 'city' => $returnData['sender_city'],
452 'state' => $returnData['sender_province'],
453 'country' => $returnData['sender_country'],
454 'name' => $returnData['sender_reference'],
455 'phone' => $returnData['sender_telephone_1'],
456 'email' => $returnData['sender_email_1'],
457 ];
458 $pickupData = [
459 'pickup_batch_id' => '',
460 'pickup_address_id' => '',
461 'date' => $returnData['pickup_date'],
462 'preferred_from' => $returnData['pickup_time_preferred_from'],
463 'preferred_to' => $returnData['pickup_time_preferred_to'],
464 'alternative_from' => $returnData['pickup_time_alternative_from'],
465 'alternative_to' => $returnData['pickup_time_alternative_to'],
466 'notes' => $returnData['pickup_notes'],
467 ];
468
469 //Send $tracking data to be used as IsEcommerceReturnOriginalTracking to identify the Pickup as an advanced return
470 $result = $this->createReturnPickupShipping(
471 $wsUrl, $wsUsername, $wsPassword, $shipmentType, $service, $subzone, $system, $notes, $firstName, $lastName, $companyName,
472 $address, $phone, $city, $region, $country, $postCode, $email, $items, $products, $shipperType, $goodsValue, $reference,
473 $isCod, $codValue, $insurance, $insuranceValue, $senderInfo, $pickupData, $insuranceCode, $tracking
474 );
475
476 $this->logVar( $result, 'RETURN SHIPPING RESPONSE' );
477
478 if ( isset( $result->Errors ) ) {
479 $this->logVar( $result->Errors, 'RETURN SHIPPING ERRORS' );
480 }
481
482 } else {
483 $message = 'User is not enabled for advanced return';
484 $this->log( $message );
485 throw new \MbeExceptions\ValidationException( __( $message, 'mail-boxes-etc' ) );
486 }
487 } catch ( Exception $e ) {
488 $this->log( 'RETURN SHIPPING EXCEPTION' );
489 $this->log( $e->getMessage() );
490 throw $e;
491 }
492
493 $this->logVar( $result, 'RETURN SHIPPING RESULT' );
494
495 return $result;
496 } else {
497 $message = 'ADVANCED RETURN SHIPPING - '. __('Order not selected or not found');
498 $this->log( $message );
499 throw new \MbeExceptions\ValidationException( __( $message, 'mail-boxes-etc' ) );
500 }
501 }
502
503 public function getShipmentItems( $ws, $username, $password, $system, $tracking ) {
504 $message = 'GET SHIPPING ITEMS - ';
505
506 $this->log( $message . $tracking );
507
508 $result = false;
509
510 try {
511 $soapClient = new MbeSoapClient( $ws, array(
512 'encoding' => 'utf-8',
513 'trace' => 1
514 ), $username, $password, false );
515 $internalReferenceID = 'GET-SHIPPING-ITEMS-' . $tracking;
516
517 //WS ARGS
518 $args = $this->setBaseClass( $system, $username, $password );
519
520 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
521 $args->RequestContainer->MBEMasterTrackings = $tracking;
522
523 $this->logVarNoCredentials( $args, 'GET SHIPPING ITEMS' );
524
525 $soapResult = $soapClient->__soapCall( "ShipmentsListV3Request", array( $args ) );
526
527 $lastResponse = $soapClient->__getLastResponse();
528
529 $this->logVar( $lastResponse, $message . 'RESPONSE' );
530
531 $this->checkResponseErrors( $soapResult, $message );
532
533 if ( $this->isResponseValid( $soapResult, $internalReferenceID ) ) {
534 $result = $soapResult->RequestContainer;
535 }
536
537 } catch ( \MbeExceptions\ApiRequestException $e ) {
538 $this->log( $message . ' EXCEPTION' );
539 $this->log( $e->getMessage() );
540 throw $e;
541 } catch ( Exception $e ) {
542 $this->log( $message . ' EXCEPTION' );
543 $this->log( $e->getMessage() );
544 throw $e;
545 }
546 $this->logVar( $result, $message . ' RESULT' );
547
548 return $result;
549 }
550
551
552 /**
553 * @throws Exception | ShippingDocumentException
554 */
555 public function getShippingDocument( $ws, $username, $password, $system, $tracking ) {
556 $messageTitle = 'GET SHIPPING DOCUMENT';
557 $result = false;
558
559 try {
560 $soapClient = new MbeSoapClient( $ws, array(
561 'encoding' => 'utf-8',
562 'trace' => 1
563 ), $username, $password );
564 $internalReferenceID = $this->generateRandomString();
565
566 //WS ARGS
567 $args = $this->setBaseClass( $system, $username, $password );
568 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
569
570 $args->RequestContainer->TrackingMBE = $tracking;
571 $args->RequestContainer->CourierWaybill = true;
572
573 $this->logVarNoCredentials( $args, $messageTitle . ' ARGS' );
574
575 $soapResult = $soapClient->__soapCall( "shipmentDocumentsRequest", array( $args ) );
576
577 $lastResponse = $soapClient->__getLastResponse();
578 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
579
580 $this->checkResponseErrors( $soapResult, $messageTitle );
581
582 if ( $this->isResponseValid( $soapResult, $internalReferenceID )
583 && ! empty( $soapResult->RequestContainer->ShipmentDocuments->ShipmentDocument->CourierWaybill ?? null )
584 ) {
585 $this->logVar( $result, $messageTitle . ' RESULTS' );
586 } else {
587 $this->logVar( $soapResult->RequestContainer, $messageTitle . ' ERROR' );
588 // throw new ShippingDocumentException($soapResult->RequestContainer->ShipmentDocuments->ShipmentDocument->Errors->Error->Description);
589 }
590 $result = $soapResult->RequestContainer->ShipmentDocuments->ShipmentDocument;
591
592 } catch ( Exception $e ) {
593 $this->log( $messageTitle . 'EXCEPTION' );
594 $this->log( $e->getMessage() );
595 if ( ShippingDocumentException::class === get_class( $e ) ) {
596 throw new ShippingDocumentException( esc_html( $e->getMessage() ) );
597 } else {
598 throw $e;
599 }
600 }
601
602 return $result;
603 }
604
605 // PICKUP REQUEST
606
607 /**
608 * @throws \MbeExceptions\ApiRequestException
609 */
610 public function createPickupShipping(
611 $ws, $username, $password, $shipmentType, $service, $subZone, $system,
612 $notes, $firstName, $lastName, $companyName, $address, $phone, $city,
613 $state, $country, $postCode, $email, $items, $products, $shipperType = 'MBE',
614 $goodsValue = 0.0, $reference = "", $isCod = false, $codValue = 0.0,
615 $insurance = false, $insuranceValue = 0.0,
616 $senderInfo = [],
617 $pickupData = [],
618 $insuranceCode = null
619 ) {
620 $messageTitle = 'CREATE PICKUP SHIPPING';
621
622 $result = false;
623
624
625 try {
626 $soapClient = new MbeSoapClient( $ws, array(
627 'encoding' => 'utf-8',
628 'trace' => 1
629 ), $username, $password );
630 $internalReferenceID = $this->generateRandomString();
631 $args = $this->setShipmentContainer( $system, $username, $password, $internalReferenceID, $firstName, $lastName, $companyName, $address, $phone, $postCode, $city, $state, $country, $email, $subZone, $shipperType, $isCod, $codValue, $insurance, $insuranceValue, $service, $shipmentType, $reference, $items, $products, $goodsValue, $notes, $insuranceCode );
632
633 if ( $this->helper->getPickupRequestEnabled() ) {
634 switch ( $this->helper->getPickupRequestMode() ) {
635 case Mbe_Shipping_Helper_Data::MBE_PICKUP_REQUEST_AUTOMATIC:
636 $args->RequestContainer->Pickup = new stdClass();
637 $args->RequestContainer->Pickup->MolDefaultPickupTime = true;
638 break;
639 case Mbe_Shipping_Helper_Data::MBE_PICKUP_REQUEST_MANUAL:
640
641 if ( ! empty( $pickupData['pickup_batch_id'] ) ) { // If it's a pickup batch
642 $args->RequestContainer->Pickup = new stdClass();
643 $args->RequestContainer->Pickup->MolDefaultPickupTime = false;
644 $args->RequestContainer->Pickup->PickupBatchId = $pickupData['pickup_batch_id'];
645 $args->RequestContainer->Pickup->PickupAddressId = $pickupData['pickup_address_id'];
646
647 } else { // If it's a single pickup
648 if ( empty( $senderInfo ) ) {
649 $message = 'Pickup request cannot be sent, sender information are missing';
650 throw new Exception( __( $message, 'mail-boxes-etc' ) );
651 }
652 if ( empty( $pickupData ) ) {
653 $message = 'Pickup request cannot be sent, pickup data are missing';
654 throw new Exception( __( $message, 'mail-boxes-etc' ) );
655 }
656 $args->RequestContainer->Sender = new stdClass();
657 $args->RequestContainer->Sender->Name = $senderInfo['company-name'];
658 $args->RequestContainer->Sender->CompanyName = $senderInfo['company-name'];
659 $args->RequestContainer->Sender->Address = $senderInfo['address'];
660 $args->RequestContainer->Sender->Phone = $senderInfo['phone'];
661 $args->RequestContainer->Sender->ZipCode = $senderInfo['zipcode'];
662 $args->RequestContainer->Sender->City = $senderInfo['city'];
663 $args->RequestContainer->Sender->State = $senderInfo['state'];
664 $args->RequestContainer->Sender->Country = $senderInfo['country'];
665 $args->RequestContainer->Sender->Email = $senderInfo['email'];
666
667 $args->RequestContainer->Pickup = new stdClass();
668 $args->RequestContainer->Pickup->PickupData = new stdClass();
669 $args->RequestContainer->Pickup->PickupData->Notes = $pickupData['notes'] ?? '';
670 $args->RequestContainer->Pickup->PickupData->Date = $pickupData['date'];
671 $args->RequestContainer->Pickup->PickupData->PreferredFrom = $pickupData['preferred_from'];
672 $args->RequestContainer->Pickup->PickupData->PreferredTo = $pickupData['preferred_to'];
673 $args->RequestContainer->Pickup->PickupData->AlternativeFrom = $pickupData['alternative_from'] ?? '';
674 $args->RequestContainer->Pickup->PickupData->AlternativeTo = $pickupData['alternative_to'] ?? '';
675 }
676 break;
677 default:
678 # code...
679 break;
680 }
681
682 } else {
683 $message = 'User is not enabled for third party pickup';
684 $this->log( $message );
685 throw new Exception( __( $message, 'mail-boxes-etc' ) );
686 }
687
688 $this->logVarNoCredentials( $args, $messageTitle . ' ARGS' );
689
690 $soapResult = $soapClient->__soapCall( "ShipmentRequest", array( $args ) );
691
692 $lastResponse = $soapClient->__getLastResponse();
693 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
694
695 $this->checkResponseErrors( $soapResult, $messageTitle );
696
697 if ( $this->isResponseValid( $soapResult, $internalReferenceID ) ) {
698 $result = $soapResult->RequestContainer;
699 }
700
701 } catch ( \MbeExceptions\ApiRequestException $e ) {
702 $this->log( $messageTitle . ' EXCEPTION' );
703 $this->log( $e->getMessage() );
704 throw $e;
705 } catch ( Exception $e ) {
706 $this->log( $messageTitle . ' EXCEPTION' );
707 $this->log( $e->getMessage() );
708 }
709 $this->logVar( $result, $messageTitle . ' RESULT' );
710
711 return $result;
712 }
713
714 // PICKUP REQUEST
715
716 /**
717 * @throws \MbeExceptions\ApiRequestException
718 */
719 public function createReturnPickupShipping(
720 $ws, $username, $password, $shipmentType, $service, $subZone, $system,
721 $notes, $firstName, $lastName, $companyName, $address, $phone, $city,
722 $state, $country, $postCode, $email, $items, $products, $shipperType = 'MBE',
723 $goodsValue = 0.0, $reference = "", $isCod = false, $codValue = 0.0,
724 $insurance = false, $insuranceValue = 0.0,
725 $senderInfo = [],
726 $pickupData = [],
727 $insuranceCode = null,
728 $advancedReturnTracking = ''
729 ) {
730 $messageTitle = 'CREATE ADVANCE RETURN PICKUP SHIPPING';
731
732 $result = false;
733
734
735 try {
736 $soapClient = new MbeSoapClient( $ws, array(
737 'encoding' => 'utf-8',
738 'trace' => 1
739 ), $username, $password );
740 $internalReferenceID = 'ADVANCED-RETURN-SHIPPING-' . $advancedReturnTracking;
741 $args = $this->setShipmentContainer( $system, $username, $password, $internalReferenceID, $firstName, $lastName, $companyName, $address, $phone, $postCode, $city, $state, $country, $email, $subZone, $shipperType, $isCod, $codValue, $insurance, $insuranceValue, $service, $shipmentType, $reference, $items, $products, $goodsValue, $notes, $insuranceCode );
742
743 // Add information for advance return
744 $args->RequestContainer->Shipment->IsEcommerceReturn = true;
745 $args->RequestContainer->Shipment->IsEcommerceReturnOriginalTracking = $advancedReturnTracking;
746 $args->RequestContainer->Shipment->Referring = 'Return: ' . $reference . ' - ' . $advancedReturnTracking;
747
748 if ( empty( $senderInfo ) ) {
749 $message = 'Pickup request cannot be sent, sender information are missing';
750 throw new Exception( __( $message, 'mail-boxes-etc' ) );
751 }
752 if ( empty( $pickupData ) ) {
753 $message = 'Pickup request cannot be sent, pickup data are missing';
754 throw new Exception( __( $message, 'mail-boxes-etc' ) );
755 }
756
757 $args->RequestContainer->Sender = new stdClass();
758 $args->RequestContainer->Sender->Name = mb_substr($senderInfo['company-name']?? '',0,100,'UTF-8');
759 $args->RequestContainer->Sender->CompanyName = mb_substr($senderInfo['company-name']?? '',0,100,'UTF-8');
760 $args->RequestContainer->Sender->Address = mb_substr($senderInfo['address']?? '',0,200,'UTF-8');
761 $args->RequestContainer->Sender->Address2 = mb_substr($senderInfo['address2']?? '',0,100,'UTF-8');
762 $args->RequestContainer->Sender->Address3 = mb_substr($senderInfo['address3']?? '',0,100,'UTF-8');
763 $args->RequestContainer->Sender->Phone = mb_substr($senderInfo['phone']?? '',0,50,'UTF-8');
764 $args->RequestContainer->Sender->ZipCode = mb_substr($senderInfo['zipcode']?? '',0,12,'UTF-8');
765 $args->RequestContainer->Sender->City = mb_substr($senderInfo['city']?? '',0,100,'UTF-8');
766 $args->RequestContainer->Sender->State = mb_substr($senderInfo['state']?? '',0,2,'UTF-8');
767 $args->RequestContainer->Sender->Country = mb_substr($senderInfo['country']?? '',0,2,'UTF-8');
768 $args->RequestContainer->Sender->Email = mb_substr($senderInfo['email']?? '',0,75,'UTF-8');
769
770 $args->RequestContainer->Pickup = new stdClass();
771 $args->RequestContainer->Pickup->PickupData = new stdClass();
772 $args->RequestContainer->Pickup->PickupData->Notes = $pickupData['notes'] ?? '';
773 $args->RequestContainer->Pickup->PickupData->Date = $pickupData['date'];
774 $args->RequestContainer->Pickup->PickupData->PreferredFrom = $pickupData['preferred_from'];
775 $args->RequestContainer->Pickup->PickupData->PreferredTo = $pickupData['preferred_to'];
776 $args->RequestContainer->Pickup->PickupData->AlternativeFrom = $pickupData['alternative_from'] ?? '';
777 $args->RequestContainer->Pickup->PickupData->AlternativeTo = $pickupData['alternative_to'] ?? '';
778
779 $this->logVarNoCredentials( $args, $messageTitle . ' ARGS' );
780
781 $soapResult = $soapClient->__soapCall( "ShipmentRequest", array( $args ) );
782
783 $lastResponse = $soapClient->__getLastResponse();
784 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
785
786 $this->checkResponseErrors( $soapResult, $messageTitle );
787
788 if ( $this->isResponseValid( $soapResult, $internalReferenceID ) ) {
789 $result = $soapResult->RequestContainer;
790 }
791
792 } catch ( \MbeExceptions\ApiRequestException $e ) {
793 $this->log( $messageTitle . ' EXCEPTION' );
794 $this->log( $e->getMessage() );
795 throw $e;
796 } catch ( Exception $e ) {
797 $this->log( $messageTitle . ' EXCEPTION' );
798 $this->log( $e->getMessage() );
799 }
800 $this->logVar( $result, $messageTitle . ' RESULT' );
801
802 return $result;
803 }
804
805
806 /**
807 * @throws \MbeExceptions\ApiRequestException
808 */
809 public function closePickupShippping(
810 $ws, $username, $password, $system, $pickupBatchID, $pickupData
811 ) {
812 $messageTitle = 'CLOSE PICKUP SHIPPING ';
813 $this->log( $messageTitle );
814 $internalReferenceID = $this->generateRandomString();
815
816 $result = false;
817
818 try {
819 $soapClient = new MbeSoapClient( $ws, array(
820 'encoding' => 'utf-8',
821 'trace' => 1
822 ), $username, $password, false );
823
824 $args = new stdClass;
825 $args->RequestContainer = new stdClass;
826 $args->RequestContainer->System = $system;
827 $args->RequestContainer->Credentials = new stdClass();
828 $args->RequestContainer->Credentials->Username = $username;
829 $args->RequestContainer->Credentials->Passphrase = $password;
830 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
831 $args->RequestContainer->PickupBatchId = $pickupBatchID;
832 $args->RequestContainer->PickupData = new stdClass();
833 $args->RequestContainer->PickupData->Notes = $pickupData['notes'] ?? '';
834 $args->RequestContainer->PickupData->Date = $pickupData['date'];
835 $args->RequestContainer->PickupData->PreferredFrom = $pickupData['preferred_from'];
836 $args->RequestContainer->PickupData->PreferredTo = $pickupData['preferred_to'];
837 $args->RequestContainer->PickupData->AlternativeFrom = $pickupData['alternative_from'] ?? '';
838 $args->RequestContainer->PickupData->AlternativeTo = $pickupData['alternative_to'] ?? '';
839
840 $response = $this->sendRequest( $args, "CourierPickupClosureRequest", $messageTitle, $soapClient );
841
842 if ( isset( $soapResult->RequestContainer->Status ) && $soapResult->RequestContainer->Status == "OK" ) {
843 $result = $soapResult->RequestContainer;
844 }
845
846 $this->checkResponseErrors( $soapResult, $messageTitle );
847
848 } catch ( \MbeExceptions\ApiRequestException $e ) {
849 $this->log( $messageTitle . ' EXCEPTION' );
850 $this->log( $e->getMessage() );
851 throw $e;
852 } catch ( Exception $e ) {
853 $this->log( $messageTitle . ' EXCEPTION' );
854 $this->log( $e->getMessage() );
855 }
856
857 $this->logVar( $result, $messageTitle . ' RESULT' );
858
859 return $result;
860 }
861
862 public function getCustomerPickupAddresses( $ws, $username, $password, $system ) {
863 $messageTitle = 'GET CUSTOMER PICKUP ADDRESSES ';
864 $this->log( $messageTitle );
865 $internalReferenceID = $this->generateRandomString();
866
867 $result = false;
868
869 try {
870 $soapClient = new MbeSoapClient( $ws, array(
871 'encoding' => 'utf-8',
872 'trace' => 1
873 ), $username, $password, false );
874
875 $args = new stdClass;
876 $args->RequestContainer = new stdClass;
877 $args->RequestContainer->System = $system;
878 $args->RequestContainer->Credentials = new stdClass();
879 $args->RequestContainer->Credentials->Username = $username;
880 $args->RequestContainer->Credentials->Passphrase = $password;
881
882 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
883 $response = $this->sendRequest( $args, "GetPickupAddressesRequest", $messageTitle, $soapClient );
884
885 $result = [];
886 if ( is_array( $response->PickupAddress ) ) {
887 $result = array_map( function ( $address ) {
888 return get_object_vars( $address->PickupContainer );
889 }, $response->PickupAddress );
890 } else {
891 $result[] = get_object_vars( $response->PickupAddress->PickupContainer );
892 }
893
894 } catch ( Exception $e ) {
895 $this->log( $messageTitle . ' EXCEPTION' );
896 $this->log( $e->getMessage() );
897 }
898
899 $this->logVar( $result, $messageTitle . ' RESULT' );
900
901 return $result;
902 }
903
904 /**
905 * @throws \MbeExceptions\ApiRequestException
906 */
907 public function createPickupAddress( $ws, $username, $password, $system, $addressData, $update = false ) {
908 $messageTitle = ( $update ? 'UPDATE' : 'CREATE' ) . ' PICKUP ADDRESS';
909 $this->log( $messageTitle );
910 $internalReferenceID = $this->generateRandomString();
911
912 $result = false;
913
914 try {
915 $soapClient = new MbeSoapClient( $ws, array(
916 'encoding' => 'utf-8',
917 'trace' => 1
918 ), $username, $password, false );
919
920 $args = new stdClass;
921 $args->RequestContainer = new stdClass;
922 $args->RequestContainer->System = $system;
923 $args->RequestContainer->Credentials = new stdClass;
924 $args->RequestContainer->Credentials->Username = $username;
925 $args->RequestContainer->Credentials->Passphrase = $password;
926
927 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
928
929 $args->RequestContainer->PickupContainer = new stdClass();
930 if ( $update && ! array_key_exists( 'pickup_address_id', $addressData ) ) {
931 throw new \MbeExceptions\ApiRequestException( __( 'ID field is missing. Cannot update the address in MBE Online' ) );
932 }
933
934 if ( $update ) {
935 $args->RequestContainer->PickupContainer->PickupAddressId = $addressData['pickup_address_id'];
936 }
937
938 $args->RequestContainer->PickupContainer->TradeName = $addressData['trade_name'];
939 $args->RequestContainer->PickupContainer->Address1 = $addressData['address_1'];
940 $args->RequestContainer->PickupContainer->Address2 = $addressData['address_2'];
941 $args->RequestContainer->PickupContainer->Address3 = $addressData['address_3'];
942 $args->RequestContainer->PickupContainer->ZipCode = $addressData['zip_code'];
943 $args->RequestContainer->PickupContainer->City = $addressData['city'];;
944 $args->RequestContainer->PickupContainer->Province = $addressData['province'];
945 $args->RequestContainer->PickupContainer->Country = strtoupper( $addressData['country'] );
946 $args->RequestContainer->PickupContainer->Reference = $addressData['reference'];
947 $args->RequestContainer->PickupContainer->Phone1 = $addressData['phone_1'];
948 $args->RequestContainer->PickupContainer->Phone2 = $addressData['phone_2'];
949 $args->RequestContainer->PickupContainer->Email1 = $addressData['email_1'];
950 $args->RequestContainer->PickupContainer->Email2 = $addressData['email_2'];
951 $args->RequestContainer->PickupContainer->Fax = $addressData['fax'];
952 $args->RequestContainer->PickupContainer->RES = $addressData['res'];
953 $args->RequestContainer->PickupContainer->MMR = $addressData['mmr'];
954 $args->RequestContainer->PickupContainer->LTZ = $addressData['ltz'];
955 $args->RequestContainer->PickupContainer->IsDefault = $addressData['is_default'];
956
957 $result = $this->sendRequest( $args, "CreatePickupAddressRequest", $messageTitle, $soapClient );
958
959 } catch ( \MbeExceptions\ApiRequestException $e ) {
960 $this->log( $messageTitle . ' EXCEPTION' );
961 $this->log( $e->getMessage() );
962 throw $e;
963 } catch ( Exception $e ) {
964 $this->log( $messageTitle . ' EXCEPTION' );
965 $this->log( $e->getMessage() );
966 }
967
968 $this->logVar( $result, $messageTitle . ' RESULT' );
969
970 return $result;
971 }
972
973 /**
974 * @throws \MbeExceptions\ApiRequestException
975 */
976 public function updatePickupAddress( $ws, $username, $password, $system, $addressData ) {
977 return $this->createPickupAddress( $ws, $username, $password, $system, $addressData, true );
978 }
979
980 public function deletePickupAddress( $ws, $username, $password, $system, $addressId ) {
981 $messageTitle = 'DELETE PICKUP ADDRESS';
982 $this->log( $messageTitle );
983 $internalReferenceID = $this->generateRandomString();
984
985 $result = false;
986
987 try {
988 $soapClient = new MbeSoapClient( $ws, array(
989 'encoding' => 'utf-8',
990 'trace' => 1
991 ), $username, $password, false );
992
993 $args = $this->setBaseClass( $system, $username, $password );
994
995 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
996
997 $args->RequestContainer->PickupAddressId = $addressId;
998
999 $result = $this->sendRequest( $args, 'DeletePickupAddressRequest', $messageTitle, $soapClient );
1000
1001 } catch ( Exception $e ) {
1002 $this->log( $messageTitle . ' EXCEPTION' );
1003 $this->log( $e->getMessage() );
1004 }
1005
1006 $this->logVar( $result, $messageTitle . ' RESULT' );
1007
1008 return $result;
1009 }
1010
1011 public function setPickupDefaultData( $ws, $username, $password, $system, $data ) {
1012
1013 $messageTitle = 'SET PICKUP DEFAULT DATA';
1014 $this->log( $messageTitle );
1015 $internalReferenceID = $this->generateRandomString();
1016
1017 $result = false;
1018
1019 try {
1020 $soapClient = new MbeSoapClient( $ws, array(
1021 'encoding' => 'utf-8',
1022 'trace' => 1
1023 ), $username, $password, false );
1024
1025 $args = $this->setBaseClass( $system, $username, $password );
1026
1027 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1028
1029 $args->RequestContainer->Cutoff = $data['cutoff'];
1030 $args->RequestContainer->Notes = $data['notes'];
1031 $args->RequestContainer->PreferredFrom = $data['preferred_from'];
1032 $args->RequestContainer->PreferredTo = $data['preferred_to'];
1033 $args->RequestContainer->AlternativeFrom = $data['alternative_from'];
1034 $args->RequestContainer->AlternativeTo = $data['alternative_to'];
1035
1036 $result = $this->sendRequest( $args, 'SetPickupDefaultDataRequest', $messageTitle, $soapClient );
1037
1038 } catch ( Exception $e ) {
1039 $this->log( $messageTitle . ' EXCEPTION' );
1040 $this->log( $e->getMessage() );
1041 }
1042
1043 $this->logVar( $result, $messageTitle . ' RESULT' );
1044
1045 return $result;
1046
1047
1048 }
1049
1050 public function getPickupDefaultData( $ws, $username, $password, $system ) {
1051
1052 $messageTitle = 'GET PICKUP DEFAULT DATA';
1053 $this->log( $messageTitle );
1054 $internalReferenceID = $this->generateRandomString();
1055
1056 $result = false;
1057
1058 try {
1059 $soapClient = new MbeSoapClient( $ws, array(
1060 'encoding' => 'utf-8',
1061 'trace' => 1
1062 ), $username, $password, false );
1063
1064 $args = $this->setBaseClass( $system, $username, $password );
1065
1066 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1067
1068 $result = $this->sendRequest( $args, 'GetPickupDefaultDataRequest', $messageTitle, $soapClient );
1069
1070 } catch ( Exception $e ) {
1071 $this->log( $messageTitle . ' EXCEPTION' );
1072 $this->log( $e->getMessage() );
1073 }
1074
1075 $this->logVar( $result, $messageTitle . ' RESULT' );
1076
1077 return $result;
1078
1079
1080 }
1081
1082 public function getPickupManifest( $ws, $username, $password, $system, $masterTrackingId ) {
1083 $messageTitle = 'GET PICKUP MANIFEST DATA';
1084 $this->log( $messageTitle );
1085 $internalReferenceID = $this->generateRandomString();
1086
1087 $result = false;
1088
1089 try {
1090 $soapClient = new MbeSoapClient( $ws, array(
1091 'encoding' => 'utf-8',
1092 'trace' => 1
1093 ), $username, $password, false );
1094
1095 // $masterTrackingMBE = [];
1096 // foreach ( $masterTrackingIds as $masterTrackingId ) {
1097 // $masterTrackingMBE[] = $masterTrackingId;
1098 // }
1099
1100 $args = $this->setBaseClass( $system, $username, $password );
1101 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1102 $args->RequestContainer->MasterTrackingMBE = $masterTrackingId;
1103 $result = $this->sendRequest( $args, 'PickupManifestListRequest', $messageTitle, $soapClient );
1104
1105 } catch ( Exception $e ) {
1106 $this->log( $messageTitle . ' EXCEPTION' );
1107 $this->log( $e->getMessage() );
1108 throw $e;
1109 }
1110
1111 $this->logVar( $result, $messageTitle . ' RESULT' );
1112
1113 return $result;
1114 }
1115
1116 /**
1117 * @throws SoapFault
1118 * @throws \MbeExceptions\ApiRequestException
1119 */
1120 public function sendRestApiCredentials( $ws, $username, $password, $system, $apiKey, $trackingEndpoint ) {
1121 $messageTitle = 'SEND REST API CREDENTIALS AND ENDPOINT';
1122 $this->log( $messageTitle );
1123 $internalReferenceID = $this->generateRandomString();
1124
1125 $result = false;
1126
1127 try {
1128 $soapClient = new MbeSoapClient( $ws, array(
1129 'encoding' => 'utf-8',
1130 'trace' => 1
1131 ), $username, $password, false );
1132
1133 $args = $this->setBaseClass( $system, $username, $password );
1134 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1135
1136 $args->RequestContainer->TrackingEndpoint = $trackingEndpoint;
1137 $args->RequestContainer->Apikey = $apiKey;
1138 $args->RequestContainer->Source = MBE_ESHIP_PLUGIN_NAME . " WooCommerce " . MBE_ESHIP_PLUGIN_VERSION;
1139
1140 $result = $this->sendRequest( $args, 'PushServiceConfigRequest', $messageTitle, $soapClient );
1141
1142 } catch ( \MbeExceptions\ApiRequestException $e ) {
1143 $this->log( $messageTitle . ' API EXCEPTION' );
1144 $this->log( $e->getMessage() );
1145 throw $e;
1146 } catch ( Exception $e ) {
1147 $this->log( $messageTitle . ' EXCEPTION' );
1148 $this->log( $e->getMessage() );
1149 throw $e;
1150 }
1151
1152 $this->logVar( $result, $messageTitle . ' RESULT' );
1153
1154 return $result;
1155 }
1156
1157 /**
1158 * Generates a proForma object and remove Price from the product object, as it's not needed for the request
1159 *
1160 * @param $products
1161 *
1162 * @return array
1163 */
1164 protected function generateProforma( $products ) {
1165 $proForma = [];
1166 foreach ( $products as $product ) {
1167 $item = new stdClass();
1168 $item->Amount = $product->Quantity ?? 0;
1169 $item->Currency = mb_substr( $product->Currency ?? '', 0, 10, 'UTF-8' );
1170 $item->Value = $product->Price ?? 0;
1171 $item->Unit = 'PCS';
1172 $item->Description = mb_substr( $product->Description ?? '', 0, 35, 'UTF-8' );
1173 $proForma[] = $item;
1174 }
1175
1176 return $proForma;
1177 }
1178
1179 protected function generateProducts( $products ) {
1180 foreach ( $products as $product ) {
1181 unset( $product->Price );
1182 unset( $product->Currency );
1183 }
1184
1185 return $products; // this is not really needed, as $products in the calling environment is updated when elements are unset, but we want to use the function return value directly
1186 }
1187
1188 public function getApiBearer( $user, $password ) {
1189 $response = $this->httpPostRequest(
1190 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'oauth/token',
1191 [
1192 'headers' => [
1193 'Accept' => ' application/json, text/plain, */*',
1194 'Content-Type' => ' application/x-www-form-urlencoded;charset=UTF-8',
1195 'Authorization' => ' Basic dGVsZXBvcnQtZmU6',
1196 ],
1197 'body' => 'grant_type=password&username=' . $user . '&password=' . $password
1198 ]
1199 );
1200 // $this->logVar( $response, 'API BEARER' );
1201 $this->log( 'API BEARER' );
1202
1203 return $response;
1204 }
1205
1206 public function getApiCustomer( $apiBearer ) {
1207 $response = $this->httpGetRequest(
1208 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'oauth/mine',
1209 [
1210 'headers' => [
1211 'Accept' => ' application/json, text/plain, */*',
1212 'Accept-Language' => ' it,it-IT;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
1213 'Authorization' => ' Bearer ' . $apiBearer->access_token,
1214 ],
1215 ]
1216 );
1217 // $this->logVar( $response, 'API CUSTOMER' );
1218 $this->log( 'API CUSTOMER' );
1219
1220 return $response;
1221 }
1222
1223 public function getApiToken( $user, $password ) {
1224 $apiBearer = $this->getApiBearer( $user, $password );
1225 if ( ! empty( $apiBearer ) ) {
1226 $apiCustomer = $this->getApiCustomer( $apiBearer );
1227 if ( ! empty( $apiCustomer ) ) {
1228 $response = $this->httpPostRequest(
1229 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'oauth/select-store',
1230 [
1231 'headers' => [
1232 'Accept' => 'application/json, text/plain, */*',
1233 'Content-Type' => 'application/json',
1234 'Authorization' => ' Bearer ' . $apiBearer->access_token,
1235 ],
1236 'body' => json_encode( $apiCustomer[0] )
1237 ]
1238 );
1239
1240 // $this->logVar( $response, 'API TOKEN' );
1241 $this->log( 'API TOKEN' );
1242
1243 return $response;
1244 }
1245 }
1246
1247 return null;
1248 }
1249
1250 public function getApiKey( $user, $password ) {
1251 $apiToken = $this->getApiToken( $user, $password );
1252 if ( ! empty( $apiToken ) ) {
1253 $response = $this->httpGetRequest(
1254 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'auth-registry/apikey?idEntity=' . $apiToken->{'legal-entity-id'} . '&legalEntityType=CUSTOMER',
1255 [
1256 'headers' => [
1257 'Accept' => ' application/json, text/plain, */*',
1258 'Authorization' => ' Bearer ' . $apiToken->access_token
1259 ]
1260 ]
1261 );
1262 // $this->logVar( $response, 'GET API KEY' );
1263 $this->log( 'GET API KEY' );
1264
1265 return $response;
1266 }
1267
1268 return null;
1269 }
1270
1271 public function deleteApiKey( $user, $password ) {
1272 $apiKey = $this->getApiKey( $user, $password );
1273 if ( ! empty( $apiKey->content ) ) {
1274 $apiToken = $this->getApiToken( $user, $password );
1275 if ( ! empty( $apiToken ) ) {
1276 $response = $this->httpDeleteRequest(
1277 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'auth-registry/apikey?apikey=' . $apiKey->content[0]->apiKey,
1278 [
1279 'headers' => [
1280 'Accept' => ' application/json, text/plain, */*',
1281 'Authorization' => ' Bearer ' . $apiToken->access_token
1282 ]
1283 ]
1284 );
1285 // $this->logVar( $response, 'DELETE API KEY' );
1286 $this->log( 'DELETE API KEY' );
1287 }
1288
1289 return true;
1290 }
1291
1292 return false;
1293 }
1294
1295 /**
1296 * @throws Exception
1297 */
1298 public function generateApiKey( $user, $password ) {
1299 $apiKeyDeleted = $this->deleteApiKey( $user, $password );
1300 if ( $apiKeyDeleted ) {
1301 $apiToken = $this->getApiToken( $user, $password );
1302 if ( ! empty( $apiToken ) ) {
1303 $response = $this->httpPostRequest(
1304 preg_replace( '/(ws\/e-link\.wsdl)$/i', '', $this->wsUrl ) . 'auth-registry/apikey',
1305 [
1306 'headers' => [
1307 'Accept' => ' application/json, text/plain, */*',
1308 'Authorization' => ' Bearer ' . $apiToken->access_token
1309 ],
1310 'body' => 'legalEntityType=CUSTOMER&roleName=ONLINEMBE_USER&idEntity=' . $apiToken->{'legal-entity-id'} . '&username=' . $apiToken->username,
1311 ]
1312 );
1313 // $this->logVar( $response, 'GENERATE API KEY' );
1314 $this->log( 'GENERATE API KEY' );
1315
1316 return $response;
1317 }
1318 }
1319
1320 return null;
1321 }
1322
1323 /**
1324 * @throws Exception
1325 */
1326 public function httpGetRequest( $url, $options ) {
1327 $response = wp_remote_get( $url, $options );
1328
1329 return $this->checkHttpResponse( $response, $url );
1330 }
1331
1332 /**
1333 * @throws Exception
1334 */
1335 public function httpPostRequest( $url, $options ) {
1336 $response = wp_remote_post( $url, $options );
1337
1338 return $this->checkHttpResponse( $response, $url, 'POST' );
1339 }
1340
1341 /**
1342 * @throws Exception
1343 */
1344 public function httpDeleteRequest( $url, $options ) {
1345 $options['method'] = 'DELETE';
1346 $response = wp_remote_request( $url, $options );
1347
1348 return $this->checkHttpResponse( $response, $url, 'DELETE' );
1349 }
1350
1351 /**
1352 * @throws HttpRequestException
1353 */
1354 protected function checkHttpResponse( $response, $url, $method = 'GET' ) {
1355 if ( ( ! is_wp_error( $response ) ) && ( preg_match( '(2[0-9]{2})', wp_remote_retrieve_response_code( $response ) ) ) ) {
1356 $responseBody = json_decode( $response['body'] );
1357 if ( empty( $response['body'] ) || json_last_error() === JSON_ERROR_NONE ) {
1358 return $responseBody;
1359 }
1360 throw new HttpRequestException( esc_html( 'JSON Error: ' . json_last_error_msg() ) );
1361 }
1362 $message = wp_remote_retrieve_response_code( $response ) . ' - ' . wp_remote_retrieve_response_message( $response ) . ' - ' . wp_remote_retrieve_body( $response );
1363 $this->log( $method . ' Http Request: ' . $url . ' - ' . $message );
1364 throw new HttpRequestException( esc_html( $message ) );
1365 }
1366
1367 /**
1368 * @throws \MbeExceptions\ApiRequestException
1369 */
1370 private function sendRequest( $args, $functionName, $messageTitle, $soapClient, $checkInternalId = true ) {
1371 $result = false;
1372
1373 $logArgs = json_decode( json_encode( $args ), true );
1374 $logArgs['RequestContainer']['Credentials'] = null;
1375
1376 if ( isset( $logArgs['RequestContainer']['Apikey'] ) ) {
1377 $logArgs['RequestContainer']['Apikey'] = null;
1378 }
1379
1380 $this->logVar( $logArgs, $messageTitle . ' ARGS' );
1381
1382 $soapResult = $soapClient->__soapCall( $functionName, array( $args ) );
1383
1384 $lastResponse = $soapClient->__getLastResponse();
1385
1386 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
1387
1388 $this->checkResponseErrors( $soapResult, $lastResponse );
1389
1390 $internalReferenceIDMatches = $args->RequestContainer->InternalReferenceID === ( $soapResult->RequestContainer->InternalReferenceID ?? null );
1391
1392 if ( $this->isResponseValid( $soapResult, $args->RequestContainer->InternalReferenceID, $checkInternalId ) ) {
1393 $result = $soapResult->RequestContainer;
1394 } elseif ( ! $internalReferenceIDMatches && $checkInternalId ) {
1395 $message = 'InternalReferenceID doesn\'t match';
1396 throw new \MbeExceptions\ApiRequestEmptyResponse( esc_html( $message ) );
1397 }
1398
1399 return $result;
1400 }
1401
1402 private function setBaseClass( $system, $username, $password ): stdClass {
1403
1404 $args = new stdClass;
1405 $args->RequestContainer = new stdClass;
1406 $args->RequestContainer->System = $system;
1407 $args->RequestContainer->Credentials = new stdClass;
1408 $args->RequestContainer->Credentials->Username = $username;
1409 $args->RequestContainer->Credentials->Passphrase = $password;
1410
1411 return $args;
1412 }
1413
1414 /**
1415 * @throws \MbeExceptions\ApiRequestException
1416 */
1417 private function setShipmentContainer(
1418 $system,
1419 $username,
1420 $password,
1421 string $internalReferenceID,
1422 $firstName,
1423 $lastName,
1424 $companyName,
1425 $address,
1426 $phone,
1427 $postCode,
1428 $city,
1429 $state,
1430 $country,
1431 $email,
1432 $subZone,
1433 $shipperType,
1434 $isCod,
1435 $codValue,
1436 $insurance,
1437 $insuranceValue,
1438 $service,
1439 $shipmentType,
1440 $reference,
1441 $items,
1442 $products,
1443 $goodsValue,
1444 $notes,
1445 $insuranceCode = null
1446 ): stdClass {
1447 //WS ARGS
1448 $args = $this->setBaseClass( $system, $username, $password );
1449
1450 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1451
1452 $args->RequestContainer->Recipient = new stdClass;
1453
1454 $recipientName = $firstName . " " . $lastName;
1455 $RecipientCompanyName = $companyName;
1456 $recipientName = mb_substr( $recipientName, 0, 35, 'UTF-8' );
1457 $RecipientCompanyName = mb_substr( $RecipientCompanyName, 0, 35, 'UTF-8' );
1458 if ( empty( $RecipientCompanyName ) ) {
1459 $RecipientCompanyName = $recipientName;
1460 }
1461
1462 $args->RequestContainer->Recipient->Name = $recipientName;
1463 //$args->RequestContainer->Recipient->LastName = $lastName;
1464 $args->RequestContainer->Recipient->CompanyName = $RecipientCompanyName;
1465 $args->RequestContainer->Recipient->Address = $address;
1466 //$args->RequestContainer->Recipient->Phone = $phone;
1467 $args->RequestContainer->Recipient->Phone = mb_substr( $phone ?? '', 0, 50, 'UTF-8' );
1468 $args->RequestContainer->Recipient->ZipCode = $postCode;
1469 $args->RequestContainer->Recipient->City = $city;
1470 $args->RequestContainer->Recipient->State = mb_substr( $state ?? '', 0, 2, 'UTF-8' );
1471 $args->RequestContainer->Recipient->Country = $country;
1472 $args->RequestContainer->Recipient->Email = $email;
1473 if ( $subZone ) {
1474 $args->RequestContainer->Recipient->SubzoneId = $subZone;
1475 }
1476
1477 //Department ID if any
1478
1479 if ( $this->helper->getCustomerAddressAsSender() ) {
1480 $departmentAddressId = $this->helper->getOrderDepartmentAddressId( $reference );
1481 // If address is missing, but it's mandatory, it will throw an error
1482 if ( $this->helper->getMandatoryDepartment() && empty( $departmentAddressId ) && ! $this->helper->hasDepartmentDefault() ) {
1483 throw new \MbeExceptions\ApiRequestException( __( 'Order', 'mail-boxes-etc' ) . ':' . $reference . ' - ' . __( 'Department address not set or not valid, shipment not created', 'mail-boxes-etc' ) );
1484 }
1485 $args->RequestContainer->DepartmentID = $departmentAddressId ?? '';
1486 }
1487
1488 $shipmentNode = new stdClass;
1489
1490 $shipmentNode->ShipperType = $shipperType;//"MBE";//COURIERLDV - MBE
1491 $shipmentNode->Description = "ECOMMERCE SHOP PURCHASE";
1492 $shipmentNode->COD = $isCod;
1493 if ( $isCod ) {
1494 $shipmentNode->CODValue = $codValue;
1495 $shipmentNode->MethodPayment = "CASH";//CASH - CHECK
1496 }
1497
1498 $this->insuranceParameters( $insurance, $insuranceCode, $insuranceValue, $shipmentNode );
1499
1500 $shipmentNode->Service = $service;//SEE /SSE
1501
1502 //$shipmentNode->Courier = "";
1503 //$shipmentNode->CourierService = "SEE";
1504 //$shipmentNode->CourierAccount = "";
1505 $shipmentNode->PackageType = $shipmentType;
1506 //$shipmentNode->Value = 0;
1507 $shipmentNode->Referring = $reference;
1508
1509 $shipmentNode->Items = $items;
1510
1511 $shipmentNode->ProformaInvoice = $this->generateProforma( $products );
1512
1513 $shipmentNode->Products = $this->generateProducts( $products );
1514
1515 $shipmentNode->Value = $goodsValue;
1516
1517 $shipmentNode->ShipmentOrigin = MBE_ESHIP_PLUGIN_NAME . " WooCommerce " . MBE_ESHIP_PLUGIN_VERSION;
1518
1519 $shipmentNode->Notes = mb_substr( $notes, 0, 50, 'UTF-8' );
1520
1521 // Add information for the delivery point, if used
1522 $order_delivery_point_shipment = $this->helper->getOrderDeliveryPointShipment( $reference );
1523
1524 if ( $order_delivery_point_shipment === 'Yes' ) {
1525 // Remove new line, if any, in case of delivery point address
1526 $args->RequestContainer->Recipient->Address = str_replace( "\n", ' ', $address );
1527 $deliveryPointData = json_decode( $this->helper->getOrderDeliveryPointCustomData( $reference ) );
1528 $shipmentNode->Service = $this->getDeliveryPointMolServiceByNetworkCode( $deliveryPointData->networkCode, $reference );
1529 $shipmentNode->Courier = strtolower( $deliveryPointData->serviceType ) === 'labeling' ? $deliveryPointData->networkName : 'GEL';
1530 $shipmentNode->CourierService = $deliveryPointData->networkCode;
1531 // $args->RequestContainer->Shipment->CourierServiceId = $deliveryPointData->networkCode;
1532
1533 // Set new delivery point object for API
1534 $args->RequestContainer->RecipientDeliveryPoint = clone $args->RequestContainer->Recipient;
1535 $args->RequestContainer->RecipientDeliveryPoint->DeliveryPointId = sanitize_text_field( $deliveryPointData->code );
1536 }
1537
1538 if ( $this->helper->isEnabledTaxAndDuties() ) {
1539 $shipmentNode->LanguageCode = $this->helper->getCountry();
1540 $shipmentNode->TaxAndDutyPluginAct = true;
1541 $shipmentNode->ProformaIncoterms = $this->helper->getTaxAndDutiesModeName();
1542 }
1543
1544 $args->RequestContainer->Shipment = $shipmentNode;
1545
1546 return $args;
1547 }
1548
1549 public function getDefaultPickupAddress( $ws, $username, $password, $system ): array {
1550 $pickupAddresses = $this->getCustomerPickupAddresses( $ws, $username, $password, $system );
1551 $address = [];
1552 if ( ! empty( $pickupAddresses ) ) {
1553 $address = array_values( array_filter( $pickupAddresses, function ( $var ) {
1554 return ( $var['IsDefault'] == true );
1555 } ) );
1556 }
1557
1558 return empty( $address ) ? [] : $address;
1559 }
1560
1561 public function getPickupAddressById( $ws, $username, $password, $system, $id ): array {
1562 $pickupAddresses = $this->getCustomerPickupAddresses( $ws, $username, $password, $system );
1563 $address = [];
1564 if ( ! empty( $pickupAddresses ) ) {
1565 $address = array_values( array_filter( $this->getCustomerPickupAddresses( $ws, $username, $password, $system ), function ( $var ) use ( $id ) {
1566 return ( $var['PickupAddressId'] == $id );
1567 } ) );
1568 }
1569
1570 return empty( $address ) ? [] : $address[0];
1571 }
1572
1573 /**
1574 * @throws \MbeExceptions\ApiRequestException
1575 */
1576 protected function checkResponseErrors( $soapResult, string $messageTitle ): void {
1577 if ( isset( $soapResult->RequestContainer->Errors ) ) {
1578 $this->logVar( $soapResult->RequestContainer->Errors, $messageTitle . ' ERRORS' );
1579 $message = '';
1580 if ( is_array( $soapResult->RequestContainer->Errors->Error ) ) {
1581 foreach ( $soapResult->RequestContainer->Errors->Error as $error ) {
1582 $message .= ', ' . $error->Description;
1583 }
1584 } else {
1585 $message = $soapResult->RequestContainer->Errors->Error->Description;
1586 }
1587 throw new \MbeExceptions\ApiRequestException( esc_html( $message ) );
1588 }
1589 }
1590
1591 private function isResponseValid( object $soapResult, string $internalReferenceID, bool $checkInternalId = true ): bool {
1592 $isValid = isset( $soapResult->RequestContainer->Status )
1593 && $soapResult->RequestContainer->Status == self::EXPECTED_STATUS;
1594
1595 if ( $checkInternalId ) {
1596 $isValid = $isValid && isset( $soapResult->RequestContainer->InternalReferenceID )
1597 && $soapResult->RequestContainer->InternalReferenceID == $internalReferenceID;
1598 }
1599
1600 return $isValid;
1601 }
1602
1603
1604 protected function getDeliveryPointMolServiceByNetworkCode( $networkCode, $orderId ) {
1605 $combined = array_combine( $this->helper->getOrderDeliveryPointServices( $orderId )['courier'], $this->helper->getOrderDeliveryPointServices( $orderId )['mol'] );
1606
1607 return $combined[ $networkCode ] ?? null;
1608 }
1609
1610 /**
1611 * @param $insurance
1612 * @param $insuranceCode
1613 * @param stdClass $node
1614 * @param $insuranceValue
1615 *
1616 * @return void
1617 */
1618 private function insuranceParameters( $insurance, $insuranceCode, $insuranceValue, stdClass $node ): void {
1619 $mbeSafeValue = false;
1620 $mbeSafeValue4Business = false;
1621 $mbeInsurance = false;
1622
1623 switch ( $insuranceCode ) {
1624 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_INSURANCE_CODE_SUFFIX:
1625 $mbeInsurance = true;
1626 break;
1627 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_CODE_SUFFIX:
1628 $mbeSafeValue = true;
1629 break;
1630 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_ART_CODE_SUFFIX:
1631 $mbeSafeValue = true;
1632 $node->GoodType = 'ART';
1633 break;
1634 case Mbe_Shipping_Helper_Data::MBE_SHIPPING_WITH_SAFE_VALUE_4B_CODE_SUFFIX:
1635 $mbeSafeValue4Business = true;
1636 break;
1637 default:
1638 //
1639 break;
1640 }
1641
1642 $node->Insurance = $mbeInsurance;
1643 $node->InsuranceValue = $mbeInsurance ? $insuranceValue : '';
1644 $node->MBESafeValue = $mbeSafeValue;
1645 $node->MBESafeValueValue = $mbeSafeValue ? $insuranceValue : '';;
1646 $node->MBESafeValue4Business = $mbeSafeValue4Business;
1647 $node->MBESafeValue4BusinessValue = $mbeSafeValue4Business ? $insuranceValue : '';
1648 }
1649
1650 public function getDepartmentsAddress( $ws, $username, $password ) {
1651 $messageTitle = 'GET CUSTOMER DEPARTMENTS ADDRESS ';
1652 $this->log( $messageTitle );
1653 $internalReferenceID = $this->generateRandomString();
1654
1655 $result = false;
1656
1657 try {
1658 $soapClient = new MbeSoapClient( $ws, array(
1659 'encoding' => 'utf-8',
1660 'trace' => 1
1661 ), $username, $password, false );
1662
1663 $args = new stdClass;
1664 $args->RequestContainer = new stdClass;
1665 // $args->RequestContainer->System = $system;
1666 $args->RequestContainer->Credentials = new stdClass();
1667 $args->RequestContainer->Credentials->Username = $username;
1668 $args->RequestContainer->Credentials->Passphrase = $password;
1669 $args->RequestContainer->CustomerID = $this->helper->getCustomerID();
1670 $args->RequestContainer->OnlyWithEmptyAddress = false;
1671
1672 $args->RequestContainer->InternalReferenceID = $internalReferenceID;
1673
1674 $soapResult = $soapClient->__soapCall( "ListDepartmentsRequest", array( $args ) );
1675
1676 $lastResponse = $soapClient->__getLastResponse();
1677
1678 $this->logVar( $lastResponse, $messageTitle . ' RESPONSE' );
1679
1680 $this->checkResponseErrors( $soapResult, $lastResponse );
1681
1682 $this->logVar( $args, $messageTitle . ' REQUEST ' );
1683
1684 $result = [];
1685
1686 if ( ! property_exists( $soapResult->RequestContainer, 'Department' ) ) {
1687 return $result;
1688 }
1689
1690 if ( is_array( $soapResult->RequestContainer->Department ) ) {
1691 $result = array_map( function ( $address ) {
1692 return get_object_vars( $address );
1693 }, $soapResult->RequestContainer->Department );
1694 } else {
1695 $result[] = get_object_vars( $soapResult->RequestContainer->Department );
1696 }
1697
1698 } catch ( Exception $e ) {
1699 $this->log( $messageTitle . ' EXCEPTION' );
1700 $this->log( $e->getMessage() );
1701 }
1702
1703 $this->logVar( $result, $messageTitle . ' RESULT' );
1704
1705 return $result;
1706 }
1707
1708 }