diff --git a/backend/app/plugins/wiaas/includes/api/class-wiaas-cart-api.php b/backend/app/plugins/wiaas/includes/api/class-wiaas-cart-api.php index a8cd272..f6be64a 100644 --- a/backend/app/plugins/wiaas/includes/api/class-wiaas-cart-api.php +++ b/backend/app/plugins/wiaas/includes/api/class-wiaas-cart-api.php @@ -229,11 +229,13 @@ class Wiaas_Cart_API { $request['options_ids'] ); - if ($success) { - return wiaas_api_notice('PACKAGE_ADDED', 'success'); + if (!$success) { + return (wc_notice_count('error') > 0) ? + wiaas_api_cart_error_notices() : + wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error'); } - return wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error'); + return wiaas_api_notice('PACKAGE_ADDED', 'success'); } /** @@ -245,11 +247,13 @@ class Wiaas_Cart_API { $success = Wiaas_Cart::remove_package_from_cart($request['key']); - if ($success) { - return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success'); + if (!$success) { + return (wc_notice_count('error') > 0) ? + wiaas_api_cart_error_notices() : + wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error'); } - return wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error'); + return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success'); } /** @@ -260,11 +264,13 @@ class Wiaas_Cart_API { $success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']); - if ($success) { - return wiaas_api_notice('QUANTITY_UPDATED', 'success'); + if (!$success) { + return (wc_notice_count('error') > 0) ? + wiaas_api_cart_error_notices() : + wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error'); } - return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error'); + return wiaas_api_notice('QUANTITY_UPDATED', 'success'); } /** @@ -283,25 +289,15 @@ class Wiaas_Cart_API { * @return mixed|WP_REST_Response */ public static function upload_cart_document($request) { - $result = Wiaas_Cart::upload_cart_document($request['doc_type'], $request['package_key']); + $success = Wiaas_Cart::upload_cart_document($request['doc_type'], $request['package_key']); - if (is_wp_error($result)) { - $error_code = $result->get_error_code(); - - if ($error_code === 'wiaas_upload_missing_file') { - return wiaas_api_generate_error('NO_FILE'); - } - - if ($error_code === 'wiaas_upload_error') { - return wiaas_api_generate_error('UPLOAD_ERROR'); - } - - return wiaas_api_generate_error('NOT_LINKED_TO_CART'); + if (!$success) { + return (wc_notice_count('error') > 0) ? + wiaas_api_cart_error_notices() : + wiaas_api_generate_error('UPLOAD_ERROR'); } - return wiaas_api_notice('FILE_UPLOADED', 'success', array( - 'id_document' => $result, - )); + return wiaas_api_notice('FILE_UPLOADED', 'success'); } /** @@ -325,6 +321,10 @@ class Wiaas_Cart_API { Wiaas_Checkout::process_checkout($request->get_body_params()); + if (wc_notice_count('error') > 0) { + return wiaas_api_cart_error_notices(); + } + return wiaas_api_notice('ORDER_PLACED', 'success'); } } \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/api/wiaas-api-functions.php b/backend/app/plugins/wiaas/includes/api/wiaas-api-functions.php index fb34974..15db837 100644 --- a/backend/app/plugins/wiaas/includes/api/wiaas-api-functions.php +++ b/backend/app/plugins/wiaas/includes/api/wiaas-api-functions.php @@ -22,6 +22,59 @@ function wiaas_api_notice($message, $code, $data = null) { )); } +/** + * Generates REST API notice responses with wc error notices + * + * @param array|null $data + * + * @return WP_REST_Response + */ +function wiaas_api_cart_error_notices($data = null) { + return wiaas_api_cart_notices('error', $data); +} + +/** + * Generates REST API notice responses with wc warning notices + * + * @param array|null $data + * + * @return WP_REST_Response + */ +function wiaas_api_cart_warning_notices($data = null) { + return wiaas_api_cart_notices('notice', $data); +} + +/** + * Generates REST API notice responses with wc notices + * + * @param string $types Notice types (error, success) + * @param array|null $data + * + * @return WP_REST_Response + */ +function wiaas_api_cart_notices($types, $data = null) { + + $types = is_array($types) ? $types : array( $types ); + + $messages = array(); + foreach ($types as $type) { + $messages = array_merge($messages, wc_get_notices($type)); + } + wc_clear_notices(); + + $messages = array_map(function($message) { + return array( + 'code' => 'error', + 'message' => $message + ); + }, $messages); + + return rest_ensure_response(array( + 'messages' => $messages, + 'data' => $data + )); +} + /** * Generate REST API error * diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-cart.php b/backend/app/plugins/wiaas/includes/class-wiaas-cart.php index 9ac3df7..03e9f65 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-cart.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-cart.php @@ -26,7 +26,6 @@ class Wiaas_Cart { add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1); add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1); - } /** @@ -73,29 +72,35 @@ class Wiaas_Cart { */ public static function upload_cart_document($doc_type, $package_item_key) { try { - $version = Wiaas_Document_Upload::upload_document_version(); + $result = Wiaas_Document_Upload::upload_document_version(); - if (is_wp_error($version)) { - return $version; + // File upload failed + if (is_wp_error($result)) { + $code = $result->get_error_code(); + + if ($code === 'wiaas_upload_error_missing_file' || $code === 'wiaas_upload_error_invalid_extension') { + wc_add_notice($result->get_error_message(), 'error'); + } else { + wc_add_notice('File upload failed!', 'error'); + } + + return false; } - $cart_document = array( - 'version' => $version, + // save uploaded cart documents info to cart item + WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = array( + 'version' => $result, 'key' => wp_generate_uuid4(), - ); + );; - // persist uploaded cart document in cart - WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = $cart_document; + // persist changes + WC()->cart->calculate_totals(); - // persist changes to cart session - // Note: TODO Refactor this logic of persisting updates made to cart - $s = new WC_Cart_Session(WC()->cart); - $s->set_session(); - - return $cart_document['key']; + return true; } catch( Exception $e) { - return new WP_Error('wiass_cart_upload_error', 'Error while uploading cart file!'); + wc_add_notice('Could not upload cart document!', 'error'); + return false; } } @@ -117,9 +122,16 @@ class Wiaas_Cart { public static function add_package_to_cart($package_id, $price_id, $addons_ids, $options_ids) { // try adding package to cart try { + // Check if package is in cart + if (self::_is_package_in_cart($package_id)) { + wc_add_notice('Package already in cart!', 'error'); + return false; + } + //Check if package exists $package = wc_get_product( $package_id ); if (!$package) { + wc_add_notice('Package does not exists!', 'error'); return false; } @@ -143,6 +155,7 @@ class Wiaas_Cart { $cart_item_key = WC()->cart->add_to_cart($package_id, 1, 0, array(), $wiaas_cart_item_data); if (!$cart_item_key) { + wc_add_notice('Package could not be added to cart!', 'error'); return false; } @@ -697,6 +710,24 @@ class Wiaas_Cart { WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_addon_items'] = $addon_items_keys; WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_option_items'] = $option_items_keys; } + + /** + * Check if package is added to cart + * @param int $package_id + * + * @return bool + */ + private static function _is_package_in_cart($package_id) { + $cart_item = WC()->cart->get_cart_contents(); + + foreach ($cart_item as $item) { + if($item['product_id'] === $package_id) { + return true; + } + } + + return false; + } } Wiaas_Cart::init(); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-checkout.php b/backend/app/plugins/wiaas/includes/class-wiaas-checkout.php index 5f0a044..ae2324a 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-checkout.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-checkout.php @@ -12,49 +12,41 @@ class Wiaas_Checkout { public static function process_checkout($data) { try { - $customer = wp_get_current_user(); + wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true ); + wc_set_time_limit( 0 ); - $order_id = WC()->checkout()->create_order(array()); + do_action( 'woocommerce_before_checkout_process' ); + + // Check if cart empty before proceeding + if (WC()->cart->is_empty()) { + return false; + } + + do_action( 'woocommerce_checkout_process' ); + + // Validate cart items before proceeding + WC()->checkout()->check_cart_items(); + + // if something is wrong bail out + if(wc_notice_count( 'error' ) > 0) { + return false; + } + + // try processing order + $order_id = WC()->checkout()->create_order($data); $order = wc_get_order( $order_id ); - // set order currency - $line_items = $order->get_items(); - foreach ($line_items as $line_item) { - if (isset($line_item['wiaas_currency'])) { - $order->set_currency($line_item['wiaas_currency']); - break; - } + if ( is_wp_error( $order_id ) ) { + throw new Exception( $order_id->get_error_message() ); } - $delivery_address = Wiaas_Customer::get_customer_profile_address($customer->ID, $data['delivery_address_id']); - $billing_address = Wiaas_Customer::get_customer_billing_address($customer->ID, $data['billing_address_id']); - - if (isset($delivery_address)) { - $order->set_shipping_city($delivery_address['city']); - $order->set_shipping_country($delivery_address['country_name']); - $order->set_shipping_address_1($delivery_address['detailed_address']); - $order->set_shipping_postcode($delivery_address['zip_code']); - $order->set_shipping_first_name($delivery_address['first_name']); - $order->set_shipping_last_name($delivery_address['last_name']); + if ( ! $order ) { + throw new Exception( __( 'Unable to create order.', 'woocommerce' ) ); } - if (isset($billing_address)) { - $order->set_billing_city($billing_address['city']); - $order->set_billing_country($billing_address['country_name']); - $order->set_billing_address_1($billing_address['detailed_address']); - $order->set_billing_postcode($billing_address['zip_code']); - $order->set_billing_first_name($billing_address['first_name']); - $order->set_billing_last_name($billing_address['last_name']); - } + self::_add_wiaas_checkout_data($order, $data); - Wiaas_Order::set_order_vat($order_id, $data['vat']); - Wiaas_Order::set_order_company($order_id, $data['company_name']); - Wiaas_Order::set_order_reference($order_id, $data['reference']); - Wiaas_Order::set_order_tender($order_id, $data['tender']); - - if (isset($data['project_id'])) { - Wiaas_Order_Project::set_project_for_order($order_id, $data['project_id']); - } + do_action( 'woocommerce_checkout_order_processed', $order_id, array(), $order ); $order->payment_complete(); @@ -63,7 +55,35 @@ class Wiaas_Checkout { return true; } catch (Exception $e) { + wc_add_notice( $e->getMessage(), 'error' ); return false; } } + + /** + * Add additional wiaas checkout info for order + * @param $order + * @param array $data + */ + private static function _add_wiaas_checkout_data($order, $data) { + // save currency + $line_items = $order->get_items(); + foreach ($line_items as $line_item) { + if (isset($line_item['wiaas_currency'])) { + $order->set_currency($line_item['wiaas_currency']); + break; + } + } + + // save additional wiaas order info + Wiaas_Order::set_order_vat($order->get_id(), $data['vat']); + Wiaas_Order::set_order_company($order->get_id(), $data['company_name']); + Wiaas_Order::set_order_reference($order->get_id(), $data['reference']); + Wiaas_Order::set_order_tender($order->get_id(), $data['tender']); + + // add order to project + if (isset($data['project_id'])) { + Wiaas_Order_Project::set_project_for_order($order->get_id(), $data['project_id']); + } + } } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index eca0dd8..5a241fa 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -118,6 +118,8 @@ class Wiaas_Order { $data = self::_append_order_process($data, $order, $request); $data = self::_append_order_comments($data, $order, $request); + + $data = self::_append_documents($data, $order, $request); } $response->set_data($data); @@ -242,10 +244,6 @@ class Wiaas_Order { // add only product lines that represent product bundles if (isset($item['wiaas_standard_package'])) { - - // get documents - $product_line['documents'] = wiaas_get_package_order_item_documents($order, $product_line['id']); - # get payment type info $product_line['payment_type'] = $item['wiaas_payment_type']; $product_line['service_price'] = floatval($item['wiaas_services_extra']); @@ -354,6 +352,27 @@ class Wiaas_Order { } } + return $data; + } + + /** Append downloadable documents from order products if single order is requested + * @param $data + * @param $order + * @param $request + */ + private static function _append_documents($data, $order, $request) { + + // Go trough available line items and append documents for wiaas standard packages line items + foreach ($data['line_items'] as $index => $product_line) { + $order_item = $order->get_item($product_line['id']); + + if (wiaas_is_order_item__standard_package($order_item)) { + $documents = wiaas_get_standard_package_order_item_documents($order, $product_line['id']); + $data['line_items'][$index] ['documents'] = $documents; + } + + } + return $data; } } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-package.php b/backend/app/plugins/wiaas/includes/class-wiaas-package.php index 4ae92ed..5c4dea8 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-package.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-package.php @@ -58,7 +58,7 @@ class Wiaas_Package { unset($doc['url']); unset($doc['version']); return $doc; - }, wiaas_get_all_package_documents($package, true)); + }, wiaas_get_standard_package_documents($package, true)); return $data; } diff --git a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php index 71a3c97..df4b6f0 100644 --- a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php +++ b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-download.php @@ -9,23 +9,24 @@ class Wiaas_Document_Download { public static function init() { // register download trigger for downloads from admin panel - if ( is_admin() && isset($_GET['wiaasdoc']) ) { + if ( isset($_GET['wiaasdoc']) ) { add_action( 'init', array( __CLASS__, 'admin_download' ) ); } } /** - * Download document from admin panel + * Since only REST api uses JWT Authentication this endpoint will work only for users + * that are logged in directly to wordpress */ public static function admin_download() { if (!is_user_logged_in()) { - wp_die( __( 'No Access.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'No Access.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 403 ) ); } $document_id = (int)$_GET['wiaasdoc']; if ($document_id <= 0) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $version = isset($_GET['v']) ? (int)$_GET['v'] : null; @@ -44,7 +45,7 @@ class Wiaas_Document_Download { $file_path = wiaas_get_document_version_path($version); if (!file_exists($file_path)) { - wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) ); } WC_Download_Handler::download_file_force( @@ -64,15 +65,15 @@ class Wiaas_Document_Download { public static function download_order_item_document($order_id, $item_id, $type, $document_key) { $order = wc_get_order($order_id); if (!$order) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $item = $order->get_item($item_id); if (!$item) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } - $item_documents = $item['wiaas_documents']; + $item_documents = wiaas_get_standard_package_order_item_documents($order, $item_id); $order_document = null; foreach ($item_documents as $item_document) { @@ -83,13 +84,13 @@ class Wiaas_Document_Download { } if (!isset($order_document)) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $file_path = wiaas_get_document_version_path($order_document['version']); if (!file_exists($file_path)) { - wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) ); } WC_Download_Handler::download_file_force( @@ -109,25 +110,25 @@ class Wiaas_Document_Download { public static function download_cart_document($item_key, $type, $document_key) { $item = WC()->cart->get_cart_item($item_key); if (!isset($item)) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $item_documents = $item['_wiaas_documents']; if (!isset($item_documents) || empty($item_documents) || !array_key_exists($type, $item_documents)) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $cart_document = $item_documents[$type]; if ($cart_document['key'] !== $document_key) { - wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $file_path = wiaas_get_document_version_path($cart_document['version']); if (!file_exists($file_path)) { - wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) ); + wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) ); } WC_Download_Handler::download_file_force( diff --git a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-upload.php b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-upload.php index 9e7cdee..87211ab 100644 --- a/backend/app/plugins/wiaas/includes/document/class-wiaas-document-upload.php +++ b/backend/app/plugins/wiaas/includes/document/class-wiaas-document-upload.php @@ -34,7 +34,7 @@ class Wiaas_Document_Upload { // validate file if (!isset($_FILES[$file_id])) { - return new WP_Error( 'wiaas_upload_missing_file', 'No file!' ); + return new WP_Error( 'wiaas_upload_error_missing_file', 'No file!' ); } $file_name = $_FILES[$file_id]['name']; @@ -45,7 +45,7 @@ class Wiaas_Document_Upload { $ext = strtolower($ext); if(!in_array($ext, self::$allowed_extensions)) { - return new WP_Error( 'wiaas_upload_error', 'Invalid file extension!' ); + return new WP_Error( 'wiaas_upload_error_invalid_extension', 'Invalid file extension!' ); } $file = wp_handle_upload( diff --git a/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php b/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php index eae9beb..a6d43f5 100644 --- a/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php +++ b/backend/app/plugins/wiaas/includes/document/wiaas-document-functions.php @@ -91,7 +91,7 @@ function wiaas_get_object_attached_documents($object_id) { } /** - * Retrieve all documents for single wiaas package + * Retrieve all documents for single standard wiaas package * * this includes: * - package linked documents @@ -104,7 +104,7 @@ function wiaas_get_object_attached_documents($object_id) { * * @return array */ -function wiaas_get_all_package_documents($package, $only_visible = false) { +function wiaas_get_standard_package_documents($package, $only_visible = false) { // retrieve addons and option packages ids $all_package_ids = array_merge( Wiaas_Package_Addon::get_package_addons_ids($package), @@ -143,14 +143,14 @@ function wiaas_get_all_package_documents($package, $only_visible = false) { } /** - * Retrieve all documents for single order package item + * Retrieve all documents for single order standard package item * * @param WC_Order $order * @param int $package_item_id * * @return array */ -function wiaas_get_package_order_item_documents($order, $package_item_id) { +function wiaas_get_standard_package_order_item_documents($order, $package_item_id) { $order_items = $order->get_items( 'line_item' ); $package_order_item = $order->get_item($package_item_id); diff --git a/backend/app/plugins/wiaas/includes/package/wiaas-package-functions.php b/backend/app/plugins/wiaas/includes/package/wiaas-package-functions.php index 26cd623..462ec94 100644 --- a/backend/app/plugins/wiaas/includes/package/wiaas-package-functions.php +++ b/backend/app/plugins/wiaas/includes/package/wiaas-package-functions.php @@ -88,4 +88,14 @@ function wiaas_get_cart_item_addons($cart_item) { } return $addon_cart_items; +} + +/** + * Check if order item is wiaas standard package + * @param WC_Order_Item $item + * + * @return bool + */ +function wiaas_is_order_item__standard_package($item) { + return isset($item['wiaas_standard_package']); } \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php index b330dfb..6d78633 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php @@ -198,4 +198,28 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { $this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']); $this->assertArrayHasKey('phone', $transformed_order_response['commercial_lead']); } + + /** + * @covers Wiaas_Order::transform_rest_order() + */ + function test_order_rest_response_has_empty_documents_when_package_has_no_documents() { + $order_response = array( + 'customer_id' => $this->customer_id, + 'status' => 'processing', + 'line_items' => array() + ); + $request = array( 'id' => $this->order_id); + + $order_rest_response = new WP_REST_Response($order_response); + + $order_rest_response = Wiaas_Order::transform_rest_order( + $order_rest_response, + wc_get_order($this->order_id), + $request); + + $transformed_order_response = $order_rest_response->get_data(); + + $this->assertNotNull($transformed_order_response['documents']); + $this->assertTrue(is_array($transformed_order_response['documents'])); + } } \ No newline at end of file diff --git a/frontend/src/actions/cart/cartActions.js b/frontend/src/actions/cart/cartActions.js index 4384c4d..012da89 100644 --- a/frontend/src/actions/cart/cartActions.js +++ b/frontend/src/actions/cart/cartActions.js @@ -70,6 +70,10 @@ export const fetchCartCount = () => { } } +export const fetchCartNotices = () => { + +}; + export const fetchCartItems = (isForSteps = false) => { return dispatch => { dispatch(requestShopCartItems()); @@ -326,6 +330,19 @@ export const placeOrder = (orderInfo) => { project_id: orderDetails.details.idProject, delivery_address_id: orderDetails.delivery.id, billing_address_id: orderDetails.billing.id, + shipping_city: orderDetails.delivery.city, + shipping_country: orderDetails.delivery.countryName, + shipping_address_1: orderDetails.delivery.detailedAddress, + shipping_postcode: orderDetails.delivery.zipCode, + shipping_first_name: orderDetails.delivery.firstName, + shipping_last_name: orderDetails.delivery.lastName, + billing_city: orderDetails.billing.city, + billing_country: orderDetails.billing.countryName, + billing_address_1: orderDetails.billing.detailedAddress, + billing_postcode: orderDetails.billing.zipCode, + billing_first_name: orderDetails.billing.firstName, + billing_last_name: orderDetails.billing.lastName, + billing_last_email: orderDetails.billing.invoiceMail } }).then(response => { if (typeof response.data !== 'undefined' && 'messages' in response.data) { diff --git a/frontend/src/containers/cart/components/CartUploadDocument.jsx b/frontend/src/containers/cart/components/CartUploadDocument.jsx index af84eba..4ad72e3 100644 --- a/frontend/src/containers/cart/components/CartUploadDocument.jsx +++ b/frontend/src/containers/cart/components/CartUploadDocument.jsx @@ -37,13 +37,13 @@ class CartUploadDocument extends Component { downloadDocument(document){ fileHandler.download( `${API_SERVER}/wp-json/wiaas/cart/items/${this.props.cartItem.key}/documents/${this.props.idDocumentType}?document_key=${document.key}`, - document.name); + `${document.name}.${document.extension}`); } downloadTemplate(document) { fileHandler.download( `${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`, - document.name); + `${document.name}.${document.extension}`); } render() { diff --git a/frontend/src/containers/coMarket/components/PackageInfo.jsx b/frontend/src/containers/coMarket/components/PackageInfo.jsx index b678e24..5a4907e 100644 --- a/frontend/src/containers/coMarket/components/PackageInfo.jsx +++ b/frontend/src/containers/coMarket/components/PackageInfo.jsx @@ -10,7 +10,7 @@ class PackageInfo extends Component { downloadDocument(document){ fileHandler.download( `${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`, - document.name); + `${document.name}.${document.extension}`); } render() { diff --git a/frontend/src/containers/orders/ProcessContainer.jsx b/frontend/src/containers/orders/ProcessContainer.jsx index 4c04de0..7ae0345 100644 --- a/frontend/src/containers/orders/ProcessContainer.jsx +++ b/frontend/src/containers/orders/ProcessContainer.jsx @@ -140,9 +140,6 @@ class ProcessContainer extends Component { } onViewChange(activeView){ - if(activeView === 'documents'){ - this.props.dispatch(fetchOrderInfo(this.props.idOrder)); - } this.setState({activeView}); } @@ -232,7 +229,7 @@ class ProcessContainer extends Component { { this.state.activeView === 'documents' && - + } } diff --git a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx index 29f8af1..b638813 100644 --- a/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx +++ b/frontend/src/containers/orders/components/OrderDocumentsGroup.jsx @@ -5,20 +5,6 @@ import FileDownloader from '../../../helpers/FileDownloader'; const fileHandler = new FileDownloader(); -const iconTypes = { - doc: 'file-word-o', - docx: 'file-word-o', - odt: 'file-word-o', - ods: 'file-excel-o', - pdf: 'file-pdf-o', - png: 'file-image-o', - jpg: 'file-image-o', - xls: 'file-excel-o', - xlsx: 'file-excel-o', - ppt: 'file-powerpoint-o', - pptx: 'file-powerpoint-o' -}; - class OrderDocumentsGroup extends Component { constructor(props) { super(props); @@ -26,13 +12,9 @@ class OrderDocumentsGroup extends Component { this.state = {}; } - getDocumentIcon(documentType) { - return iconTypes[documentType] || 'file'; - } - downloadDocument(orderId, itemId, document){ const fileUrl = `${API_SERVER}/wp-json/wiaas/documents/order/${orderId}/${document.type}?item_id=${itemId}&document_key=${document.key}`; - fileHandler.download(fileUrl, document.name); + fileHandler.download(fileUrl, `${document.name}.${document.extension}`); } render() { @@ -47,7 +29,7 @@ class OrderDocumentsGroup extends Component { { documentsGroup.documents.map(document =>
{this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big"> - +
{document.name}
diff --git a/frontend/src/containers/orders/components/packages/PackageInfo.jsx b/frontend/src/containers/orders/components/packages/PackageInfo.jsx index 66d086e..a79bac8 100644 --- a/frontend/src/containers/orders/components/packages/PackageInfo.jsx +++ b/frontend/src/containers/orders/components/packages/PackageInfo.jsx @@ -22,12 +22,6 @@ class PackageInfo extends Component { }); } - downloadDocument(document){ - const fileUrl = `${API_SERVER}/utils/api/downloadFile?idDocument=${document.idDocument}&fileName=${document.documentName}.${document.extension}&fileType=${document.documentTypeName}` - const fileName = document.documentName + '.' + document.extension; - fileHandler.download(fileUrl, fileName); - } - render() { const {orderPackage, currency, onViewChange} = this.props; const shouldShowPriceInfo = orderPackage.recurringPrice > 0 || orderPackage.servicePrice > 0; @@ -122,17 +116,6 @@ class PackageInfo extends Component { } } - { - orderPackage.documents && - -
{orderTexts.labels.DOCUMENTS}:
- { - orderPackage.documents.length > 0 ? -
onViewChange('documents')}>{orderTexts.buttons.SEE_DOCUMENTS}
: - {orderTexts.labels.NO_DOCUMENTS} - } - - } ); diff --git a/frontend/src/helpers/OrderHelper.js b/frontend/src/helpers/OrderHelper.js index df8394e..1b585c3 100644 --- a/frontend/src/helpers/OrderHelper.js +++ b/frontend/src/helpers/OrderHelper.js @@ -38,6 +38,7 @@ export const fromWCOrder = (WCOrder) => { email: WCOrder.billing.email, address: formatAddress(WCOrder.billing) }, + documents: WCOrder.documents || [], packages: WCOrder['line_items'].map(packageLine => { return { id: packageLine['product_id'], @@ -64,10 +65,10 @@ export const fromWCOrder = (WCOrder) => { packageName: packageOption.name, groupName: packageOption['group_name'] || '', })) : [], - documents: packageLine.documents.map(document => { + documents: packageLine.documents ? packageLine.documents.map(document => { document['icon'] = getDocumentIcon(document.extension); return document; - }) || [] + }) : [] }; }), process: processInfo,