765 lines
17 KiB
PHP
765 lines
17 KiB
PHP
<?php
|
|
/**
|
|
* ShipX API.
|
|
*
|
|
* @package PaczkomatyInpost
|
|
*/
|
|
|
|
/**
|
|
* InPost ShipX API
|
|
* http://parcellockers.io/pages/viewpage.action?pageId=2621453
|
|
*/
|
|
class WPDesk_Paczkomaty_ShipX {
|
|
|
|
const API_URL = 'https://api-shipx-pl.easypack24.net/v1/';
|
|
|
|
const TEST_API_URL = 'https://sandbox-api-shipx-pl.easypack24.net/v1/';
|
|
|
|
const MAP_ENDPOINT_URL = 'https://api-pl-points.easypack24.net/v1';
|
|
|
|
const MAP_ENDPOINT_URL_SANDBOX = 'https://sandbox-api-pl-points.easypack24.net/v1';
|
|
|
|
const API_PER_PAGE = 500;
|
|
|
|
/**
|
|
* API URL.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $api_url = self::API_URL;
|
|
|
|
/**
|
|
* Test API URL.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $test_api_url = self::TEST_API_URL;
|
|
|
|
/**
|
|
* User agent.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $user_agent = 'WooCommerce Paczkomaty Inpost';
|
|
|
|
/**
|
|
* User agent version.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $user_agent_version = WOOCOMMERCE_PACZKOMATY_INPOST_VERSION;
|
|
|
|
/**
|
|
* Test mode.
|
|
*
|
|
* @var bool
|
|
*/
|
|
private $test_mode = false;
|
|
|
|
/**
|
|
* Token.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $token = '';
|
|
|
|
/**
|
|
* Test token.
|
|
*
|
|
* @var string
|
|
*/
|
|
private $test_token = '';
|
|
|
|
/**
|
|
* Request ID.
|
|
*
|
|
* @var null
|
|
*/
|
|
private $request_id = null;
|
|
|
|
/**
|
|
* Organization ID.
|
|
*
|
|
* @var int
|
|
*/
|
|
private $organization_id = 0;
|
|
|
|
/**
|
|
* Point types.
|
|
*
|
|
* @var array
|
|
*/
|
|
public $point_types = [
|
|
'parcel_locker' => 'pl',
|
|
'laundry_locker' => 'll',
|
|
'avizo_locker' => 'al',
|
|
'pop' => 'pop',
|
|
'pok' => 'pok',
|
|
'air_on_airport' => 'aa',
|
|
'air_outside_airport' => 'aoa',
|
|
'parcel_locker_only' => 'plo',
|
|
];
|
|
|
|
|
|
/**
|
|
* WPDesk_Paczkomaty_ShipX constructor.
|
|
*
|
|
* @param string $token .
|
|
* @param string $test_token .
|
|
* @param bool $test_mode .
|
|
*/
|
|
public function __construct( $token, $test_token = '', $test_mode = false ) {
|
|
$this->token = $token;
|
|
$this->test_token = $test_token;
|
|
$this->test_mode = $test_mode;
|
|
}
|
|
|
|
/**
|
|
* @param int $organization_id .
|
|
*/
|
|
public function set_organization_id( $organization_id ) {
|
|
$this->organization_id = $organization_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $token .
|
|
*/
|
|
public function set_token( $token ) {
|
|
$this->token = $token;
|
|
}
|
|
|
|
/**
|
|
* @param string $test_token .
|
|
*/
|
|
public function set_test_token( $test_token ) {
|
|
$this->test_token = $test_token;
|
|
}
|
|
|
|
/**
|
|
* @param bool $test_mode .
|
|
*/
|
|
public function set_test_mode( $test_mode ) {
|
|
$this->test_mode = $test_mode;
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
*/
|
|
public function get_test_mode() {
|
|
return $this->test_mode;
|
|
}
|
|
|
|
/**
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function ping() {
|
|
return $this->get_organizations();
|
|
}
|
|
|
|
/**
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_services() {
|
|
$params = [];
|
|
|
|
return $this->get( 'services', $params );
|
|
}
|
|
|
|
/**
|
|
* Retrieves status list with translation.
|
|
*
|
|
* @param string $target_locale .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
* @see https://docs.inpost24.com/display/PL/%5B1.2.0%5D+Statusy
|
|
*/
|
|
public function get_statuses( $target_locale ) {
|
|
$params = [ 'lang' => $target_locale ];
|
|
|
|
return $this->get( 'statuses', $params );
|
|
}
|
|
|
|
/**
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_sending_methods() {
|
|
$params = [];
|
|
|
|
return $this->get( 'sending_methods', $params );
|
|
}
|
|
|
|
/**
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_organizations() {
|
|
$params = $this->add_per_page_parameter( [] );
|
|
|
|
return $this->get( 'organizations', $params );
|
|
}
|
|
|
|
/**
|
|
* @param string $organization_id .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_organization( $organization_id ) {
|
|
return $this->get( 'organizations/' . $organization_id );
|
|
}
|
|
|
|
/**
|
|
* @param array $params .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_points( $params ) {
|
|
return $this->get( 'points', $params, true );
|
|
}
|
|
|
|
/**
|
|
* @param string $point_name .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_point( $point_name ) {
|
|
return $this->get( 'points/' . $point_name, [], true );
|
|
}
|
|
|
|
/**
|
|
* @param array $params .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_dispatch_points( $params ) {
|
|
return $this->get( 'organizations/' . $this->organization_id . '/dispatch_points', $params );
|
|
}
|
|
|
|
/**
|
|
* @param string $point_id .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_dispatch_point( $point_id ) {
|
|
return $this->get( 'dispatch_points/' . $point_id, [] );
|
|
}
|
|
|
|
/**
|
|
* @param array $shipment_data .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function create_shipment( $shipment_data ) {
|
|
return $this->post( 'organizations/' . $this->organization_id . '/shipments', [], $shipment_data );
|
|
}
|
|
|
|
/**
|
|
* @param string $shipment_id .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_shipment( $shipment_id ) {
|
|
return $this->get( 'shipments/' . $shipment_id, [] );
|
|
}
|
|
|
|
/**
|
|
* @param string $shipment_id .
|
|
*
|
|
* @return array|bool|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function delete_shipment( $shipment_id ) {
|
|
return $this->delete( 'shipments/' . $shipment_id, [] );
|
|
}
|
|
|
|
/**
|
|
* @param array|string $shipment_ids .
|
|
* @param string $format .
|
|
* @param string $type .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_shipments_labels( $shipment_ids, $format = 'Pdf', $type = 'normal' ) {
|
|
if ( ! is_array( $shipment_ids ) ) {
|
|
$shipment_ids = [ $shipment_ids ];
|
|
}
|
|
$post_data = new stdClass();
|
|
$post_data->shipment_ids = $shipment_ids;
|
|
$post_data->format = $format;
|
|
$post_data->type = $type;
|
|
|
|
return $this->get_labels( 'organizations/' . $this->organization_id . '/shipments/labels', [], $post_data );
|
|
}
|
|
|
|
/**
|
|
* @param array $dispatch_order_data .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function create_dispatch_order( $dispatch_order_data ) {
|
|
return $this->post( 'organizations/' . $this->organization_id . '/dispatch_orders', [], $dispatch_order_data );
|
|
}
|
|
|
|
/**
|
|
* @param string $dispatch_order_id .
|
|
*
|
|
* @return array|bool|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function delete_dispatch_order( $dispatch_order_id ) {
|
|
return $this->delete( 'dispatch_orders/' . $dispatch_order_id, [] );
|
|
}
|
|
|
|
/**
|
|
* @param string $dispatch_order_id .
|
|
* @param string $format .
|
|
*
|
|
* @return array|bool|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_dispatch_order_printout( $dispatch_order_id, $format = 'Pdf' ) {
|
|
$api_method = 'dispatch_orders/' . $dispatch_order_id . '/printout?format=Pdf';
|
|
$url = $this->build_url( $api_method );
|
|
$url = $this->add_params_to_url( $url, [] );
|
|
$args = $this->prepare_args( 'GET' );
|
|
|
|
$response = wp_remote_get( $url, $args );
|
|
|
|
$this->check_response( $response );
|
|
|
|
if ( (int) wp_remote_retrieve_response_code( $response ) === 200 ) {
|
|
return $response['body'];
|
|
}
|
|
|
|
return $this->parse_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @param string $api_method .
|
|
* @param array $params .
|
|
* @param bool $no_auth .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get( $api_method, $params = [], $no_auth = false ) {
|
|
$url = $this->build_url( $api_method );
|
|
$url = $this->add_params_to_url( $url, $params );
|
|
$args = $this->prepare_args( 'GET' );
|
|
|
|
if ( true === $no_auth ) {
|
|
unset( $args['headers']['Authorization'] );
|
|
}
|
|
$response = wp_remote_get( $url, $args );
|
|
$this->check_response( $response );
|
|
|
|
return $this->parse_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @param string $api_method .
|
|
* @param array $params .
|
|
* @param array $data .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function post( $api_method, $params = [], $data = [] ) {
|
|
$url = $this->build_url( $api_method );
|
|
$url = $this->add_params_to_url( $url, $params );
|
|
|
|
$args = $this->prepare_args( 'POST' );
|
|
$args['body'] = json_encode( $data, JSON_PRETTY_PRINT );
|
|
$this->log( 'ShipX API POST args:', $args );
|
|
|
|
$response = wp_remote_post( $url, $args );
|
|
$this->log( 'ShipX API response:', $response );
|
|
$this->check_response( $response );
|
|
|
|
return $this->parse_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @param string $api_method .
|
|
* @param array $params .
|
|
* @param array $data .
|
|
*
|
|
* @return array|bool|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function delete( $api_method, $params = [], $data = [] ) {
|
|
$url = $this->build_url( $api_method );
|
|
$url = $this->add_params_to_url( $url, $params );
|
|
|
|
$args = $this->prepare_args( 'POST' );
|
|
$args['body'] = json_encode( $data );
|
|
$args['method'] = 'DELETE';
|
|
|
|
$response = wp_remote_post( $url, $args );
|
|
|
|
$this->check_response( $response );
|
|
|
|
if ( 204 === intval( $response['response']['code'] ) ) {
|
|
return true;
|
|
}
|
|
|
|
return $this->parse_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @param string $api_method .
|
|
* @param array $params .
|
|
* @param array $data .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws Exception .
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function get_labels( $api_method, $params = [], $data = [] ) {
|
|
$url = $this->build_url( $api_method );
|
|
$url = $this->add_params_to_url( $url, $params );
|
|
|
|
$args = $this->prepare_args( 'POST' );
|
|
$args['body'] = json_encode( $data );
|
|
|
|
$response = wp_remote_post( $url, $args );
|
|
$this->check_response( $response );
|
|
|
|
if ( (int) wp_remote_retrieve_response_code( $response ) === 200 ) {
|
|
return $response['body'];
|
|
}
|
|
|
|
return $this->parse_response( $response );
|
|
}
|
|
|
|
/**
|
|
* @param string $method .
|
|
*
|
|
* @return array
|
|
*/
|
|
public function prepare_args( $method ) {
|
|
$args = $this->create_args( $method );
|
|
$headers = [];
|
|
$headers = $this->add_headers( $headers );
|
|
$args['headers'] = $headers;
|
|
|
|
return $args;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_headers( array $headers ) {
|
|
$headers = $this->add_header_token( $headers );
|
|
$headers = $this->add_header_request_id( $headers );
|
|
$headers = $this->add_header_user_agent( $headers );
|
|
$headers = $this->add_header_user_agent_version( $headers );
|
|
$headers = $this->add_header_content_type( $headers );
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_header_token( array $headers ) {
|
|
$headers['Authorization'] = $this->test_mode ? 'Bearer ' . $this->test_token : 'Bearer ' . $this->token;
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_header_request_id( array $headers ) {
|
|
if ( null !== $this->request_id ) {
|
|
$headers['X-Request-ID'] = $this->request_id;
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_header_user_agent( array $headers ) {
|
|
$headers['X-User-Agent'] = $this->user_agent;
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_header_user_agent_version( array $headers ) {
|
|
$headers['X-User-Agent-Version'] = $this->user_agent_version;
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param array $headers .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function add_header_content_type( array $headers ) {
|
|
$headers['Content-Type'] = 'application/json';
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* @param int $request_id .
|
|
*/
|
|
public function set_request_id( $request_id ) {
|
|
$this->request_id = $request_id;
|
|
}
|
|
|
|
/**
|
|
* @param string $api_method .
|
|
*
|
|
* @return string
|
|
*/
|
|
private function build_url( $api_method ) {
|
|
if ( $this->test_mode ) {
|
|
$url = $this->test_api_url . $api_method;
|
|
} else {
|
|
$url = $this->api_url . $api_method;
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
/**
|
|
* @param string $url .
|
|
* @param array $params .
|
|
*
|
|
* @return string
|
|
*/
|
|
private function add_params_to_url( $url, $params ) {
|
|
if ( is_array( $params ) ) {
|
|
foreach ( $params as $key => $value ) {
|
|
$url = add_query_arg( $key, $value, $url );
|
|
}
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
/**
|
|
* Add per page parameter.
|
|
*
|
|
* @param array $params .
|
|
*
|
|
* @return array
|
|
*/
|
|
public function add_per_page_parameter( array $params ) {
|
|
$params['per_page'] = self::API_PER_PAGE;
|
|
|
|
return $params;
|
|
}
|
|
|
|
/**
|
|
* @param string $method .
|
|
*
|
|
* @return array
|
|
*/
|
|
private function create_args( $method = 'GET' ) {
|
|
$args = [
|
|
'method' => $method,
|
|
'timeout' => 30,
|
|
'redirection' => 10,
|
|
'httpversion' => '1.0',
|
|
'blocking' => true,
|
|
'cookies' => [],
|
|
'body' => null,
|
|
'compress' => false,
|
|
'decompress' => true,
|
|
'sslverify' => false,
|
|
'stream' => false,
|
|
'filename' => null,
|
|
];
|
|
|
|
return $args;
|
|
}
|
|
|
|
/**
|
|
* @param array $response .
|
|
*
|
|
* @throws Exception .
|
|
*/
|
|
private function check_response( $response ) {
|
|
if ( is_wp_error( $response ) ) {
|
|
throw new Exception( $response->get_error_message() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse response.
|
|
*
|
|
* @param array $response Response .
|
|
*
|
|
* @return array|mixed|object
|
|
* @throws WPDesk_Paczkomaty_ShipX_Exception .
|
|
*/
|
|
public function parse_response( $response ) {
|
|
$api_response_object = json_decode( $response['body'] );
|
|
if ( is_array( $api_response_object ) ) {
|
|
return $api_response_object;
|
|
}
|
|
if ( ! is_object( $api_response_object ) ) {
|
|
throw new WPDesk_Paczkomaty_ShipX_Exception(
|
|
sprintf(
|
|
// Translators: response.
|
|
__(
|
|
'Błędna odpowiedź API shipX - spodziewany obiekt! Otrzymana odpowiedż serwera: %1$s',
|
|
'woocommerce-paczkomaty-inpost'
|
|
),
|
|
esc_html( $response['body'] )
|
|
),
|
|
$response
|
|
);
|
|
}
|
|
if ( isset( $api_response_object->error ) ) {
|
|
throw new WPDesk_Paczkomaty_ShipX_Exception( $this->parse_error_message( $api_response_object ), $response );
|
|
}
|
|
|
|
return $api_response_object;
|
|
}
|
|
|
|
/**
|
|
* @param stdClass $api_response_object .
|
|
*
|
|
* @return string
|
|
*/
|
|
private function parse_error_message( $api_response_object ) {
|
|
if ( isset( $api_response_object->message ) ) {
|
|
if ( isset( $api_response_object->details ) ) {
|
|
if ( ! is_array( $api_response_object->details ) ) {
|
|
$api_response_object->details = [ $api_response_object->details ];
|
|
}
|
|
$message = '';
|
|
foreach ( $api_response_object->details as $key => $detail ) {
|
|
if ( '' !== $message ) {
|
|
$message .= ', ';
|
|
}
|
|
$error_detail = $this->parse_error_detail( $detail, $message );
|
|
if ( $error_detail ) {
|
|
$message .= ( $key + 1 ) . ': ' . $error_detail;
|
|
}
|
|
}
|
|
if ( $message ) {
|
|
$message = $api_response_object->message . '<br/><br/>' . __( 'Szczegóły:', 'woocommerce-paczkomaty-inpost' ) . ' ' . $message;
|
|
} else {
|
|
$message = $api_response_object->message;
|
|
}
|
|
} else {
|
|
$message = $api_response_object->message;
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
return $api_response_object->error;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $error_detail .
|
|
* @param string $message .
|
|
*
|
|
* @return string
|
|
* Example error_detail value:
|
|
* stdClass Object
|
|
* (
|
|
* [receiver] => Array
|
|
* (
|
|
* [0] => stdClass Object
|
|
* (
|
|
* [address] => Array
|
|
* (
|
|
* [0] => stdClass Object
|
|
* (
|
|
* [building_number] => Array
|
|
* (
|
|
* [0] => required
|
|
* )
|
|
* )
|
|
* )
|
|
* )
|
|
* )
|
|
* )
|
|
*/
|
|
private function parse_error_detail( $error_detail, $message = '' ) {
|
|
if ( is_object( $error_detail ) ) {
|
|
foreach ( $error_detail as $key => $detail ) {
|
|
$message .= ' ' . $key . ': ' . $this->parse_error_detail( $detail );
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
if ( is_array( $error_detail ) ) {
|
|
foreach ( $error_detail as $key => $detail ) {
|
|
$message .= $this->parse_error_detail( $detail );
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
return $error_detail . ';';
|
|
}
|
|
|
|
/**
|
|
* @param string $message .
|
|
* @param mixed $data .
|
|
*/
|
|
public function log( $message, $data = null ) {
|
|
if ( isset( $data ) ) {
|
|
$message .= ' ';
|
|
$message .= print_r( $data, true );
|
|
}
|
|
do_action( 'paczkomaty_shipping_method_log', $message );
|
|
}
|
|
|
|
}
|