login = $login; $this->password = $password; $this->test_api = $test_api; $this->auth_data = [ 'username' => $login, 'password' => $password, ]; $this->logger = $logger; } public function clear_cache() { global $wpdb; $transients = $wpdb->get_results( "SELECT option_name AS name, option_value AS value FROM $wpdb->options WHERE option_name LIKE '_transient_dhl%' " ); foreach ( $transients as $transient ) { delete_transient( substr( $transient->name, 11 ) ); } } /** * @return SoapClient * @throws SoapFault */ public function get_client() { if ( ! $this->client ) { $dhl_webapi_url = 'https://dhl24.com.pl/webapi2'; if ( $this->test_api ) { $dhl_webapi_url = 'https://sandbox.dhl24.com.pl/webapi2'; } $this->client = new \VendorDHL\WPDesk\SOAP\SoapClientWithLogger( new SoapClient( $dhl_webapi_url, [ 'keep-alive' => true, 'connection_timeout' => 30, 'trace' => 1, ] ), $this->logger, 2000 ); } return $this->client; } public function ping() { $client = $this->get_client(); $params = [ 'authData' => $this->auth_data, 'createdFrom' => date( 'Y-m-d' ), 'createdTo' => date( 'Y-m-d', time() + 5 * DAY_IN_SECONDS ), ]; return $client->getMyShipments( $params ); } public function create_shipment( $shipments ) { $client = $this->get_client(); $params = [ 'authData' => $this->auth_data, 'shipments' => $shipments, ]; return $client->createShipments( $params ); } public function get_service_point( $service_point, $post_code, $radius, $is_cod = false, $country = '', $city = '' ) { try { try { $all_items = $this->get_nearest_servicepoints( $post_code, $radius, $is_cod, $country, $city ); } catch ( Exception $e ) { $this->logger->warning( $e->getMessage() ); } foreach ( $all_items as $item ) { if ( $item->id === (string) $service_point ) { return $item; } } return ''; } catch ( Exception $e ) { error_log( "Exception {$e->getMessage()} in DHL::get_service_point" ); return ''; } } public function get_postal_code_services( $post_code, $pickup_date, $cache = true ) { $transient_name = 'dhl_pcs_services_' . $post_code . '-' . $pickup_date . '_' . date( 'H' ); if ( $this->test_api ) { $transient_name .= '_test'; } $ret = get_transient( $transient_name ); if ( $ret === false ) { $client = $this->get_client(); $params = [ 'authData' => $this->auth_data, 'postCode' => $post_code, 'pickupDate' => $pickup_date, ]; $ret = $client->getPostalCodeServices( $params ); set_transient( $transient_name, $ret, HOUR_IN_SECONDS ); } return $ret; } public function get_label( $shipment_id, $label_format ) { $client = $this->get_client(); $params = [ 'authData' => $this->auth_data, 'itemsToPrint' => [ [ 'labelType' => $label_format, 'shipmentId' => $shipment_id, ], ], ]; return $client->getLabels( $params ); } public function delete_shipment( $shipment_id ) { $client = $this->get_client(); $params = [ 'authData' => $this->auth_data, 'shipments' => [ $shipment_id ], ]; return $client->deleteShipments( $params ); } public function get_nearest_servicepoints( $postcode = '00001', $radius = 5000, $is_cod = false, $country = '', $city = '' ) { $city = function_exists( 'mb_substr' ) ? mb_substr( $city, 0, -1 ) : substr( $city, 0, -1 ); $postcode = str_replace( '-', '', $postcode ); $params = [ 'authData' => $this->auth_data, 'structure' => [ 'postcode' => $postcode, 'radius' => $radius, 'country' => $country, 'city' => $city, ], ]; $params_for_cache = array_merge( $params, [ 'test' => $this->test_api, 'version' => WOOCOMMERCE_DHL_VERSION, ] ); $transient_key = $this->prepare_transient_key( $params_for_cache, __METHOD__ ); $nearest_servicepoints_array = get_transient( $transient_key ); $nearest_servicepoints_download_interval = 60 * 60 * 24; if ( ! $nearest_servicepoints_array ) { $client = $this->get_client(); $nearest_servicepoints_results = null; $nearest_servicepoints = $client->getNearestServicepoints( $params ); // phpcs:ignore if ( isset( $nearest_servicepoints ) && isset( $nearest_servicepoints->getNearestServicepointsResult ) ) { // phpcs:ignore $nearest_servicepoints_results = $nearest_servicepoints->getNearestServicepointsResult; // phpcs:ignore } $nearest_servicepoints_array = []; if ( isset( $nearest_servicepoints_results ) && isset( $nearest_servicepoints_results->points ) && isset( $nearest_servicepoints_results->points->item ) ) { $points = $nearest_servicepoints_results->points->item; if ( ! is_array( $nearest_servicepoints_array ) ) { $points = [ $nearest_servicepoints_array ]; } foreach ( $points as $point ) { if ( $point->type === 'PAKETSHOP' || $point->type === 'PARCELSHOP' ) { $point->id = $point->sap . ':' . $point->address->postcode; $nearest_servicepoints_array[ $point->id ] = $point; } } uasort( $nearest_servicepoints_array, [ $this, 'cmp_servicepoints' ] ); } set_transient( $transient_key, $nearest_servicepoints_array, $nearest_servicepoints_download_interval ); // Day. } return $nearest_servicepoints_array; } } }