* @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 = ''; } $sub = is_array($item) ? $this->parseErrors($item) : $item; if (strpos($sub, '$key: $sub" : "$sub"; $output .= PHP_EOL; } $output .= ''; if (strpos($output, 'organizationId.'/shipments'; $result = $this->curl($url, array(), 'GET', false); if ($result !== false) { $resultData = json_decode($result, true); } return $resultData; } public function payByPack($id_pack) { $resultData = false; $this->getShipments(); $url = self::$shipxUrl.'/v1/shipments/'.(int)$id_pack.'/buy'; $result = $this->curl($url, array(), 'POST', false); if ($result !== false) { $resultData = json_decode($result, true); } return $resultData; } public function curl($url, $post, $method = 'POST', $return_array = true) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $this->getAuthToken())); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); if (is_array($post) && sizeof($post)) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post)); } $result = curl_exec($curl); curl_close($curl); if ($result === false) { return false; } if (Configuration::get('PMINPOSTPACZKOMATY_LOG')) { file_put_contents(dirname(__FILE__).'/../log/log.txt', date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND); file_put_contents(dirname(__FILE__).'/../log/log.txt', print_r($post, true), FILE_APPEND); file_put_contents(dirname(__FILE__).'/../log/log.txt', print_r(json_decode($result), true), FILE_APPEND); } if ($return_array) { return json_decode($result, true); } else { return $result; } } public function downloadLabel($id_package, $format = false) { $post = array(); if ($format !== false) { self::$label_format = $format; } if (self::$label_format == 'Pdf') { $url = self::$shipxUrl.'/v1/shipments/'.$id_package.'/label?format='.self::$label_format.'&type='.self::$label_type; } else { $url = self::$shipxUrl.'/v1/shipments/'.$id_package.'/label?format='.self::$label_format; } return $this->curl($url, $post, 'GET', false); } public function getTrackingNumber($package_id) { sleep(1); for ($i = 0; $i <= $this->api_max_calls; $i++) { $url = self::$shipxUrl.'/v1/shipments/'.$package_id; $result = $this->curl($url, false, 'GET'); if ($result['status'] == 404 || (isset($result['transactions'], $result['transactions'][0], $result['transactions'][0]['details']) && isset($result['transactions'][0]['details']['message']))) { if ($i == $this->api_max_calls) { self::$errors[] = isset($result['message']) ? $result['message'] : $result['transactions'][0]['details']['message']; return false; } } elseif ($result['status'] == 'offer_selected') { if ($i == $this->api_max_calls) { self::$errors[] = 'offered'; return false; } } elseif (!isset($result['tracking_number']) || $result['tracking_number'] == '') { if ($i == $this->api_max_calls) { self::$errors[] = 'trackempty'; return false; } } else { return $result['tracking_number']; } sleep(0.5); } return false; } public function getSticker($numer_listu, $format = false) { if ($numer_listu == '') { self::$errors[] = 'Nie udało się pobrać listu przewozowego'; return false; } $sql = 'SELECT id_pack FROM `'._DB_PREFIX_.'pminpostpaczkomatylist` WHERE nr_listu = "'.$numer_listu.'"'; $value = Db::getInstance()->getValue($sql); if ($value === false) { return false; } else { return $this->downloadLabel($value, $format); } } public function checkConnections() { $url = self::$shipxUrl.'/v1/organizations'; return $this->curl($url, array(), 'GET', true); } public function getPackStatus($packcode) { $url = self::$shipxUrl.'/v1/tracking/'.$packcode; return $this->curl($url, array(), 'GET', false); } public function getZlecenia() { return $this->curl(self::$shipxUrl.'/v1/organizations/'.$this->organizationId.'/dispatch_orders', array(), 'GET'); } public function zlecenie($ids) { $shp = array(); $shp['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' ); $shp['shipments'] = $ids; $result = $this->curl( self::$shipxUrl.'/v1/organizations/'.$this->organizationId.'/dispatch_orders', $shp ); if ($result['status'] == 400 || $result['status'] == 401) { self::$errors[] = $result['message']; if (isset($result['details'])) { self::$errors[] = $this->parseErrors($result['details']); } return false; } return $result; } public function printZlecenie($id_pack) { if (file_exists(dirname(__FILE__).'/../cache/'.$id_pack.'.pdf')) { header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="'.$id_pack.'.pdf"'); $result =Tools::file_get_contents(dirname(__FILE__).'/../cache/'.$id_pack.'.pdf'); die($result); } $result = $this->curl( self::$shipxUrl.'/v1/organizations/'.$this->organizationId.'/dispatch_orders/printouts?'. 'dispatch_order_id='.$id_pack.'&format=Pdf', array(), 'GET', false ); if (strlen($result) < 300) { $module = Module::getInstanceByName('pminpostpaczkomaty'); $result_array = json_decode($result, true); $error = ''; if (isset($result_array['message'])) { $error = $result_array['message'].' '.$result_array['details']; $error = '
'.$module->translateText($error); } return 'Nie udało się pobrać zlecenia transportowego. '.$error; } header('Content-Type: application/pdf'); header('Content-Disposition: inline; filename="'.$id_pack.'.pdf"'); file_put_contents(dirname(__FILE__).'/../cache/'.$id_pack.'.pdf', $result); die($result); } public function setLabelType($type) { self::$label_type = $type == 'A6P' ? 'A6' : 'normal'; } private function getAuthToken() { return 'Authorization: Bearer '.$this->tokenShipx; } }