* @copyright 2014-2023 Presta-Mod.pl * @license Licecnja na jedną domenę * Presta-Mod.pl Rafał Zontek */ class InpostApiShipx extends InpostApi { private $api_max_calls = 10; private $organizationId = ''; private $tokenShipx = ''; protected static $errors; protected static $instance; public static $shipxUrl = 'https://api-shipx-pl.easypack24.net'; public function __construct() { self::$errors = array(); $this->organizationId = Configuration::get('PMINPOSTPACZKOMATY_ID'); $this->tokenShipx = Configuration::get('PMINPOSTPACZKOMATY_TOKEN'); self::$label_format = Configuration::get('PMINPOSTPACZKOMATY_LABEL_FORMAT'); self::$label_type = Configuration::get('PMINPOSTPACZKOMATY_LABEL_SIZE') == 'A6P' ? 'A6' : 'normal'; if (Tools::isSubmit('format') && Tools::isSubmit('size_l') && Tools::getValue('format') != 'undefined' && Tools::getValue('size_l') != 'undefined') { self::$label_format = Tools::getValue('format'); self::$label_type = Tools::getValue('size_l') == 'A6P' ? 'A6' : 'normal'; } } public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new InpostApiShipx(); } return self::$instance; } public function createList($params) { $customer_email = $params['customer_email']; $phone = $params['customer_phone']; $machine = isset($params['selected_machine']) ? $params['selected_machine'] : ''; $package_size = isset($params['package_size']) ? $params['package_size'] : ''; $innurance_amount = isset($params['insurance_ammount']) ? $params['insurance_ammount'] : ''; $cod = isset($params['cod_ammount']) ? $params['cod_ammount'] : ''; $sender_machine = isset($params['sender_machine']) ? $params['sender_machine'] : ''; $package_reference = isset($params['package_reference']) ? $params['package_reference'] : ''; $enf_of_week = isset($params['end_of_week_collection']) ? $params['end_of_week_collection'] : 'false'; $mpk = Configuration::get('PMINPOSTPACZKOMATY_MPK'); if ($innurance_amount > 0) { $ins = true; } else { $ins = false; } if (strpos($customer_email, '@user.allegrogroup.pl') !== false || strpos($customer_email, '@allegromail.pl') !== false || strpos($customer_email, '@user.allegromail.pl') !== false ) { $service = 'inpost_locker_allegro_smart'; } else { $service = 'inpost_locker_standard'; } switch ($package_size) { case 'A': $package_size = 'small'; break; case 'B': $package_size = 'medium'; break; case 'C': $package_size = 'large'; break; } $parcels = array(); $parcels[] = array( 'id' => 'small', 'template' => $package_size, 'tracking_number' => null, 'is_non_standard' => false ); $inpostData = array( 'receiver' => array('email' => $customer_email, 'phone' => $phone, 'name' => ''), 'sender' => array( 'name' => 'Nazwa', 'company_name' => Configuration::get('PMINPOSTPACZKOMATY_COMPANY_NAME'), 'first_name' => Configuration::get('PMINPOSTPACZKOMATY_FIRST_NAME'), 'last_name' => Configuration::get('PMINPOSTPACZKOMATY_LAST_NAME'), 'email' => Configuration::get('PMINPOSTPACZKOMATY_EMAIL'), 'phone' => preg_replace("/[^0-9]/", "", Configuration::get('PMINPOSTPACZKOMATY_PHONE')), 'address' => array( 'street' => Configuration::get('PMINPOSTPACZKOMATY_STREET'), 'building_number' => Configuration::get('PMINPOSTPACZKOMATY_BUILDING_NUMBER'), 'city' => Configuration::get('PMINPOSTPACZKOMATY_CITY'), 'post_code' => Configuration::get('PMINPOSTPACZKOMATY_POST_CODE'), 'country_code' => 'PL' ) ), 'cod' => array('amount' => $cod, 'currency' => 'PLN'), 'insurance' => array('amount' => $innurance_amount,'currency' => 'PLN'), 'mpk' => $mpk, 'reference' => $package_reference, 'parcels' => $parcels, 'service' => $service, 'end_of_week_collection' => $enf_of_week, 'only_choice_of_offer' => false, 'custom_attributes' => array('target_point' => $machine,'dropoff_point' => $sender_machine) ); if ($mpk == '') { unset($inpostData['mpk']); } if ($cod == 0) { unset($inpostData['cod']); } if (!$ins || (float)$innurance_amount == 0) { unset($inpostData['insurance']); } if (!is_null($sender_machine) && $sender_machine != '') { $inpostData['custom_attributes']['sending_method'] = 'parcel_locker'; } if ($service == 'inpost_locker_allegro_smart' && !isset($inpostData['custom_attributes']['sending_method'])) { $inpostData['custom_attributes']['sending_method'] = 'dispatch_order'; } if (Configuration::get('PMINPOSTPACZKOMATY_SHIPPING_METHOD') && $sender_machine == '') { $inpostData['custom_attributes']['sending_method'] = 'pop'; } if (isset($params['pminpostorder_machine']) && $params['pminpostorder_machine'] == 0) { // Kurier $inpostData['custom_attributes']['sending_method'] = 'dispatch_order'; if (isset($inpostData['custom_attributes']['dropoff_point'])) { unset($inpostData['custom_attributes']['dropoff_point']); } } elseif (isset($params['pminpostorder_machine']) && $params['pminpostorder_machine'] == 1) { // Paczkomat $inpostData['custom_attributes']['sending_method'] = 'parcel_locker'; } elseif (isset($params['pminpostorder_machine']) && $params['pminpostorder_machine'] == 2) { // POP $inpostData['custom_attributes']['sending_method'] = 'pop'; if (isset($inpostData['custom_attributes']['dropoff_point'])) { unset($inpostData['custom_attributes']['dropoff_point']); } } $result = $this->curl(self::$shipxUrl.'/v1/organizations/'.$this->organizationId.'/shipments', $inpostData); if ($result['status'] == 400 || $result['status'] == 401) { self::$errors[] = $result['message']; if (isset($result['details'])) { self::$errors[] = $this->parseErrors($result['details']); // print_r($result['details']); // self::$errors[] = die($this->parseErrors($result['details'])); } return false; } elseif (isset($result['message']) || isset($result['details'])) { if (isset($result['message'])) { self::$errors[] = $result['message']; } if (isset($result['details'])) { self::$errors[] = $this->parseErrors($result['details']); } return false; } return $result; } public function parseErrors($details) { $output = ''; $end_output = ''; foreach ($details as $key => $item) { if (!is_numeric($key) && $output == '') { $output = '