234 lines
5.9 KiB
PHP
234 lines
5.9 KiB
PHP
<?php
|
|
|
|
use VendorDHL\Psr\Log\LoggerInterface;
|
|
use WPDesk\DHL\HelperTrait;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
} // Exit if accessed directly
|
|
|
|
if ( ! class_exists( 'WPDesk_WooCommerce_DHL_Parcelshop_API' ) ) {
|
|
|
|
/**
|
|
* DHL Parcelshop API.
|
|
*/
|
|
class WPDesk_WooCommerce_DHL_Parcelshop_API {
|
|
use HelperTrait;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $login;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private $password;
|
|
|
|
/**
|
|
* @var bool
|
|
*/
|
|
private $test_api;
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $auth_data;
|
|
|
|
/**
|
|
* @var bool|SoapClient
|
|
*/
|
|
private $client = false;
|
|
|
|
/**
|
|
* @var LoggerInterface
|
|
*/
|
|
private $logger;
|
|
|
|
/**
|
|
* @param string $login .
|
|
* @param string $password .
|
|
* @param LoggerInterface $logger .
|
|
* @param bool $test_api .
|
|
*/
|
|
public function __construct( $login, $password, $logger, $test_api = false ) {
|
|
$this->login = $login;
|
|
$this->password = $password;
|
|
$this->test_api = $test_api;
|
|
$this->auth_data = [
|
|
'username' => $login,
|
|
'password' => $password,
|
|
];
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
public function get_client() {
|
|
if ( ! $this->client ) {
|
|
$dhl_webapi_url = 'https://dhl24.com.pl/servicepoint';
|
|
if ( $this->test_api ) {
|
|
$dhl_webapi_url = 'https://sandbox.dhl24.com.pl/servicepoint';
|
|
}
|
|
$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 = [
|
|
'structure' => [
|
|
'authData' => $this->auth_data,
|
|
'postcode' => '53031',
|
|
'city' => 'Wrocław',
|
|
'radius' => 1,
|
|
],
|
|
];
|
|
$transient_key = $this->prepare_transient_key( $params, __METHOD__ );
|
|
$ret = get_transient( $transient_key );
|
|
if ( false === $ret ) {
|
|
$ret = $client->getNearestServicepoints( $params );
|
|
$ten_minutes = 60 * 10;
|
|
set_transient( $transient_key, $ret, $ten_minutes );
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function create_shipment( $shipment ) {
|
|
$client = $this->get_client();
|
|
$params = [
|
|
'shipment' => [
|
|
'authData' => $this->auth_data,
|
|
'shipmentData' => $shipment,
|
|
],
|
|
];
|
|
|
|
return $client->createShipment( $params );
|
|
}
|
|
|
|
public function get_nearest_servicepoints( $postcode = '00001', $radius = 5000, $is_cod = false, $country = '', $city = '' ) {
|
|
if ( $radius === 5000 ) {
|
|
$postcode = '00001';
|
|
}
|
|
$postcode = str_replace( '-', '', $postcode );
|
|
$params = [
|
|
'structure' => [
|
|
'authData' => $this->auth_data,
|
|
'postcode' => $postcode,
|
|
'radius' => $radius,
|
|
'country' => $country,
|
|
'city' => $city,
|
|
],
|
|
];
|
|
|
|
$params_for_cache = array_merge(
|
|
$params,
|
|
[
|
|
'test' => $this->test_api,
|
|
'is_cod' => $is_cod,
|
|
'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;
|
|
if ( $is_cod ) {
|
|
$nearest_servicepoints = $client->getNearestServicepointsCOD( $params ); // phpcs:ignore
|
|
if ( isset( $nearest_servicepoints ) && isset( $nearest_servicepoints->getNearestServicepointsCODResult ) ) { // phpcs:ignore
|
|
$nearest_servicepoints_results = $nearest_servicepoints->getNearestServicepointsCODResult; // phpcs:ignore
|
|
}
|
|
} else {
|
|
$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( $points ) ) {
|
|
$points = [ $points ];
|
|
}
|
|
foreach ( $points as $point ) {
|
|
$point->id = $point->sap;
|
|
$nearest_servicepoints_array[ $point->id ] = $point;
|
|
}
|
|
setlocale( LC_ALL, 'pl_PL' );
|
|
uasort( $nearest_servicepoints_array, [ $this, 'cmp_servicepoints' ] );
|
|
}
|
|
set_transient( $transient_key, $nearest_servicepoints_array, $nearest_servicepoints_download_interval ); // Day.
|
|
}
|
|
|
|
return $nearest_servicepoints_array;
|
|
}
|
|
|
|
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->log( $e->getMessage() );
|
|
}
|
|
foreach ( $all_items as $item ) {
|
|
if ( $item->sap == $service_point ) {
|
|
return $item;
|
|
}
|
|
}
|
|
|
|
return '';
|
|
} catch ( Exception $e ) {
|
|
error_log( "Exception {$e->getMessage()} in DHL::get_service_point" );
|
|
|
|
return '';
|
|
}
|
|
}
|
|
|
|
public function get_label( $shipment_id, $label_format ) {
|
|
|
|
$client = $this->get_client();
|
|
|
|
$params = [
|
|
'structure' => [
|
|
'authData' => $this->auth_data,
|
|
'shipment' => $shipment_id,
|
|
'type' => $label_format,
|
|
],
|
|
];
|
|
|
|
return $client->getLabel( $params );
|
|
}
|
|
|
|
public function delete_shipment( $shipment_id ) {
|
|
$client = $this->get_client();
|
|
|
|
$params = [
|
|
'shipment' => [
|
|
'authData' => $this->auth_data,
|
|
'shipment' => $shipment_id,
|
|
],
|
|
];
|
|
|
|
return $client->deleteShipment( $params );
|
|
}
|
|
}
|
|
}
|