346 lines
8.9 KiB
PHP
346 lines
8.9 KiB
PHP
<?php
|
|
/**
|
|
* DPD API.
|
|
*
|
|
* @package WooCommerce DPD
|
|
*/
|
|
|
|
use DPDVendor\Psr\Log\LoggerInterface;
|
|
use DPDVendor\WPDesk\SOAP\SoapClientWithLogger;
|
|
|
|
/**
|
|
* Can send DPD API requests.
|
|
*/
|
|
class WPDesk_WooCommerce_DPD_API {
|
|
|
|
/**
|
|
* @var LoggerInterface
|
|
*/
|
|
private $logger;
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @var WPDesk_WooCommerce_DPD_Auth_Data
|
|
*/
|
|
private $auth_data;
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @var bool
|
|
*/
|
|
private $test_api;
|
|
|
|
/**
|
|
*
|
|
* .
|
|
*
|
|
* @var bool
|
|
*/
|
|
private $disable_ssl_verification = false;
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @var bool
|
|
*/
|
|
private $disable_cache_wsdl = false;
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @var bool
|
|
*/
|
|
private $client = false;
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @var array
|
|
*/
|
|
private $messages = array();
|
|
|
|
/**
|
|
* WPDesk_WooCommerce_DPD_API constructor.
|
|
*
|
|
* @param WPDesk_WooCommerce_DPD_Auth_Data $auth_data .
|
|
* @param LoggerInterface $logger .
|
|
* @param bool $test_api .
|
|
* @param bool $disable_ssl_verification .
|
|
* @param bool $disable_cache_wsdl .
|
|
*/
|
|
public function __construct(
|
|
WPDesk_WooCommerce_DPD_Auth_Data $auth_data,
|
|
LoggerInterface $logger,
|
|
$test_api = false,
|
|
$disable_ssl_verification = false,
|
|
$disable_cache_wsdl = false
|
|
) {
|
|
$this->auth_data = $auth_data;
|
|
$this->test_api = $test_api;
|
|
|
|
$this->messages = array(
|
|
'DISALLOWED_FID' => __( 'Błędny numer klienta', 'woocommerce-dpd' ),
|
|
'Login failed' => __( 'Błędny login lub hasło', 'woocommerce-dpd' ),
|
|
);
|
|
|
|
$this->disable_ssl_verification = $disable_ssl_verification;
|
|
$this->disable_cache_wsdl = $disable_cache_wsdl;
|
|
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param string $message .
|
|
*
|
|
* @return string
|
|
*/
|
|
public function translate_messages( $message ) {
|
|
if ( isset( $this->messages[ $message ] ) ) {
|
|
return $this->messages[ $message ];
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*/
|
|
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_dpd%'" ); // phpcs:ignore
|
|
foreach ( $transients as $transient ) {
|
|
delete_transient( substr( $transient->name, 11 ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @return bool|SoapClient
|
|
*
|
|
* @throws SoapFault .
|
|
*/
|
|
public function get_client() {
|
|
if ( ! $this->client ) {
|
|
$dpd_webapi_url = 'https://dpdservices.dpd.com.pl/DPDPackageObjServicesService/DPDPackageObjServices?WSDL';
|
|
if ( $this->test_api ) {
|
|
$dpd_webapi_url = 'https://dpdservicesdemo.dpd.com.pl/DPDPackageObjServicesService/DPDPackageObjServices?WSDL';
|
|
}
|
|
$client_options = array(
|
|
'keep-alive' => true,
|
|
'connection_timeout' => 30,
|
|
'trace' => 1,
|
|
);
|
|
if ( $this->disable_ssl_verification ) {
|
|
$context = stream_context_create(
|
|
array(
|
|
'ssl' => array(
|
|
// set some SSL/TLS specific options.
|
|
'verify_peer' => false,
|
|
'verify_peer_name' => false,
|
|
),
|
|
)
|
|
);
|
|
|
|
$client_options['stream_context'] = $context;
|
|
}
|
|
if ( $this->disable_cache_wsdl ) {
|
|
$client_options['cache_wsdl'] = 0;
|
|
}
|
|
$this->client = new SoapClientWithLogger(
|
|
new SoapClient(
|
|
$dpd_webapi_url,
|
|
$client_options
|
|
),
|
|
$this->logger
|
|
);
|
|
}
|
|
return $this->client;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @return mixed
|
|
*
|
|
* @throws SoapFault .
|
|
*/
|
|
public function ping() {
|
|
$params = array(
|
|
'authDataV1' => $this->auth_data->get_auth_data_v1(),
|
|
'postalCodeV1' => array(
|
|
'countryCode' => 'PL',
|
|
'zipCode' => '22500',
|
|
),
|
|
);
|
|
|
|
return $this->get_client()->findPostalCodeV1( $params );
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param array $shipments .
|
|
*
|
|
* @return stdClass
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
public function create_shipment( $shipments ) {
|
|
$client = $this->get_client();
|
|
$params = array(
|
|
'authDataV1' => $this->auth_data->get_auth_data_v1(),
|
|
'openUMLFeV3' => $shipments,
|
|
'policyV1' => array(),
|
|
'langCode' => 'PL',
|
|
);
|
|
try {
|
|
return $client->generatePackagesNumbersV4( $params );
|
|
} catch ( Exception $e ) {
|
|
// Translators: error message.
|
|
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param array $packages .
|
|
* @param string $session_type .
|
|
* @param array $pickup_address .
|
|
* @see WPDesk_Flexible_Shipping_Manifest_dpd::prepare_pickup_address()
|
|
* @return mixed
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
public function create_manifest( $packages, $session_type, $pickup_address ) {
|
|
$client = $this->get_client();
|
|
$params = array(
|
|
'authDataV1' => $this->auth_data->get_auth_data_v1(),
|
|
'dpdServicesParamsV1' => array(
|
|
'pickupAddress' => $pickup_address,
|
|
'policy' => 'IGNORE_ERRORS',
|
|
'session' => array(
|
|
'sessionType' => $session_type,
|
|
'packages' => array(),
|
|
),
|
|
),
|
|
'outputDocFormatV1' => 'PDF',
|
|
'outputDocPageFormatV1' => 'A4',
|
|
);
|
|
foreach ( $packages as $package ) {
|
|
$params['dpdServicesParamsV1']['session']['packages'][] = array( 'packageId' => $package );
|
|
}
|
|
try {
|
|
$ret = $client->generateProtocolV2( $params );
|
|
} catch ( Exception $e ) {
|
|
// Translators: error message.
|
|
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param string $package_id .
|
|
* @param string $session_type .
|
|
* @param string $label_format .
|
|
* @param string $label_page_format .
|
|
* @param string $label_type .
|
|
* @return mixed .
|
|
* @throws Exception .
|
|
*/
|
|
public function get_label( $package_id, $session_type, $label_format, $label_page_format, $label_type ) {
|
|
return $this->get_labels_in_single_file( array( $package_id ), $session_type, $label_format, $label_page_format, $label_type );
|
|
}
|
|
|
|
/**
|
|
* Get labels for multiple packages in single file.
|
|
*
|
|
* @param string[] $packages .
|
|
* @param string $session_type .
|
|
* @param string $label_format .
|
|
* @param string $label_page_format .
|
|
* @param string $label_type .
|
|
*
|
|
* @return mixed
|
|
* @throws Exception .
|
|
*/
|
|
public function get_labels_in_single_file( array $packages, $session_type, $label_format, $label_page_format, $label_type ) {
|
|
$client = $this->get_client();
|
|
$params = array(
|
|
'authDataV1' => $this->auth_data->get_auth_data_v1(),
|
|
'dpdServicesParamsV1' => array(
|
|
'policy' => 'IGNORE_ERRORS',
|
|
'session' => array(
|
|
'sessionType' => $session_type,
|
|
),
|
|
),
|
|
'outputDocFormatV1' => $label_format,
|
|
'outputDocPageFormatV1' => $label_page_format,
|
|
'outputLabelType' => $label_type,
|
|
);
|
|
foreach ( $packages as $package_id ) {
|
|
$param_package = array( 'packageId' => $package_id );
|
|
$params['dpdServicesParamsV1']['session']['packages'][] = $param_package;
|
|
}
|
|
try {
|
|
$api_response = $client->generateSpedLabelsV4( $params );
|
|
if ( ! isset( $api_response->return, $api_response->return->documentData ) ) {
|
|
throw new \WPDesk\DPD\Exceptions\EmptyLabelException( __( 'API DPD zwróciło błędną odpowiedz: brak pliku etykiety. Skontaktuj się z DPD.', 'woocommerce-dpd' ) );
|
|
}
|
|
} catch ( \WPDesk\DPD\Exceptions\EmptyLabelException $ele ) {
|
|
throw $ele;
|
|
} catch ( Exception $e ) {
|
|
// Translators: exception message.
|
|
throw new Exception( sprintf( __( 'Komunikat API DPD: %1$s', 'woocommerce-dpd' ), $e->getMessage() ) );
|
|
}
|
|
return $api_response;
|
|
}
|
|
|
|
/**
|
|
* .
|
|
*
|
|
* @param stdClass $package .
|
|
* @return string
|
|
*/
|
|
public function parse_shipment_errors( $package ) {
|
|
$parsed_errors = '';
|
|
if ( isset( $package->return->Status ) && 'OK' !== $package->return->Status ) {
|
|
$parsed_errors = $this->translate_messages( $package->return->Status );
|
|
if ( isset( $package->return->Info ) ) {
|
|
$parsed_errors = $package->return->Info;
|
|
}
|
|
}
|
|
if ( isset( $package->return->Packages->Package->ValidationDetails ) ) {
|
|
if ( ! is_array( $package->return->Packages->Package->ValidationDetails->ValidationInfo ) ) {
|
|
$package->return->Packages->Package->ValidationDetails->ValidationInfo = array( $package->return->Packages->Package->ValidationDetails->ValidationInfo );
|
|
}
|
|
foreach ( $package->return->Packages->Package->ValidationDetails->ValidationInfo as $validation_info ) {
|
|
if ( '' !== $parsed_errors ) {
|
|
$parsed_errors .= ', ';
|
|
}
|
|
$parsed_errors .= $validation_info->Info; // phpcs:ignore
|
|
}
|
|
}
|
|
if ( isset( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails ) ) {
|
|
if ( ! is_array( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo ) ) {
|
|
$package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo = array( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo );
|
|
}
|
|
foreach ( $package->return->Packages->Package->Parcels->Parcel->ValidationDetails->ValidationInfo as $validation_info ) {
|
|
if ( '' !== $parsed_errors ) {
|
|
$parsed_errors .= ', ';
|
|
}
|
|
$parsed_errors .= $validation_info->Info; // phpcs:ignore
|
|
}
|
|
}
|
|
return $parsed_errors;
|
|
}
|
|
|
|
}
|