|null $input * @return array */ public static function require_verified_session( $input = null ): array { if ( ! Agentmatica_Support_Settings::is_order_lookup_enabled() ) { return array( 'success' => false, 'ok' => false, 'message' => __( 'Order lookup is disabled on this store.', 'agentmatica-mcp-connector' ), ); } $input = is_array( $input ) ? $input : array(); $session_key = Agentmatica_WooCommerce_Cart::sanitize_session_key( (string) ( $input['session_key'] ?? '' ) ); if ( $session_key === '' ) { return array( 'success' => false, 'ok' => false, 'message' => __( 'session_key is required.', 'agentmatica-mcp-connector' ), ); } $verified = Agentmatica_WooCommerce_Orders::get_verified_session( $session_key ); if ( $verified === null ) { return array( 'success' => false, 'ok' => false, 'message' => __( 'Customer is not verified for this chat session.', 'agentmatica-mcp-connector' ), ); } return $verified; } /** * @param array $verified * @return array|null */ public static function require_logged_in_session( array $verified ): ?array { $wp_user_id = isset( $verified['wp_user_id'] ) ? (int) $verified['wp_user_id'] : 0; if ( $wp_user_id > 0 ) { return null; } return array( 'success' => false, 'ok' => false, 'code' => 'login_required', 'result' => 'refused', 'message' => __( 'This action requires a logged-in store account.', 'agentmatica-mcp-connector' ), ); } /** * Cancel / return: logged-in account or order-number + email verification. * * @param array $verified * @return array|null */ public static function require_verified_customer_session( array $verified ): ?array { $wp_user_id = isset( $verified['wp_user_id'] ) ? (int) $verified['wp_user_id'] : 0; $email = strtolower( trim( (string) ( $verified['email'] ?? '' ) ) ); if ( $wp_user_id > 0 || $email !== '' ) { return null; } return array( 'success' => false, 'ok' => false, 'code' => 'login_required', 'result' => 'refused', 'message' => __( 'This action requires a verified customer for this chat session.', 'agentmatica-mcp-connector' ), ); } public static function order_owned_by_session( WC_Order $order, array $verified ): bool { $wp_user_id = isset( $verified['wp_user_id'] ) ? (int) $verified['wp_user_id'] : 0; if ( $wp_user_id > 0 ) { // Logged-in sessions own orders through the account only. WooCommerce // does not verify account emails, so matching the billing email would // let anyone register with a guest shopper's email and act on their orders. $customer_id = (int) $order->get_customer_id(); return $customer_id > 0 && $customer_id === $wp_user_id; } $email = strtolower( trim( (string) ( $verified['email'] ?? '' ) ) ); if ( $email !== '' && Agentmatica_WooCommerce_Orders::email_matches_order( $order, $email ) ) { return true; } $customer_id = isset( $verified['customer_id'] ) ? (int) $verified['customer_id'] : 0; if ( $customer_id > 0 ) { $order_customer = (int) $order->get_customer_id(); if ( $order_customer > 0 && $order_customer === $customer_id ) { return true; } } return false; } public static function cancel_eligibility( WC_Order $order ): ?string { $status = (string) $order->get_status(); if ( in_array( $status, array( 'cancelled', 'refunded', 'failed', 'trash' ), true ) ) { return __( 'This order is already cancelled or closed.', 'agentmatica-mcp-connector' ); } if ( in_array( $status, array( 'completed', 'shipped' ), true ) ) { return __( 'This order has already shipped and cannot be cancelled.', 'agentmatica-mcp-connector' ); } return null; } public static function address_update_eligibility( WC_Order $order ): ?string { $status = (string) $order->get_status(); if ( in_array( $status, array( 'completed', 'cancelled', 'refunded', 'failed', 'trash' ), true ) ) { return __( 'The shipping address can no longer be changed on this order.', 'agentmatica-mcp-connector' ); } return null; } /** * @param WC_Order $order * @param array $new_address */ public static function same_region_violation( WC_Order $order, array $new_address, bool $same_region_only ): ?string { if ( ! $same_region_only ) { return null; } $current_country = strtolower( trim( (string) $order->get_shipping_country() ) ); $current_state = strtolower( trim( (string) $order->get_shipping_state() ) ); $new_country = strtolower( trim( (string) ( $new_address['country'] ?? '' ) ) ); $new_state = strtolower( trim( (string) ( $new_address['state'] ?? '' ) ) ); if ( $new_country !== '' && $current_country !== '' && $new_country !== $current_country ) { return __( 'Country cannot be changed for this order.', 'agentmatica-mcp-connector' ); } if ( $new_state !== '' && $current_state !== '' && $new_state !== $current_state ) { return __( 'State or province cannot be changed for this order.', 'agentmatica-mcp-connector' ); } return null; } public static function return_request_eligibility( WC_Order $order ): ?string { $status = (string) $order->get_status(); if ( ! in_array( $status, array( 'completed', 'processing' ), true ) ) { return __( 'Returns can only be requested on fulfilled or completed orders.', 'agentmatica-mcp-connector' ); } return null; } /** * @param array $input */ public static function confirmed( array $input ): bool { return ! empty( $input['confirmed'] ); } }