From 7a6f411e9a279c8236282f69ede1b6a524743fad Mon Sep 17 00:00:00 2001 From: kFrolichs Date: Tue, 24 Apr 2018 11:07:30 +0200 Subject: [PATCH 1/5] Added composer needs updating --- .gitignore | 3 ++- composer.json | 11 +++++++++++ includes/wcwuunder-create.php | 21 ++++++++++++++++++++- woocommerce-wuunder.php | 2 ++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 composer.json diff --git a/.gitignore b/.gitignore index 723ef36..3ce5adb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dbb6fb2 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "require": { + "wuunder/connector-php": ">=1.0.0" + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/wuunder/connector-php.git" + } + ] +} diff --git a/includes/wcwuunder-create.php b/includes/wcwuunder-create.php index 83297d9..3342d0c 100644 --- a/includes/wcwuunder-create.php +++ b/includes/wcwuunder-create.php @@ -109,6 +109,25 @@ private function buildWuunderData($orderId) public function generateBookingUrl() { + $connector = new Wuunder\Connector("YVc7rKdM6e6Q_HQK81NCt7SM0LT0TtQB"); + + + $parcelshopRequest = $connector->getParcelshopById(); + + $parcelshopConfig = new \Wuunder\Api\Config\ParcelshopConfig(); + $parcelshopConfig->setId("6fa97a3b-fabe-47eb-8cda-13a53b2f83df"); + + if ($parcelshopConfig->validate()) { + $parcelshopRequest->setConfig($parcelshopConfig); + + if ($parcelshopRequest->fire()) { + var_dump($parcelshopRequest->getParcelshopResponse()->getParcelshopData()); + } else { + var_dump($parcelshopRequest->getParcelshopResponse()->getError()); + } + } else { + print("ParcelshopConfig not complete"); + } exit(); if (isset($_REQUEST['order']) && $_REQUEST['action'] === "bookorder") { $order_id = $_REQUEST['order']; if (true) { @@ -513,4 +532,4 @@ public function get_order_items($order_id) } -new WC_Wuunder_Create(); \ No newline at end of file +new WC_Wuunder_Create(); diff --git a/woocommerce-wuunder.php b/woocommerce-wuunder.php index 9250926..e301125 100644 --- a/woocommerce-wuunder.php +++ b/woocommerce-wuunder.php @@ -40,6 +40,8 @@ } +require_once 'vendor/autoload.php'; + if (!class_exists('Woocommerce_Wuunder')) { class Woocommerce_Wuunder From 6c3f2116105fa3f6b1d82199ee4cacd4f1e13707 Mon Sep 17 00:00:00 2001 From: kFrolichs Date: Wed, 25 Apr 2018 11:36:10 +0200 Subject: [PATCH 2/5] Added composer implementation --- includes/wcwuunder-create.php | 105 ++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/includes/wcwuunder-create.php b/includes/wcwuunder-create.php index 3342d0c..4d1c51b 100644 --- a/includes/wcwuunder-create.php +++ b/includes/wcwuunder-create.php @@ -57,7 +57,6 @@ private function buildWuunderData($orderId) } } - $defLength = 80; $defWidth = 50; $defHeight = 35; @@ -109,25 +108,6 @@ private function buildWuunderData($orderId) public function generateBookingUrl() { - $connector = new Wuunder\Connector("YVc7rKdM6e6Q_HQK81NCt7SM0LT0TtQB"); - - - $parcelshopRequest = $connector->getParcelshopById(); - - $parcelshopConfig = new \Wuunder\Api\Config\ParcelshopConfig(); - $parcelshopConfig->setId("6fa97a3b-fabe-47eb-8cda-13a53b2f83df"); - - if ($parcelshopConfig->validate()) { - $parcelshopRequest->setConfig($parcelshopConfig); - - if ($parcelshopRequest->fire()) { - var_dump($parcelshopRequest->getParcelshopResponse()->getParcelshopData()); - } else { - var_dump($parcelshopRequest->getParcelshopResponse()->getError()); - } - } else { - print("ParcelshopConfig not complete"); - } exit(); if (isset($_REQUEST['order']) && $_REQUEST['action'] === "bookorder") { $order_id = $_REQUEST['order']; if (true) { @@ -138,8 +118,8 @@ public function generateBookingUrl() $bookingToken = uniqid(); update_post_meta($order_id, '_wuunder_label_booking_token', $bookingToken); - $redirectUrl = urlencode(get_site_url(null, "/wp-admin/edit.php?post_type=shop_order")); - $webhookUrl = urlencode(get_site_url(null, "/wuunder/webhook?order=" . $order_id . '&token=' . $bookingToken)); + $redirectUrl = get_site_url(null, "/wp-admin/edit.php?post_type=shop_order"); + $webhookUrl = get_site_url(null, "/wuunder/webhook?order=" . $order_id . '&token=' . $bookingToken); $status = get_option('wc_wuunder_api_status'); if ($status == 'productie') { @@ -150,33 +130,60 @@ public function generateBookingUrl() $apiKey = get_option('wc_wuunder_test_api'); } - $wuunderData = $this->buildWuunderData($order_id, $postData); - - // Encode variables - $json = json_encode($wuunderData); - - // Setup API connection - $cc = curl_init($apiUrl); - - curl_setopt($cc, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $apiKey, 'Content-type: application/json')); - curl_setopt($cc, CURLOPT_POST, 1); - curl_setopt($cc, CURLOPT_POSTFIELDS, $json); - curl_setopt($cc, CURLOPT_RETURNTRANSFER, true); - curl_setopt($cc, CURLOPT_VERBOSE, 1); - curl_setopt($cc, CURLOPT_HEADER, 1); - - // Don't log base64 image string - $wuunderData['picture'] = 'base64 string removed'; - - // Execute the cURL, fetch the XML - $result = curl_exec($cc); - $header_size = curl_getinfo($cc, CURLINFO_HEADER_SIZE); - $header = substr($result, 0, $header_size); - preg_match("!\r\n(?:Location|URI): *(.*?) *\r\n!i", $header, $matches); - $url = $matches[1]; - - // Close connection - curl_close($cc); + $connector = new Wuunder\Connector($apiKey); + + $booking = $connector->createBooking(); + + $bookingConfig = new Wuunder\Api\Config\BookingConfig(); + $bookingConfig->setWebhookUrl($webhookUrl); + $bookingConfig->setRedirectUrl($redirectUrl); + + $wuunderData = $this->buildWuunderData($order_id); + + $bookingConfig->setDescription($wuunderData['description']); + $bookingConfig->setKind($wuunderData['kind']); + $bookingConfig->setValue($wuunderData['value']); + $bookingConfig->setLength($wuunderData['length']); + $bookingConfig->setWidth($wuunderData['width']); + $bookingConfig->setHeight($wuunderData['height']); + $bookingConfig->setWeight($wuunderData['weight']); + $bookingConfig->setPreferredServiceLevel($wuunderData["preferred_service_level"]); + + $deliveryAddress = new \Wuunder\Api\Config\AddressConfig(); + $deliveryAddress->setEmailAddress($wuunderData['delivery_address']['email_address']); + $deliveryAddress->setFamilyName($wuunderData['delivery_address']['family_name']); + $deliveryAddress->setGivenName($wuunderData['delivery_address']['given_name']); + $deliveryAddress->setLocality($wuunderData['delivery_address']['locality']); + $deliveryAddress->setStreetName($wuunderData['delivery_address']['street_name']); + $deliveryAddress->setHouseNumber($wuunderData['delivery_address']['house_number']); + $deliveryAddress->setZipCode($wuunderData['delivery_address']['zip_code']); + $deliveryAddress->setPhoneNumber($wuunderData['delivery_address']['phone_number']); + $deliveryAddress->setCountry($wuunderData['delivery_address']['country']); + $bookingConfig->setDeliveryAddress($deliveryAddress); + + $pickupAddress = new \Wuunder\Api\Config\AddressConfig(); + $pickupAddress->setEmailAddress($wuunderData['pickup_address']['email_address']); + $pickupAddress->setFamilyName($wuunderData['pickup_address']['family_name']); + $pickupAddress->setGivenName($wuunderData['pickup_address']['given_name']); + $pickupAddress->setLocality($wuunderData['pickup_address']['locality']); + $pickupAddress->setStreetName($wuunderData['pickup_address']['street_name']); + $pickupAddress->setHouseNumber($wuunderData['pickup_address']['house_number']); + $pickupAddress->setZipCode($wuunderData['pickup_address']['zip_code']); + $pickupAddress->setPhoneNumber($wuunderData['pickup_address']['phone_number']); + $pickupAddress->setCountry($wuunderData['pickup_address']['country']); + $bookingConfig->setPickupAddress($pickupAddress); + + + if ($bookingConfig->validate()) { + $booking->setConfig($bookingConfig); + if ($booking->fire()) { + $url = $booking->getBookingResponse()->getBookingUrl(); + } else { + var_dump($booking->getBookingResponse()->getError()); + } + } else { + print("Bookingconfig not complete"); + } update_post_meta($order_id, '_wuunder_label_booking_url', $url); if (!(substr($url, 0, 5) === "http:" || substr($url, 0, 6) === "https:")) { From f98ac5deb28efa220a83ac4a7cf971c031509e5d Mon Sep 17 00:00:00 2001 From: kFrolichs Date: Wed, 25 Apr 2018 13:14:23 +0200 Subject: [PATCH 3/5] Changes after comments Timo --- includes/wcwuunder-create.php | 155 +++++++++++----------------------- 1 file changed, 51 insertions(+), 104 deletions(-) diff --git a/includes/wcwuunder-create.php b/includes/wcwuunder-create.php index 4d1c51b..fa010be 100644 --- a/includes/wcwuunder-create.php +++ b/includes/wcwuunder-create.php @@ -44,7 +44,7 @@ public function sample_admin_notice__error() } - private function buildWuunderData($orderId) + private function buildWuunderData($orderId, $redirectUrl, $webhookUrl) { $orderItems = $this->get_order_items($orderId); $orderMeta = get_post_meta($orderId); @@ -88,22 +88,23 @@ private function buildWuunderData($orderId) $value = intval($order->get_subtotal() * 100); - return array( - "description" => $description, - "personal_message" => "", - "customer_reference" => $orderId, - "picture" => $orderPicture, - "value" => ($value ? $value : $defValue), - "kind" => ($totalWeight > 23000 ? "pallet" : "package"), - "length" => round($dimensions[0]), - "width" => round($dimensions[1]), - "height" => round($dimensions[2]), - "weight" => ($totalWeight ? $totalWeight : $defWeight), - "delivery_address" => $customer, - "pickup_address" => $company, - "preferred_service_level" => (count($order->get_items('shipping')) > 0) ? $this->get_filter_from_shippingmethod(reset($order->get_items('shipping'))->get_method_id()) : "", - "source" => $this->version_obj - ); + $bookingConfig = new Wuunder\Api\Config\BookingConfig(); + $bookingConfig->setWebhookUrl($webhookUrl); + $bookingConfig->setRedirectUrl($redirectUrl); + + $bookingConfig->setDescription($description); + $bookingConfig->setKind($totalWeight > 23000 ? "pallet" : "package"); + $bookingConfig->setValue($value ? $value : $defValue); + $bookingConfig->setLength(round($dimensions[0])); + $bookingConfig->setWidth(round($dimensions[1])); + $bookingConfig->setHeight(round($dimensions[2])); + $bookingConfig->setWeight($totalWeight ? $totalWeight : $defWeight); + $bookingConfig->setPreferredServiceLevel((count($order->get_items('shipping')) > 0) ? $this->get_filter_from_shippingmethod(reset($order->get_items('shipping'))->get_method_id()) : ""); + + $bookingConfig->setDeliveryAddress($customer); + $bookingConfig->setPickupAddress($company); + + return $bookingConfig; } public function generateBookingUrl() @@ -131,48 +132,8 @@ public function generateBookingUrl() } $connector = new Wuunder\Connector($apiKey); - $booking = $connector->createBooking(); - - $bookingConfig = new Wuunder\Api\Config\BookingConfig(); - $bookingConfig->setWebhookUrl($webhookUrl); - $bookingConfig->setRedirectUrl($redirectUrl); - - $wuunderData = $this->buildWuunderData($order_id); - - $bookingConfig->setDescription($wuunderData['description']); - $bookingConfig->setKind($wuunderData['kind']); - $bookingConfig->setValue($wuunderData['value']); - $bookingConfig->setLength($wuunderData['length']); - $bookingConfig->setWidth($wuunderData['width']); - $bookingConfig->setHeight($wuunderData['height']); - $bookingConfig->setWeight($wuunderData['weight']); - $bookingConfig->setPreferredServiceLevel($wuunderData["preferred_service_level"]); - - $deliveryAddress = new \Wuunder\Api\Config\AddressConfig(); - $deliveryAddress->setEmailAddress($wuunderData['delivery_address']['email_address']); - $deliveryAddress->setFamilyName($wuunderData['delivery_address']['family_name']); - $deliveryAddress->setGivenName($wuunderData['delivery_address']['given_name']); - $deliveryAddress->setLocality($wuunderData['delivery_address']['locality']); - $deliveryAddress->setStreetName($wuunderData['delivery_address']['street_name']); - $deliveryAddress->setHouseNumber($wuunderData['delivery_address']['house_number']); - $deliveryAddress->setZipCode($wuunderData['delivery_address']['zip_code']); - $deliveryAddress->setPhoneNumber($wuunderData['delivery_address']['phone_number']); - $deliveryAddress->setCountry($wuunderData['delivery_address']['country']); - $bookingConfig->setDeliveryAddress($deliveryAddress); - - $pickupAddress = new \Wuunder\Api\Config\AddressConfig(); - $pickupAddress->setEmailAddress($wuunderData['pickup_address']['email_address']); - $pickupAddress->setFamilyName($wuunderData['pickup_address']['family_name']); - $pickupAddress->setGivenName($wuunderData['pickup_address']['given_name']); - $pickupAddress->setLocality($wuunderData['pickup_address']['locality']); - $pickupAddress->setStreetName($wuunderData['pickup_address']['street_name']); - $pickupAddress->setHouseNumber($wuunderData['pickup_address']['house_number']); - $pickupAddress->setZipCode($wuunderData['pickup_address']['zip_code']); - $pickupAddress->setPhoneNumber($wuunderData['pickup_address']['phone_number']); - $pickupAddress->setCountry($wuunderData['pickup_address']['country']); - $bookingConfig->setPickupAddress($pickupAddress); - + $bookingConfig = $this->buildWuunderData($order_id, $redirectUrl, $webhookUrl); if ($bookingConfig->validate()) { $booking->setConfig($bookingConfig); @@ -248,41 +209,31 @@ public function check_company_address() public function get_company_address($orderid, $pickup_address) { - + $pickupAddress = new \Wuunder\Api\Config\AddressConfig(); if ($pickup_address == 0) { // Get Woocommerce Wuunder Settings - $company_address = array( - "business" => get_option('wc_wuunder_company_name'), - "chamber_of_commerce_number" => $orderid, - "email_address" => get_option('wc_wuunder_company_email'), - "family_name" => get_option('wc_wuunder_company_lastname'), - "given_name" => get_option('wc_wuunder_company_firstname'), - "locality" => get_option('wc_wuunder_company_city'), - "phone_number" => get_option('wc_wuunder_company_phone'), - "street_name" => get_option('wc_wuunder_company_street'), - "house_number" => get_option('wc_wuunder_company_housenumber'), - "zip_code" => get_option('wc_wuunder_company_postode'), - "country" => get_option('wc_wuunder_company_country'), - ); + $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email')); + $pickupAddress->setFamilyName(get_option('wc_wuunder_company_lastname')); + $pickupAddress->setGivenName(get_option('wc_wuunder_company_firstname')); + $pickupAddress->setLocality(get_option('wc_wuunder_company_city')); + $pickupAddress->setStreetName(get_option('wc_wuunder_company_street')); + $pickupAddress->setHouseNumber(get_option('wc_wuunder_company_housenumber')); + $pickupAddress->setZipCode(get_option('wc_wuunder_company_postode')); + $pickupAddress->setPhoneNumber(get_option('wc_wuunder_company_phone')); + $pickupAddress->setCountry(get_option('wc_wuunder_company_country')); } else { // Get Woocommerce Wuunder Settings - $company_address = array( - "business" => get_option('wc_wuunder_company_name_' . $pickup_address), - "chamber_of_commerce_number" => $orderid, - "email_address" => get_option('wc_wuunder_company_email_' . $pickup_address), - "family_name" => get_option('wc_wuunder_company_lastname_' . $pickup_address), - "given_name" => get_option('wc_wuunder_company_firstname_' . $pickup_address), - "locality" => get_option('wc_wuunder_company_city_' . $pickup_address), - "phone_number" => get_option('wc_wuunder_company_phone_' . $pickup_address), - "street_name" => get_option('wc_wuunder_company_street_' . $pickup_address), - "house_number" => get_option('wc_wuunder_company_housenumber_' . $pickup_address), - "zip_code" => get_option('wc_wuunder_company_postode_' . $pickup_address), - "country" => get_option('wc_wuunder_company_country_' . $pickup_address), - ); + $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email_' . $pickup_address)); + $pickupAddress->setFamilyName(get_option('wc_wuunder_company_lastname_' . $pickup_address)); + $pickupAddress->setGivenName(get_option('wc_wuunder_company_firstname_' . $pickup_address)); + $pickupAddress->setLocality(get_option('wc_wuunder_company_city_' . $pickup_address)); + $pickupAddress->setStreetName(get_option('wc_wuunder_company_street_' . $pickup_address)); + $pickupAddress->setHouseNumber(get_option('wc_wuunder_company_housenumber_' . $pickup_address)); + $pickupAddress->setZipCode(get_option('wc_wuunder_company_postode_' . $pickup_address)); + $pickupAddress->setPhoneNumber(get_option('wc_wuunder_company_phone_' . $pickup_address)); + $pickupAddress->setCountry(get_option('wc_wuunder_company_country_' . $pickup_address)); } - - return $company_address; - + return $pickupAddress; } private function separateAddressLine($addressLine) @@ -326,6 +277,7 @@ public function get_customer_address($orderid, $phone) { // Get customer address from order $order_meta = get_post_meta($orderid); + $deliveryAddress = new \Wuunder\Api\Config\AddressConfig(); $street_name = $this->get_customer_address_part($order_meta, '_street_name'); if (empty($street_name)) { $street_name = $this->get_customer_address_from_address_line($order_meta)[0]; @@ -334,22 +286,17 @@ public function get_customer_address($orderid, $phone) if (empty($house_number)) { $house_number = $this->get_customer_address_from_address_line($order_meta)[1]; } - $customer_address = array( - "business" => $this->get_customer_address_part($order_meta, '_company'), - "chamber_of_commerce_number" => $orderid, - "email_address" => $this->get_customer_address_part($order_meta, '_email'), - "family_name" => $this->get_customer_address_part($order_meta, '_last_name'), - "given_name" => $this->get_customer_address_part($order_meta, '_first_name'), - "locality" => $this->get_customer_address_part($order_meta, '_city'), - "phone_number" => "$phone", - "street_name" => $street_name, - "house_number" => $house_number, - "zip_code" => str_replace(' ', '', $this->get_customer_address_part($order_meta, '_postcode')), - "country" => $this->get_customer_address_part($order_meta, '_country'), - ); - - return $customer_address; - + $deliveryAddress->setEmailAddress($this->get_customer_address_part($order_meta, '_email')); + $deliveryAddress->setFamilyName($this->get_customer_address_part($order_meta, '_last_name')); + $deliveryAddress->setGivenName($this->get_customer_address_part($order_meta, '_first_name')); + $deliveryAddress->setLocality($this->get_customer_address_part($order_meta, '_city')); + $deliveryAddress->setStreetName($street_name); + $deliveryAddress->setHouseNumber($house_number); + $deliveryAddress->setZipCode(str_replace(' ', '', $this->get_customer_address_part($order_meta, '_postcode'))); + $deliveryAddress->setPhoneNumber("$phone"); + $deliveryAddress->setCountry($this->get_customer_address_part($order_meta, '_country')); + + return $deliveryAddress; } public function get_base64_image($imagepath) From 1be5f1a45f07d5e3bee3a44f57821ac39238b6a9 Mon Sep 17 00:00:00 2001 From: kFrolichs Date: Thu, 26 Apr 2018 12:02:03 +0200 Subject: [PATCH 4/5] Refactoring and clean-up of code --- includes/wcwuunder-create.php | 223 ++++++++++++++++++---------------- 1 file changed, 120 insertions(+), 103 deletions(-) diff --git a/includes/wcwuunder-create.php b/includes/wcwuunder-create.php index fa010be..c4a4fd0 100644 --- a/includes/wcwuunder-create.php +++ b/includes/wcwuunder-create.php @@ -18,6 +18,9 @@ public function __construct() wp_enqueue_style('wuunder-admin', (dirname(plugin_dir_url(__FILE__)) . '/assets/css/wuunder-admin.css')); } + /** + * Creates an error message for the admin order page + */ public function sample_admin_notice__error() { @@ -44,7 +47,13 @@ public function sample_admin_notice__error() } - private function buildWuunderData($orderId, $redirectUrl, $webhookUrl) + /** + * Sets the address and package data for the booking request + * + * @param $orderId + * @return $bookingConfig + */ + private function setBookingConfig($orderId) { $orderItems = $this->get_order_items($orderId); $orderMeta = get_post_meta($orderId); @@ -57,15 +66,9 @@ private function buildWuunderData($orderId, $redirectUrl, $webhookUrl) } } - $defLength = 80; - $defWidth = 50; - $defHeight = 35; - $defWeight = 5000; - $defValue = 25 * 100; - // Get WooCommerce Wuunder Address from options page - $company = $this->get_company_address($orderId, 0); - $customer = $this->get_customer_address($orderId, $orderMeta['_billing_phone'][0]); + $company = $this->get_company_address(); + $customer = $this->get_customer_address($orderId); $totalWeight = 0; $dimensions = null; @@ -79,26 +82,28 @@ private function buildWuunderData($orderId, $redirectUrl, $webhookUrl) $description .= "- " . $item['quantity'] . "x " . $item['name'] . " \r\n"; } - if ($totalWeight === 0) { - $totalWeight = $defWeight; - } if (count($dimensions) !== 3) { - $dimensions = array($defLength, $defWidth, $defHeight); + $dimensions = array(0, 0, 0); } $value = intval($order->get_subtotal() * 100); + $bookingToken = uniqid(); + update_post_meta($orderId, '_wuunder_label_booking_token', $bookingToken); + $redirectUrl = get_site_url(null, "/wp-admin/edit.php?post_type=shop_order"); + $webhookUrl = get_site_url(null, "/wuunder/webhook?order=" . $orderId . '&token=' . $bookingToken); + $bookingConfig = new Wuunder\Api\Config\BookingConfig(); $bookingConfig->setWebhookUrl($webhookUrl); $bookingConfig->setRedirectUrl($redirectUrl); $bookingConfig->setDescription($description); $bookingConfig->setKind($totalWeight > 23000 ? "pallet" : "package"); - $bookingConfig->setValue($value ? $value : $defValue); + $bookingConfig->setValue($value ? $value : 0); $bookingConfig->setLength(round($dimensions[0])); $bookingConfig->setWidth(round($dimensions[1])); $bookingConfig->setHeight(round($dimensions[2])); - $bookingConfig->setWeight($totalWeight ? $totalWeight : $defWeight); + $bookingConfig->setWeight($totalWeight ? $totalWeight : 0); $bookingConfig->setPreferredServiceLevel((count($order->get_items('shipping')) > 0) ? $this->get_filter_from_shippingmethod(reset($order->get_items('shipping'))->get_method_id()) : ""); $bookingConfig->setDeliveryAddress($customer); @@ -107,59 +112,37 @@ private function buildWuunderData($orderId, $redirectUrl, $webhookUrl) return $bookingConfig; } + /** + * Generates the booking url that takes the user to Wuunder. + * Returns the user to the original order page with the redirect. + */ public function generateBookingUrl() { if (isset($_REQUEST['order']) && $_REQUEST['action'] === "bookorder") { $order_id = $_REQUEST['order']; - if (true) { - $postData = array(); - if (isset($_POST['data'])) - $postData = stripslashes_deep($_POST['data']); - - $bookingToken = uniqid(); - update_post_meta($order_id, '_wuunder_label_booking_token', $bookingToken); + $postData = array(); + if (isset($_POST['data'])) + $postData = stripslashes_deep($_POST['data']); - $redirectUrl = get_site_url(null, "/wp-admin/edit.php?post_type=shop_order"); - $webhookUrl = get_site_url(null, "/wuunder/webhook?order=" . $order_id . '&token=' . $bookingToken); + $apiKey = ($status == 'productie' ? get_option('wc_wuunder_api'): get_option('wc_wuunder_test_api')); - $status = get_option('wc_wuunder_api_status'); - if ($status == 'productie') { - $apiUrl = 'https://api.wearewuunder.com/api/bookings?redirect_url=' . $redirectUrl . '&webhook_url=' . $webhookUrl; - $apiKey = get_option('wc_wuunder_api'); - } else { - $apiUrl = 'https://api-staging.wearewuunder.com/api/bookings?redirect_url=' . $redirectUrl . '&webhook_url=' . $webhookUrl; - $apiKey = get_option('wc_wuunder_test_api'); - } + $connector = new Wuunder\Connector($apiKey); + $booking = $connector->createBooking(); + $bookingConfig = $this->setBookingConfig($order_id); - $connector = new Wuunder\Connector($apiKey); - $booking = $connector->createBooking(); - $bookingConfig = $this->buildWuunderData($order_id, $redirectUrl, $webhookUrl); - - if ($bookingConfig->validate()) { - $booking->setConfig($bookingConfig); - if ($booking->fire()) { - $url = $booking->getBookingResponse()->getBookingUrl(); - } else { - var_dump($booking->getBookingResponse()->getError()); - } + if ($bookingConfig->validate()) { + $booking->setConfig($bookingConfig); + if ($booking->fire()) { + $url = $booking->getBookingResponse()->getBookingUrl(); } else { - print("Bookingconfig not complete"); + var_dump($booking->getBookingResponse()->getError()); } - - update_post_meta($order_id, '_wuunder_label_booking_url', $url); - if (!(substr($url, 0, 5) === "http:" || substr($url, 0, 6) === "https:")) { - if ($status == 'productie') { - $url = 'https://api.wearewuunder.com' . $url; - } else { - $url = 'https://api-staging.wearewuunder.com' . $url; - } - } - wp_redirect($url); - exit; } else { -// wp_redirect(get_site_url(null, " / wp - admin / edit . php ? post_type = shop_order")); -// exit; + print("Bookingconfig not complete"); } + + wp_redirect($url); + exit; } else { // wp_redirect(get_site_url(null, " / wp - admin / edit . php ? post_type = shop_order")); // exit; @@ -168,14 +151,14 @@ public function generateBookingUrl() public function test() { -// $order_meta = get_post_meta(73); -//// var_dump($this->get_customer_address_part($order_meta, '_first_name')); -// $statuses = wc_get_order_statuses(); -// echo "
";
-//            var_dump($statuses);
-//            echo " 
"; } + /** + * Returns the filter (preferred service level) that is set in the Wuunder config + * + * @param $shipping_method + * @return + */ private function get_filter_from_shippingmethod($shipping_method) { if (strpos($shipping_method, ':') !== false) { @@ -194,48 +177,39 @@ private function get_filter_from_shippingmethod($shipping_method) } } - public function check_company_address() - { - - if (get_option('wc_wuunder_company_name') && get_option('wc_wuunder_company_firstname') && get_option('wc_wuunder_company_lastname') && get_option('wc_wuunder_company_street') && get_option('wc_wuunder_company_housenumber') && get_option('wc_wuunder_company_postode') && get_option('wc_wuunder_company_city') && get_option('wc_wuunder_company_country') && get_option('wc_wuunder_company_email') && get_option('wc_wuunder_company_phone')) { - $check = true; - } else { - $check = false; - } - - return $check; - - } - - public function get_company_address($orderid, $pickup_address) + /** + * Gets the company address set in the Wuunder config + * + * @return $pickupAddress + */ + public function get_company_address() { $pickupAddress = new \Wuunder\Api\Config\AddressConfig(); - if ($pickup_address == 0) { - // Get Woocommerce Wuunder Settings - $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email')); - $pickupAddress->setFamilyName(get_option('wc_wuunder_company_lastname')); - $pickupAddress->setGivenName(get_option('wc_wuunder_company_firstname')); - $pickupAddress->setLocality(get_option('wc_wuunder_company_city')); - $pickupAddress->setStreetName(get_option('wc_wuunder_company_street')); - $pickupAddress->setHouseNumber(get_option('wc_wuunder_company_housenumber')); - $pickupAddress->setZipCode(get_option('wc_wuunder_company_postode')); - $pickupAddress->setPhoneNumber(get_option('wc_wuunder_company_phone')); - $pickupAddress->setCountry(get_option('wc_wuunder_company_country')); + + $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email')); + $pickupAddress->setFamilyName(get_option('wc_wuunder_company_lastname')); + $pickupAddress->setGivenName(get_option('wc_wuunder_company_firstname')); + $pickupAddress->setLocality(get_option('wc_wuunder_company_city')); + $pickupAddress->setStreetName(get_option('wc_wuunder_company_street')); + $pickupAddress->setHouseNumber(get_option('wc_wuunder_company_housenumber')); + $pickupAddress->setZipCode(get_option('wc_wuunder_company_postode')); + $pickupAddress->setPhoneNumber(get_option('wc_wuunder_company_phone')); + $pickupAddress->setCountry(get_option('wc_wuunder_company_country')); + if($pickupAddress->validate()) + { + return $pickupAddress; } else { - // Get Woocommerce Wuunder Settings - $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email_' . $pickup_address)); - $pickupAddress->setFamilyName(get_option('wc_wuunder_company_lastname_' . $pickup_address)); - $pickupAddress->setGivenName(get_option('wc_wuunder_company_firstname_' . $pickup_address)); - $pickupAddress->setLocality(get_option('wc_wuunder_company_city_' . $pickup_address)); - $pickupAddress->setStreetName(get_option('wc_wuunder_company_street_' . $pickup_address)); - $pickupAddress->setHouseNumber(get_option('wc_wuunder_company_housenumber_' . $pickup_address)); - $pickupAddress->setZipCode(get_option('wc_wuunder_company_postode_' . $pickup_address)); - $pickupAddress->setPhoneNumber(get_option('wc_wuunder_company_phone_' . $pickup_address)); - $pickupAddress->setCountry(get_option('wc_wuunder_company_country_' . $pickup_address)); + print("Invalid pickup address"); + return $pickupAddress; } - return $pickupAddress; } + /** + * Seperates the street name and number when both are set on the same line + * + * @param $addressLine + * @return array containing 2 values: streetName and streetNumber + */ private function separateAddressLine($addressLine) { if (preg_match('/^([^\d]*[^\d\s]) *(\d.*)$/', $addressLine, $result)) { @@ -251,6 +225,12 @@ private function separateAddressLine($addressLine) return array($addressLine, ""); } + /** + * Retrieves part of the customer address + * + * @param $order_meta, $suffix + * @return $order_meta + */ private function get_customer_address_part($order_meta, $suffix) { if (isset($order_meta['_shipping' . $suffix]) && !empty($order_meta['_shipping' . $suffix][0])) { @@ -262,6 +242,12 @@ private function get_customer_address_part($order_meta, $suffix) } } + /** + * Retrieves the customer address puts in function separateAddressLine + * + * @param $order_meta + * @return array containing 2 values: streetName and streetNumber + */ private function get_customer_address_from_address_line($order_meta) { if (isset($order_meta['_shipping_address_1']) && !empty($order_meta['_shipping_address_1'])) { @@ -273,7 +259,13 @@ private function get_customer_address_from_address_line($order_meta) } } - public function get_customer_address($orderid, $phone) + /** + * Fills the delivery address with the customers data + * + * @param $orderid + * @return $deliveryAddress + */ + public function get_customer_address($orderid) { // Get customer address from order $order_meta = get_post_meta($orderid); @@ -293,12 +285,24 @@ public function get_customer_address($orderid, $phone) $deliveryAddress->setStreetName($street_name); $deliveryAddress->setHouseNumber($house_number); $deliveryAddress->setZipCode(str_replace(' ', '', $this->get_customer_address_part($order_meta, '_postcode'))); - $deliveryAddress->setPhoneNumber("$phone"); + $deliveryAddress->setPhoneNumber($order_meta['_billing_phone'][0]); $deliveryAddress->setCountry($this->get_customer_address_part($order_meta, '_country')); - return $deliveryAddress; + if($deliveryAddress->validate()) + { + return $deliveryAddress; + } else { + print("Invalid delivery address"); + return $deliveryAddress; + } } + /** + * Checks if the image is smaller than 2MB and base 64 encodes if it is + * + * @param $imagepath + * @return $image + */ public function get_base64_image($imagepath) { try { @@ -315,6 +319,12 @@ public function get_base64_image($imagepath) } } + /** + * Employed in get_base64_image to get the correct path + * + * @param $url + * @return string + */ private function remote_filesize($url) { static $regex = '/^Content-Length: *+\K\d++$/im'; @@ -330,9 +340,12 @@ private function remote_filesize($url) return strlen(stream_get_contents($fp)); } + /** + * + * @param $order + */ public function add_listing_actions($order) { - // do not show buttons for trashed orders if ($order->get_status() == 'trash') { return; @@ -410,6 +423,10 @@ public function sidebar_box_content($post) $this->add_listing_actions($order); } + /** + * + * + */ public function get_order_items($order_id) { From 63c71400c6d3f5642240834258605789b81be6a4 Mon Sep 17 00:00:00 2001 From: kFrolichs Date: Thu, 26 Apr 2018 13:58:48 +0200 Subject: [PATCH 5/5] Added logging and source to request to Wuunder --- includes/wcwuunder-create.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/includes/wcwuunder-create.php b/includes/wcwuunder-create.php index c4a4fd0..483deca 100644 --- a/includes/wcwuunder-create.php +++ b/includes/wcwuunder-create.php @@ -55,6 +55,10 @@ public function sample_admin_notice__error() */ private function setBookingConfig($orderId) { + $logger = wc_get_logger(); + $context = array('source' => "wuunder_connector"); + $logger->log('info', "Filling the booking config", $context); + $orderItems = $this->get_order_items($orderId); $orderMeta = get_post_meta($orderId); $order = new WC_Order($orderId); @@ -105,6 +109,7 @@ private function setBookingConfig($orderId) $bookingConfig->setHeight(round($dimensions[2])); $bookingConfig->setWeight($totalWeight ? $totalWeight : 0); $bookingConfig->setPreferredServiceLevel((count($order->get_items('shipping')) > 0) ? $this->get_filter_from_shippingmethod(reset($order->get_items('shipping'))->get_method_id()) : ""); + $bookingConfig->setSource($version_obj); $bookingConfig->setDeliveryAddress($customer); $bookingConfig->setPickupAddress($company); @@ -118,6 +123,10 @@ private function setBookingConfig($orderId) */ public function generateBookingUrl() { + $logger = wc_get_logger(); + $context = array('source' => "wuunder_connector"); + $logger->log('info', "Generating the booking url", $context); + if (isset($_REQUEST['order']) && $_REQUEST['action'] === "bookorder") { $order_id = $_REQUEST['order']; $postData = array(); @@ -135,15 +144,16 @@ public function generateBookingUrl() if ($booking->fire()) { $url = $booking->getBookingResponse()->getBookingUrl(); } else { - var_dump($booking->getBookingResponse()->getError()); + $logger->log('error', $booking->getBookingResponse()->getError(), $context); } } else { - print("Bookingconfig not complete"); + $logger->log('error', "Bookingconfig not complete", $context); } wp_redirect($url); exit; } else { + $logger->log('error', "No order set", $context); // wp_redirect(get_site_url(null, " / wp - admin / edit . php ? post_type = shop_order")); // exit; } @@ -184,6 +194,9 @@ private function get_filter_from_shippingmethod($shipping_method) */ public function get_company_address() { + $logger = wc_get_logger(); + $context = array('source' => "wuunder_connector"); + $pickupAddress = new \Wuunder\Api\Config\AddressConfig(); $pickupAddress->setEmailAddress(get_option('wc_wuunder_company_email')); @@ -199,7 +212,7 @@ public function get_company_address() { return $pickupAddress; } else { - print("Invalid pickup address"); + $logger->log('error', "Invalid pickup address. There are mistakes or missing fields.", $context); return $pickupAddress; } } @@ -267,6 +280,9 @@ private function get_customer_address_from_address_line($order_meta) */ public function get_customer_address($orderid) { + $logger = wc_get_logger(); + $context = array('source' => "wuunder_connector"); + // Get customer address from order $order_meta = get_post_meta($orderid); $deliveryAddress = new \Wuunder\Api\Config\AddressConfig(); @@ -292,7 +308,7 @@ public function get_customer_address($orderid) { return $deliveryAddress; } else { - print("Invalid delivery address"); + $logger->log('error', "Invalid delivery address. There are mistakes or missing fields.", $context); return $deliveryAddress; } } @@ -305,6 +321,8 @@ public function get_customer_address($orderid) */ public function get_base64_image($imagepath) { + $logger = wc_get_logger(); + $context = array('source' => "wuunder_connector"); try { $fileSize = (substr($imagepath, 0, 4) === "http") ? $this->remote_filesize($imagepath) : filesize($imagepath); if ($fileSize <= 2097152) { //smaller or equal to 2MB @@ -315,6 +333,7 @@ public function get_base64_image($imagepath) } return $image; } catch (Exception $e) { + $logger->log('error', $e, $context); return ""; } }