Handle order project and refactor api
This commit is contained in:
@@ -6,348 +6,224 @@ class Wiaas_Cart_API {
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $namespace = 'wiaas/cart';
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
private static $rest_base = 'cart';
|
||||
|
||||
public static function register_routes() {
|
||||
register_rest_route( self::$namespace, 'count', array(
|
||||
'methods' => 'GET',
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/count', array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array(__CLASS__, 'get_cart_count'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'items', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_cart_items'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/items', array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array(__CLASS__, 'get_cart_items'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array(__CLASS__, 'add_package_to_cart'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'package_id' => array(
|
||||
'description' => __( 'Wiaas package ID.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
'price_id' => array(
|
||||
'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
'options_ids' => array(
|
||||
'description' => __( 'Wiaas package options IDs.', 'wiaas' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
),
|
||||
'addons_ids' => array(
|
||||
'description' => __( 'Wiaas package addons IDs.', 'wiaas' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'documents', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_cart_documents'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/items/(?P<key>[\w-]+)', array(
|
||||
'args' => array(
|
||||
'key' => array(
|
||||
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::DELETABLE,
|
||||
'callback' => array(__CLASS__, 'remove_package_from_cart'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array(__CLASS__, 'update_package_quantity'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'quantity' => array(
|
||||
'description' => __( 'New quantity cart package item.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
)
|
||||
)
|
||||
)
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'add', array(
|
||||
'methods' => 'post',
|
||||
'callback' => array(__CLASS__, 'add_package_to_cart'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
register_rest_route( self::$namespace, '/' . self::$rest_base . '/checkout', array(
|
||||
'methods' => WP_REST_Server::CREATABLE,
|
||||
'callback' => array(__CLASS__, 'checkout'),
|
||||
'permission_callback' => 'is_user_logged_in',
|
||||
'args' => array(
|
||||
'vat' => array(
|
||||
'description' => __( 'Vat Code for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'company' => array(
|
||||
'description' => __( 'Company name for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'reference' => array(
|
||||
'description' => __( 'Location details for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'tender' => array(
|
||||
'description' => __( 'Invoice reference for new order.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'project_id' => array(
|
||||
'description' => __( 'Order project ID for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'delivery_address_id' => array(
|
||||
'description' => __( 'ID of delivery address for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'billing_address_id' => array(
|
||||
'description' => __( 'ID of billing address for new order.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
)
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'remove', array(
|
||||
'methods' => 'post',
|
||||
'callback' => array(__CLASS__, 'remove_package_from_cart'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'update-quantity', array(
|
||||
'methods' => 'post',
|
||||
'callback' => array(__CLASS__, 'update_package_quantity'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'place-order', array(
|
||||
'methods' => 'post',
|
||||
'callback' => array(__CLASS__, 'place_order'),
|
||||
'permission_callback' => array( __CLASS__, 'permission_check' ),
|
||||
) );
|
||||
}
|
||||
|
||||
public static function permission_check() {
|
||||
if (!is_user_logged_in()) {
|
||||
return new WP_Error( 'wiaas_rest_authorization_required',
|
||||
__( 'Sorry, only authorized users can access!', 'wiaas' ),
|
||||
array( 'status' => rest_authorization_required_code() ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart count
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function get_cart_count() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$count = 0;
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (isset($item['_wiaas_standard_package'])) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'count' => $count,
|
||||
return rest_ensure_response(array(
|
||||
'count' => Wiaas_Cart::get_cart_packages_count(),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart items
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function get_cart_items() {
|
||||
$items = WC()->cart->get_cart_contents();
|
||||
|
||||
$result = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (!isset($item['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package = wc_get_product($item['product_id']);
|
||||
|
||||
// Retrieve package addons
|
||||
$addon_cart_items = wiaas_get_cart_item_addons($item);
|
||||
$additional_packages = array();
|
||||
|
||||
foreach ($addon_cart_items as $addon_cart_item) {
|
||||
$additional_package = wc_get_product($addon_cart_item['product_id']);
|
||||
$additional_packages[] = array(
|
||||
'idAdditionalPackage' => $additional_package->get_id(),
|
||||
'packageName' => $additional_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $addon_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentExtra' => $addon_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesExtra' => $addon_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Retrieve package options
|
||||
$option_cart_items = wiaas_get_cart_item_options($item);
|
||||
$package_options = array();
|
||||
foreach ($option_cart_items as $option_cart_item) {
|
||||
$option_package = wc_get_product($option_cart_item['product_id']);
|
||||
$package_options[] = array(
|
||||
'idOptionPackage' => $option_package->get_id(),
|
||||
'groupName' => $option_cart_item['_wiaas_option_group_name'],
|
||||
'packageName' => $option_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $option_cart_item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentExtra' => $option_cart_item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesExtra' => $option_cart_item['_wiaas_payment']['services_extra'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$totalPrices = Wiaas_Cart::get_cart_item_total($item);
|
||||
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
return rest_ensure_response(array(
|
||||
'items' => Wiaas_Cart::get_cart_packages(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
$result[] = array(
|
||||
'idPackage' => $item['product_id'],
|
||||
'key' => $item['key'],
|
||||
'packageName' => $package->get_title(),
|
||||
'additionalPackages' => $additional_packages,
|
||||
'areAdditionalAvailable' => true,
|
||||
'areOptionsAvailable' => true,
|
||||
'bids' => array(),
|
||||
'commercialLead' => 'Coor Service Management',
|
||||
'country' => array(
|
||||
'currency' => $country['currency']
|
||||
),
|
||||
'options' => $package_options,
|
||||
'quantity' => $item['quantity'],
|
||||
/**
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function add_package_to_cart($request) {
|
||||
|
||||
'idPayType' => $item['_wiaas_payment']['id'],
|
||||
'payType' => $item['_wiaas_payment']['payment_type'],
|
||||
'periodUnit' => $item['_wiaas_payment']['period_unit'],
|
||||
'idPrice' => $item['_wiaas_payment']['id'],
|
||||
'fixedPrice' => $item['_wiaas_payment']['fixed_extra'],
|
||||
'recurrentPrice' => $item['_wiaas_payment']['recurrent_extra'],
|
||||
'servicesPrice' => $item['_wiaas_payment']['services_extra'],
|
||||
|
||||
'totalPrices' => array(
|
||||
'fixedPrice' => $totalPrices['fixed_extra'],
|
||||
'recurrentPrice' => $totalPrices['recurrent_extra'],
|
||||
'servicesPrice' => $totalPrices['services_extra'],
|
||||
),
|
||||
|
||||
'status' => 'available',
|
||||
'idCommercialLead' => 14,
|
||||
);
|
||||
}
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'cartItems' => $result,
|
||||
'items' => $items,
|
||||
'totalPrice' => ''
|
||||
)
|
||||
$success = Wiaas_Cart::add_package_to_cart(
|
||||
$request['package_id'],
|
||||
$request['price_id'],
|
||||
$request['addons_ids'],
|
||||
$request['options_ids']
|
||||
);
|
||||
|
||||
if ($success) {
|
||||
return wiaas_api_notice('PACKAGE_ADDED', 'success');
|
||||
}
|
||||
|
||||
return wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cart documents
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function get_cart_documents() {
|
||||
return new WP_REST_Response(array(
|
||||
'areFilesUploaded' => true,
|
||||
'templates' => array(),
|
||||
'uploaded' => array()
|
||||
));
|
||||
}
|
||||
public static function remove_package_from_cart($request) {
|
||||
|
||||
/**
|
||||
* Add package to cart
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @return WP_REST_Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function add_package_to_cart() {
|
||||
$package_id = $_POST['package_id'];
|
||||
|
||||
$success = WC()->cart->add_to_cart($package_id, 1, 0, array(), array(
|
||||
'_wiaas_standard_package' => true
|
||||
));
|
||||
$success = Wiaas_Cart::remove_package_from_cart($request['key']);
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'PACKAGE_ADDED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success');
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'error',
|
||||
'message' => 'PACKAGE_ALREADY_IN_CART'
|
||||
)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove package from cart
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
*/
|
||||
public static function remove_package_from_cart() {
|
||||
$package_cart_key = $_POST['package_item_key'];
|
||||
|
||||
$success = WC()->cart->remove_cart_item($package_cart_key);
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'PACKAGE_REMOVED_FROM_CART'
|
||||
)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'error',
|
||||
'message' => 'INVALID_PACKAGE_FOR_REMOVE'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update package quantity in cart
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @return WP_REST_Response
|
||||
*/
|
||||
public static function update_package_quantity() {
|
||||
$package_item_key = $_POST['package_item_key'];
|
||||
$new_quantity = $_POST['quantity'];
|
||||
public static function update_package_quantity($request) {
|
||||
|
||||
$success = WC()->cart->set_quantity($package_item_key, $new_quantity, true);
|
||||
$success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']);
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'QUANTITY_UPDATED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('QUANTITY_UPDATED', 'success');
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'error',
|
||||
'message' => 'QUANTITY_NOT_UPDATED'
|
||||
)
|
||||
)
|
||||
));
|
||||
return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Placing order as part of checkout process
|
||||
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
|
||||
* @param WP_REST_Request $request Request data.
|
||||
*
|
||||
* @return WP_REST_Response
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function place_order() {
|
||||
public static function checkout($request) {
|
||||
|
||||
$details = $_POST['details'];
|
||||
$vat_code = $_POST['vat'];
|
||||
$company_name = $_POST['companyName'];
|
||||
$delivery_address = $_POST['delivery'];
|
||||
$billing_address = $_POST['billing'];
|
||||
Wiaas_Checkout::process_checkout($request->get_body_params());
|
||||
|
||||
$order_id = WC()->checkout()->create_order(array());
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
$order->set_shipping_city($delivery_address['city']);
|
||||
$order->set_shipping_country($delivery_address['countryName']);
|
||||
$order->set_shipping_address_1($delivery_address['detailedAddress']);
|
||||
$order->set_shipping_postcode($delivery_address['zipCode']);
|
||||
|
||||
$order->set_billing_city($billing_address['city']);
|
||||
$order->set_billing_country($billing_address['countryName']);
|
||||
$order->set_billing_address_1($billing_address['detailedAddress']);
|
||||
$order->set_billing_postcode($billing_address['zipCode']);
|
||||
$order->set_billing_first_name($billing_address['firstName']);
|
||||
$order->set_billing_last_name($billing_address['lastName']);
|
||||
|
||||
$order->payment_complete();
|
||||
|
||||
|
||||
add_post_meta($order_id, '_wiaas_vat_code', $vat_code);
|
||||
add_post_meta($order_id, '_wiaas_company_name', $company_name);
|
||||
add_post_meta($order_id, '_wiaas_project_id', $details['idProject']);
|
||||
add_post_meta($order_id, '_wiaas_reference', $details['reference']);
|
||||
add_post_meta($order_id, '_wiaas_tender', $details['tender']);
|
||||
|
||||
// $order->get_li
|
||||
|
||||
WC()->cart->empty_cart( true );
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'messages' => array(
|
||||
array(
|
||||
'code' => 'success',
|
||||
'message' => 'ORDER_PLACED'
|
||||
)
|
||||
),
|
||||
'details' => $details
|
||||
));
|
||||
return wiaas_api_notice('ORDER_PLACED', 'success');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user