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']
);
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');
}
}

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
*