Checkout logic

This commit is contained in:
Almira Krdzic
2018-10-04 03:15:51 +02:00
18 changed files with 285 additions and 147 deletions

View File

@@ -229,11 +229,13 @@ class Wiaas_Cart_API {
$request['options_ids'] $request['options_ids']
); );
if ($success) { if (!$success) {
return wiaas_api_notice('PACKAGE_ADDED', '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']); $success = Wiaas_Cart::remove_package_from_cart($request['key']);
if ($success) { if (!$success) {
return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', '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']); $success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']);
if ($success) { if (!$success) {
return wiaas_api_notice('QUANTITY_UPDATED', '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 * @return mixed|WP_REST_Response
*/ */
public static function upload_cart_document($request) { 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)) { if (!$success) {
$error_code = $result->get_error_code(); return (wc_notice_count('error') > 0) ?
wiaas_api_cart_error_notices() :
if ($error_code === 'wiaas_upload_missing_file') { wiaas_api_generate_error('UPLOAD_ERROR');
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');
} }
return wiaas_api_notice('FILE_UPLOADED', 'success', array( return wiaas_api_notice('FILE_UPLOADED', 'success');
'id_document' => $result,
));
} }
/** /**
@@ -325,6 +321,10 @@ class Wiaas_Cart_API {
Wiaas_Checkout::process_checkout($request->get_body_params()); 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'); return wiaas_api_notice('ORDER_PLACED', 'success');
} }
} }

View File

@@ -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 * Generate REST API error
* *

View File

@@ -26,7 +26,6 @@ class Wiaas_Cart {
add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1); 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); 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) { public static function upload_cart_document($doc_type, $package_item_key) {
try { try {
$version = Wiaas_Document_Upload::upload_document_version(); $result = Wiaas_Document_Upload::upload_document_version();
if (is_wp_error($version)) { // File upload failed
return $version; 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( // save uploaded cart documents info to cart item
'version' => $version, WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = array(
'version' => $result,
'key' => wp_generate_uuid4(), 'key' => wp_generate_uuid4(),
); );;
// persist uploaded cart document in cart // persist changes
WC()->cart->cart_contents[ $package_item_key ]['_wiaas_documents'][$doc_type] = $cart_document; WC()->cart->calculate_totals();
// persist changes to cart session return true;
// 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'];
} catch( Exception $e) { } 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) { public static function add_package_to_cart($package_id, $price_id, $addons_ids, $options_ids) {
// try adding package to cart // try adding package to cart
try { 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 //Check if package exists
$package = wc_get_product( $package_id ); $package = wc_get_product( $package_id );
if (!$package) { if (!$package) {
wc_add_notice('Package does not exists!', 'error');
return false; 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); $cart_item_key = WC()->cart->add_to_cart($package_id, 1, 0, array(), $wiaas_cart_item_data);
if (!$cart_item_key) { if (!$cart_item_key) {
wc_add_notice('Package could not be added to cart!', 'error');
return false; 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_addon_items'] = $addon_items_keys;
WC()->cart->cart_contents[ $package_cart_item_key ]['_wiaas_option_items'] = $option_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(); Wiaas_Cart::init();

View File

@@ -12,49 +12,41 @@ class Wiaas_Checkout {
public static function process_checkout($data) { public static function process_checkout($data) {
try { 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 ); $order = wc_get_order( $order_id );
// set order currency if ( is_wp_error( $order_id ) ) {
$line_items = $order->get_items(); throw new Exception( $order_id->get_error_message() );
foreach ($line_items as $line_item) {
if (isset($line_item['wiaas_currency'])) {
$order->set_currency($line_item['wiaas_currency']);
break;
}
} }
$delivery_address = Wiaas_Customer::get_customer_profile_address($customer->ID, $data['delivery_address_id']); if ( ! $order ) {
$billing_address = Wiaas_Customer::get_customer_billing_address($customer->ID, $data['billing_address_id']); throw new Exception( __( 'Unable to create order.', 'woocommerce' ) );
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 (isset($billing_address)) { self::_add_wiaas_checkout_data($order, $data);
$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']);
}
Wiaas_Order::set_order_vat($order_id, $data['vat']); do_action( 'woocommerce_checkout_order_processed', $order_id, array(), $order );
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']);
}
$order->payment_complete(); $order->payment_complete();
@@ -63,7 +55,35 @@ class Wiaas_Checkout {
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
wc_add_notice( $e->getMessage(), 'error' );
return false; 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']);
}
}
} }

View File

@@ -118,6 +118,8 @@ class Wiaas_Order {
$data = self::_append_order_process($data, $order, $request); $data = self::_append_order_process($data, $order, $request);
$data = self::_append_order_comments($data, $order, $request); $data = self::_append_order_comments($data, $order, $request);
$data = self::_append_documents($data, $order, $request);
} }
$response->set_data($data); $response->set_data($data);
@@ -242,10 +244,6 @@ class Wiaas_Order {
// add only product lines that represent product bundles // add only product lines that represent product bundles
if (isset($item['wiaas_standard_package'])) { 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 # get payment type info
$product_line['payment_type'] = $item['wiaas_payment_type']; $product_line['payment_type'] = $item['wiaas_payment_type'];
$product_line['service_price'] = floatval($item['wiaas_services_extra']); $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; return $data;
} }
} }

View File

@@ -58,7 +58,7 @@ class Wiaas_Package {
unset($doc['url']); unset($doc['url']);
unset($doc['version']); unset($doc['version']);
return $doc; return $doc;
}, wiaas_get_all_package_documents($package, true)); }, wiaas_get_standard_package_documents($package, true));
return $data; return $data;
} }

View File

@@ -9,23 +9,24 @@ class Wiaas_Document_Download {
public static function init() { public static function init() {
// register download trigger for downloads from admin panel // 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' ) ); 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() { public static function admin_download() {
if (!is_user_logged_in()) { 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']; $document_id = (int)$_GET['wiaasdoc'];
if ($document_id <= 0) { 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; $version = isset($_GET['v']) ? (int)$_GET['v'] : null;
@@ -44,7 +45,7 @@ class Wiaas_Document_Download {
$file_path = wiaas_get_document_version_path($version); $file_path = wiaas_get_document_version_path($version);
if (!file_exists($file_path)) { 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( 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) { public static function download_order_item_document($order_id, $item_id, $type, $document_key) {
$order = wc_get_order($order_id); $order = wc_get_order($order_id);
if (!$order) { 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); $item = $order->get_item($item_id);
if (!$item) { 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; $order_document = null;
foreach ($item_documents as $item_document) { foreach ($item_documents as $item_document) {
@@ -83,13 +84,13 @@ class Wiaas_Document_Download {
} }
if (!isset($order_document)) { 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']); $file_path = wiaas_get_document_version_path($order_document['version']);
if (!file_exists($file_path)) { 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( 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) { public static function download_cart_document($item_key, $type, $document_key) {
$item = WC()->cart->get_cart_item($item_key); $item = WC()->cart->get_cart_item($item_key);
if (!isset($item)) { 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']; $item_documents = $item['_wiaas_documents'];
if (!isset($item_documents) || if (!isset($item_documents) ||
empty($item_documents) || empty($item_documents) ||
!array_key_exists($type, $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]; $cart_document = $item_documents[$type];
if ($cart_document['key'] !== $document_key) { 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']); $file_path = wiaas_get_document_version_path($cart_document['version']);
if (!file_exists($file_path)) { 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( WC_Download_Handler::download_file_force(

View File

@@ -34,7 +34,7 @@ class Wiaas_Document_Upload {
// validate file // validate file
if (!isset($_FILES[$file_id])) { 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']; $file_name = $_FILES[$file_id]['name'];
@@ -45,7 +45,7 @@ class Wiaas_Document_Upload {
$ext = strtolower($ext); $ext = strtolower($ext);
if(!in_array($ext, self::$allowed_extensions)) { 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( $file = wp_handle_upload(

View File

@@ -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: * this includes:
* - package linked documents * - package linked documents
@@ -104,7 +104,7 @@ function wiaas_get_object_attached_documents($object_id) {
* *
* @return array * @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 // retrieve addons and option packages ids
$all_package_ids = array_merge( $all_package_ids = array_merge(
Wiaas_Package_Addon::get_package_addons_ids($package), 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 WC_Order $order
* @param int $package_item_id * @param int $package_item_id
* *
* @return array * @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' ); $order_items = $order->get_items( 'line_item' );
$package_order_item = $order->get_item($package_item_id); $package_order_item = $order->get_item($package_item_id);

View File

@@ -88,4 +88,14 @@ function wiaas_get_cart_item_addons($cart_item) {
} }
return $addon_cart_items; 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']);
} }

View File

@@ -198,4 +198,28 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case {
$this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']); $this->assertArrayHasKey('email', $transformed_order_response['commercial_lead']);
$this->assertArrayHasKey('phone', $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']));
}
} }

View File

@@ -70,6 +70,10 @@ export const fetchCartCount = () => {
} }
} }
export const fetchCartNotices = () => {
};
export const fetchCartItems = (isForSteps = false) => { export const fetchCartItems = (isForSteps = false) => {
return dispatch => { return dispatch => {
dispatch(requestShopCartItems()); dispatch(requestShopCartItems());
@@ -326,6 +330,19 @@ export const placeOrder = (orderInfo) => {
project_id: orderDetails.details.idProject, project_id: orderDetails.details.idProject,
delivery_address_id: orderDetails.delivery.id, delivery_address_id: orderDetails.delivery.id,
billing_address_id: orderDetails.billing.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 => { }).then(response => {
if (typeof response.data !== 'undefined' && 'messages' in response.data) { if (typeof response.data !== 'undefined' && 'messages' in response.data) {

View File

@@ -37,13 +37,13 @@ class CartUploadDocument extends Component {
downloadDocument(document){ downloadDocument(document){
fileHandler.download( fileHandler.download(
`${API_SERVER}/wp-json/wiaas/cart/items/${this.props.cartItem.key}/documents/${this.props.idDocumentType}?document_key=${document.key}`, `${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) { downloadTemplate(document) {
fileHandler.download( fileHandler.download(
`${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`, `${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`,
document.name); `${document.name}.${document.extension}`);
} }
render() { render() {

View File

@@ -10,7 +10,7 @@ class PackageInfo extends Component {
downloadDocument(document){ downloadDocument(document){
fileHandler.download( fileHandler.download(
`${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`, `${API_SERVER}/wp-json/wiaas/documents?document_id=${document.id}`,
document.name); `${document.name}.${document.extension}`);
} }
render() { render() {

View File

@@ -140,9 +140,6 @@ class ProcessContainer extends Component {
} }
onViewChange(activeView){ onViewChange(activeView){
if(activeView === 'documents'){
this.props.dispatch(fetchOrderInfo(this.props.idOrder));
}
this.setState({activeView}); this.setState({activeView});
} }
@@ -232,7 +229,7 @@ class ProcessContainer extends Component {
{ {
this.state.activeView === 'documents' && this.state.activeView === 'documents' &&
<OrderDocuments idOrder={orderInfo.id} /> <OrderDocuments orderDocumentsBundle={orderInfo.documents} />
} }
</div> </div>
} }

View File

@@ -5,20 +5,6 @@ import FileDownloader from '../../../helpers/FileDownloader';
const fileHandler = new 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 { class OrderDocumentsGroup extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@@ -26,13 +12,9 @@ class OrderDocumentsGroup extends Component {
this.state = {}; this.state = {};
} }
getDocumentIcon(documentType) {
return iconTypes[documentType] || 'file';
}
downloadDocument(orderId, itemId, document){ downloadDocument(orderId, itemId, document){
const fileUrl = `${API_SERVER}/wp-json/wiaas/documents/order/${orderId}/${document.type}?item_id=${itemId}&document_key=${document.key}`; 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() { render() {
@@ -47,7 +29,7 @@ class OrderDocumentsGroup extends Component {
{ {
documentsGroup.documents.map(document => <a id={'document-' + document.key} key={'order-document-' + document.key}> documentsGroup.documents.map(document => <a id={'document-' + document.key} key={'order-document-' + document.key}>
<div onClick={() => {this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big"> <div onClick={() => {this.downloadDocument(orderId, documentsGroup.orderItemId, document)}} className="document-link-big">
<i className={'fa fa-4x fa-' + this.getDocumentIcon(document.extension)} aria-hidden="true"></i> <i className={`fa fa-4x fa-${document.icon}`} aria-hidden="true"></i>
<div> <div>
{document.name} {document.name}
</div> </div>

View File

@@ -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() { render() {
const {orderPackage, currency, onViewChange} = this.props; const {orderPackage, currency, onViewChange} = this.props;
const shouldShowPriceInfo = orderPackage.recurringPrice > 0 || orderPackage.servicePrice > 0; const shouldShowPriceInfo = orderPackage.recurringPrice > 0 || orderPackage.servicePrice > 0;
@@ -122,17 +116,6 @@ class PackageInfo extends Component {
} }
</Col> </Col>
} }
{
orderPackage.documents &&
<Col xl="2">
<div className="subtitle"><h6>{orderTexts.labels.DOCUMENTS}:</h6></div>
{
orderPackage.documents.length > 0 ?
<div className="link-to-docs" onClick={()=>onViewChange('documents')}>{orderTexts.buttons.SEE_DOCUMENTS}</div>:
<span>{orderTexts.labels.NO_DOCUMENTS}</span>
}
</Col>
}
</Row> </Row>
</Container> </Container>
); );

View File

@@ -38,6 +38,7 @@ export const fromWCOrder = (WCOrder) => {
email: WCOrder.billing.email, email: WCOrder.billing.email,
address: formatAddress(WCOrder.billing) address: formatAddress(WCOrder.billing)
}, },
documents: WCOrder.documents || [],
packages: WCOrder['line_items'].map(packageLine => { packages: WCOrder['line_items'].map(packageLine => {
return { return {
id: packageLine['product_id'], id: packageLine['product_id'],
@@ -64,10 +65,10 @@ export const fromWCOrder = (WCOrder) => {
packageName: packageOption.name, packageName: packageOption.name,
groupName: packageOption['group_name'] || '', groupName: packageOption['group_name'] || '',
})) : [], })) : [],
documents: packageLine.documents.map(document => { documents: packageLine.documents ? packageLine.documents.map(document => {
document['icon'] = getDocumentIcon(document.extension); document['icon'] = getDocumentIcon(document.extension);
return document; return document;
}) || [] }) : []
}; };
}), }),
process: processInfo, process: processInfo,