WP_REST_Server::READABLE, 'callback' => array(__CLASS__, 'get_cart_count'), 'permission_callback' => 'is_user_logged_in', ) ); 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, '/' . self::$rest_base . '/items/(?P[\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, '/' . 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', ), ) ) ); } /** * Get cart count * @return WP_REST_Response */ public static function get_cart_count() { return rest_ensure_response(array( 'count' => Wiaas_Cart::get_cart_packages_count(), )); } /** * Get cart items * * @return WP_REST_Response */ public static function get_cart_items() { return rest_ensure_response(array( 'items' => Wiaas_Cart::get_cart_packages(), )); } /** * @param WP_REST_Request $request Request data. * * @return WP_REST_Response */ public static function add_package_to_cart($request) { $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'); } /** * @param WP_REST_Request $request Request data. * * @return WP_REST_Response */ public static function remove_package_from_cart($request) { $success = Wiaas_Cart::remove_package_from_cart($request['key']); if ($success) { return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success'); } return wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error'); } /** * Update package quantity in cart * @return WP_REST_Response */ public static function update_package_quantity($request) { $success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']); if ($success) { return wiaas_api_notice('QUANTITY_UPDATED', 'success'); } return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error'); } /** * @param WP_REST_Request $request Request data. * * @return WP_REST_Response */ public static function checkout($request) { Wiaas_Checkout::process_checkout($request->get_body_params()); return wiaas_api_notice('ORDER_PLACED', 'success'); } }