From ce6b67d6ff7e5dc81aed7613eab9fe1f64450d46 Mon Sep 17 00:00:00 2001 From: Joeri van Veen Date: Wed, 20 Mar 2024 09:19:57 +0100 Subject: [PATCH] fix: make compatible with php 8.2 (#17) --- install.xml | 2 +- .../en-gb/extension/module/myparcelnl.php | 2 +- .../nl-nl/extension/module/myparcelnl.php | 2 +- .../library/myparcelnl/assets/js/order.js | 2 - .../library/myparcelnl/class_myparcel.php | 6 + .../includes/class_myparcel_api.php | 121 ++++++++++-------- .../includes/class_myparcel_curl.php | 12 +- .../includes/class_myparcel_shipment.php | 46 ++++--- .../class_myparcel_shipment_checkout.php | 34 +---- .../class_myparcel_shipment_helper.php | 16 ++- .../library/myparcelnl/myparcel.ocmod.xml | 2 +- 11 files changed, 126 insertions(+), 119 deletions(-) diff --git a/install.xml b/install.xml index 643a8c2..ba1b9fd 100644 --- a/install.xml +++ b/install.xml @@ -5,7 +5,7 @@ --> MyParcel NL - 1.1.8 + 1.1.17 MyParcelNL MyParcel NL https://www.myparcel.nl/ diff --git a/upload/admin/language/en-gb/extension/module/myparcelnl.php b/upload/admin/language/en-gb/extension/module/myparcelnl.php index f8a5ddf..12c2c3a 100644 --- a/upload/admin/language/en-gb/extension/module/myparcelnl.php +++ b/upload/admin/language/en-gb/extension/module/myparcelnl.php @@ -1,6 +1,6 @@ array ( @@ -166,55 +166,64 @@ public function getShipmentLabels ( $ids, $params = array(), $return = 'pdf' ) return $response; } - /** - * Track shipments - * @param array $ids shipment ids - * @param array $params request parameters - * @return array response - */ - public function getTracktraces ( $ids, $params = array() ) - { - $endpoint = 'tracktraces'; - - $headers = array ( - 'Authorization: basic '. base64_encode("{$this->key}"), - ); + /** + * Track shipments + * @param array $ids shipment ids + * @param array $params request parameters + * @return array response + */ + public function getTracktraces ( $ids, $params = array() ) + { + $endpoint = 'tracktraces'; - $request_url = MyParcel()->helper->add_query_arg( $params, $this->api_domain . $endpoint . '/' . implode(';', $ids) ); - $response = $this->get($request_url, $headers, false); + $headers = array ( + 'Authorization: basic '. base64_encode("{$this->key}"), + ); - return $response; - } + $request_url = MyParcel()->helper->add_query_arg( $params, $this->api_domain . $endpoint . '/' . implode(';', $ids) ); + $response = $this->sendRequest($request_url, 'GET', null, $headers); + return $response; + } - public function getTracktraceUrl( $order_id, $tracktrace ) - { - if (empty($order_id)) - return; - $registry = MyParcel::$registry; + public function getTracktraceUrl( $order_id, $tracktrace ) + { + if (empty($order_id)) + return; + $registry = MyParcel::$registry; $loader = $registry->get('load'); - $loader->model(MyParcel()->getModelPath('shipment')); - $model_shipment = $registry->get('model_extension_myparcelnl_shipment'); + $loader->model(MyParcel()->getModelPath('shipment')); + $model_shipment = $registry->get('model_extension_myparcelnl_shipment'); - $order_info = MyParcel()->helper->getShippingOrder($order_id); - $country = $order_info['shipping_iso_code_2']; - $postcode = $order_info['shipping_postcode']; - // set url for NL or foreign orders - $shipment_helper = MyParcel()->shipment->shipment_helper; - $is_pickup = $shipment_helper->isPickup( $order_id ); - if ($country == 'NL') { - // use billing postcode for pickup/pakjegemak - if ( $is_pickup ) { - $postcode = $order_info['payment_postcode'];; - } - // $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Inbox/Search?lang=nl&B=%s&P=%s', $tracktrace, $postcode); - $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s', $tracktrace, $postcode); - } else { - $tracktrace_url = sprintf('https://www.internationalparceltracking.com/Main.aspx#/track/%s/%s/%s', $tracktrace, $country, $postcode); - } + $shipment_data = $model_shipment->getSavedMyParcelShipments($order_id); + $shipment_data = array_shift($shipment_data); + $response = $this->getTracktraces([$shipment_data['shipment_id']]); - return $tracktrace_url; - } + if ($response['code'] === 200) { + $tracktrace = array_shift($response['body']['data']['tracktraces']); + return $tracktrace['link_consumer_portal']; + } + + $order_info = MyParcel()->helper->getShippingOrder($order_id); + $country = $order_info['shipping_iso_code_2']; + $postcode = $order_info['shipping_postcode']; + // set url for NL or foreign orders + $shipment_helper = MyParcel()->shipment->shipment_helper; + $is_pickup = $shipment_helper->isPickup( $order_id ); + if ($country == 'NL') { + // use billing postcode for pickup/pakjegemak + if ( $is_pickup ) { + $postcode = $order_info['payment_postcode'];; + } + // $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Inbox/Search?lang=nl&B=%s&P=%s', $tracktrace, $postcode); + // $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s', $tracktrace, $postcode); + $tracktrace_url = sprintf('https://jouw.postnl.nl/track-and-trace/%s-NL-%s?language=nl', $tracktrace, $postcode); + } else { + $tracktrace_url = sprintf('https://www.internationalparceltracking.com/Main.aspx#/track/%s/%s/%s', $tracktrace, $country, $postcode); + } + + return $tracktrace_url; + } public function getTracktraceLinks ( $order_id ) { diff --git a/upload/system/library/myparcelnl/includes/class_myparcel_curl.php b/upload/system/library/myparcelnl/includes/class_myparcel_curl.php index 786edf5..dfbeba4 100644 --- a/upload/system/library/myparcelnl/includes/class_myparcel_curl.php +++ b/upload/system/library/myparcelnl/includes/class_myparcel_curl.php @@ -56,9 +56,11 @@ public function sendRequest($url, $method = 'GET', $post_data = null, $headers = $this->setOption(CURLOPT_URL, $url); - if ((ini_get('open_basedir') == '') AND (!ini_get('safe_mode'))) { - $this->setOption(CURLOPT_FOLLOWLOCATION, true); - } + // only in use for php 5.4 and lower so no need updated by Vanest ict + + // if ((ini_get('open_basedir') == '') && (!ini_get('safe_mode'))) { + // $this->setOption(CURLOPT_FOLLOWLOCATION, true); + // } switch ($method) { case "PUT": @@ -88,12 +90,12 @@ public function sendRequest($url, $method = 'GET', $post_data = null, $headers = $this->_curl = curl_init(); - if (!is_resource($this->_curl) || !isset($this->_curl)) { + if (!($this->_curl instanceof CurlHandle) && (!is_resource($this->_curl) || !isset($this->_curl))) { throw new Exception("Unable to create cURL session"); } $result_setopt = curl_setopt_array($this->_curl, $this->getOptions()); - if ($result_setopt !== TRUE) { + if ($result_setopt !== true) { throw new Exception(curl_error($this->_curl)); } diff --git a/upload/system/library/myparcelnl/includes/class_myparcel_shipment.php b/upload/system/library/myparcelnl/includes/class_myparcel_shipment.php index 3ed5a2e..71b29ea 100644 --- a/upload/system/library/myparcelnl/includes/class_myparcel_shipment.php +++ b/upload/system/library/myparcelnl/includes/class_myparcel_shipment.php @@ -60,6 +60,18 @@ function add($params, $process = false) $this->errors[] = 'The maximum weight of a shipment to a rest of world country cannot be heavier than 20 kg'; continue; } + $saved_extra_settings = $model_shipment->getSavedExtraExportSettings($order_id); + $number_of_copies = (!empty($saved_extra_settings['number_of_copies']) ? $saved_extra_settings['number_of_copies'] : 1); + if($number_of_copies > 1 && $shipment_helper->checkMultiCollo($shipment_data_single['recipient']['cc'],$shipment_data_single['options']['package_type'])){ + $secondary_shipments = array (); + + for ($i = 0; $i < $number_of_copies - 1; $i++){ + $secondary_shipments[] = array ( + 'reference_identifier' => $shipment_data_single['reference_identifier'], + ); + } + $shipment_data_single['secondary_shipments'] = $secondary_shipments; + } $shipment_data[] = $shipment_data_single; } } @@ -68,19 +80,13 @@ function add($params, $process = false) // And receive shipment_id from the API $responses = array(); if (!empty($shipment_data)) { - $saved_extra_settings = $model_shipment->getSavedExtraExportSettings($order_id); - $number_of_copies = (!empty($saved_extra_settings['number_of_copies']) ? $saved_extra_settings['number_of_copies'] : 1); - - for ($i = 1; $i <= intval($number_of_copies); $i++) { - try { - MyParcel()->log->add("Prepared data:\n".var_export($shipment_data, true)); - $response = $api->addShipments($shipment_data); - $responses[] = $response; - MyParcel()->log->add("API response (order {$order_id}):\n" . var_export($response, true)); - } catch (Exception $e) { - $this->errors[] = sprintf(MyParcel()->lang->get('entry_api_error_with_order_id'), $order_id) . $e->getMessage(); - } - + try { + MyParcel()->log->add("Prepared data:\n".var_export($shipment_data, true)); + $response = $api->addShipments($shipment_data); + $responses[] = $response; + MyParcel()->log->add("API response (order {$order_id}):\n" . var_export($response, true)); + } catch (Exception $e) { + $this->errors[] = sprintf(MyParcel()->lang->get('entry_api_error_with_order_id'), $order_id) . $e->getMessage(); } } @@ -127,11 +133,15 @@ function add($params, $process = false) $order_status_data = $model_order_status->getOrderStatus((int)MyParcel()->settings->general->automatic_order_status); if (!empty($order_status_data)) { if (version_compare(VERSION, '2.0.0.0', '>=')) { - $order_info = $model_model_sale_order->getOrder($order_id); - $model_helper->addDeliveryDataIntoOrder($order_info); - $shipment_helper->updateOrderStatus($order_id,(int)MyParcel()->settings->general->automatic_order_status, MyParcel()->lang->get('mssg_order_status_changed_by_myparcel')); - $new_order_status_name = !empty($order_status_data['name']) ? $order_status_data['name'] : null; - + /** @var MyParcel_Api $api * */ + $api = MyParcel()->api; + $response = $api->getLocalRequest('extension/myparcelnl/myparcel_order/updateorderstatus', array('order_id' => $order_id)); + if (empty($response['body']['success'])) { + $this->errors[] = MyParcel()->lang->get('entry_update_order_status_error') . ' - Order #' . $order_id; + MyParcel()->log->add('Update status error - Order #' . $order_id); + } else { + $new_order_status_name = !empty($order_status_data['name']) ? $order_status_data['name'] : null; + } } else { $data = array( 'order_status_id' => MyParcel()->settings->general->automatic_order_status, diff --git a/upload/system/library/myparcelnl/includes/class_myparcel_shipment_checkout.php b/upload/system/library/myparcelnl/includes/class_myparcel_shipment_checkout.php index a6daa85..293da5b 100644 --- a/upload/system/library/myparcelnl/includes/class_myparcel_shipment_checkout.php +++ b/upload/system/library/myparcelnl/includes/class_myparcel_shipment_checkout.php @@ -438,26 +438,6 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix = ); } - function getTaxFromCart(){ - $registry = MyParcel::$registry; - /** @var \Cart\Cart $cart **/ - $cart = $registry->get('cart'); - - /** @var Cart\Tax $tax **/ - $tax = $cart->tax; - if (!$tax || !$cart) { - return ''; - } - - foreach ($cart->getProducts() as $product) { - if ($product['tax_class_id']) { - return $product['tax_class_id']; - } - } - return ''; - - } - /** * Converts price string to float value, assuming no thousand-separators used * @param float $price @@ -480,23 +460,13 @@ function convertPriceToFloat( $price ) function getTotalDeliveryTaxAmountFromCart($delivery_fee, $cart) { /** @var Cart\Tax $tax **/ - $tax = $cart->tax; + $taxes = $cart->getTaxes(); $total = $delivery_fee; - if (!$tax || !$cart) { + if (!$taxes || !$cart) { return $total; } - $taxes = array(); - foreach ($cart->getProducts() as $product) { - if ($product['tax_class_id']) { - // Check if tax_class_id takes affect to current shipping address - if (!in_array($product['tax_class_id'], $taxes)) { - $taxes[] = $product['tax_class_id']; - } - } - } - if (empty($taxes)) { return $delivery_fee; } else { diff --git a/upload/system/library/myparcelnl/includes/class_myparcel_shipment_helper.php b/upload/system/library/myparcelnl/includes/class_myparcel_shipment_helper.php index a71e5d4..cf2d7ec 100644 --- a/upload/system/library/myparcelnl/includes/class_myparcel_shipment_helper.php +++ b/upload/system/library/myparcelnl/includes/class_myparcel_shipment_helper.php @@ -23,6 +23,7 @@ public function getOrderShipmentData($order_id) $shipment = array( + 'reference_identifier' => 'Order_' . $order_id, 'recipient' => $this->getRecipient( $order_data ), 'options' => $this->getOptions( $order_data , false, true ), 'carrier' => 1, // default to POSTNL for now @@ -78,13 +79,13 @@ public function getOrderShipmentData($order_id) if(isset($product_info['myparcel_country']) && $product_info['myparcel_country'] != ''){ $country = $product_info['myparcel_country']; }else{ - if(isset($settings['default_country_code'])) $country = $settings['default_country_code']; + if(isset($settings['default_country_origin'])) $country = $settings['default_country_origin']; } $weightItem = (isset($product_info['weight']) && is_numeric($product_info['weight'])) ? $product_info['weight'] : $weightDefault; $weightItem = $class_weight->convert($weightItem, $product_info['weight_class_id'], 2); // always convert to gram - $priceItem = (isset($product_info['price']) && is_numeric($product_info['price'])) ? $product_info['price'] : 1000; + $priceItem = (isset($product_info['price']) && is_numeric($product_info['price'])) ? $product_info['price'] * 100 : 1000; $shipment['customs_declaration']['items'][] = array( 'description' => $order_product['name'], //product name @@ -105,6 +106,9 @@ public function getOrderShipmentData($order_id) $shipment['physical_properties'] = array( 'weight' => (int) $totalWeight ); + $shipment['options'] = [ + "package_type" => 1 + ]; } if($shipment['options'] == null){ return null; @@ -161,6 +165,7 @@ public function getRecipient( $order ) 'number' => $house_number, 'number_suffix' => $number_addition, 'postal_code' => $order['shipping_postcode'], + 'street_additional_info' => strlen($order['shipping_address_2']) >= 6 ? $order['shipping_address_2'] : "" ); } $address = array_merge($address, $address_intl); @@ -1227,4 +1232,11 @@ function updateOrderStatus($order_id, $order_status_id, $comment = '', $notify = $db->query("INSERT INTO " . DB_PREFIX . "order_history SET order_id = '" . (int)$order_id . "', order_status_id = '" . (int)$order_status_id . "', notify = '" . (int)$notify . "', comment = '" . $db->escape($comment) . "', date_added = NOW()"); } + + public function checkMultiCollo ($order_country_code, $package_type) { + return in_array( + $order_country_code, + [MyParcel::COUNTRY_CODE_NL, MyParcel::COUNTRY_CODE_BE] + ) && $package_type == MyParcel::PACKAGE_TYPE_STANDARD; + } } diff --git a/upload/system/library/myparcelnl/myparcel.ocmod.xml b/upload/system/library/myparcelnl/myparcel.ocmod.xml index 643a8c2..ba1b9fd 100644 --- a/upload/system/library/myparcelnl/myparcel.ocmod.xml +++ b/upload/system/library/myparcelnl/myparcel.ocmod.xml @@ -5,7 +5,7 @@ --> MyParcel NL - 1.1.8 + 1.1.17 MyParcelNL MyParcel NL https://www.myparcel.nl/