|null $input * @return array */ public static function customer_verify( $input = null ): array { if ( ! self::is_order_lookup_enabled() ) { return array( 'success' => false, 'message' => __( 'Order lookup is disabled on this store.', 'agentmatica-mcp-connector' ), ); } if ( ! function_exists( 'WC' ) ) { return array( 'success' => false, 'message' => __( 'WooCommerce is not available.', 'agentmatica-mcp-connector' ), ); } $input = is_array( $input ) ? $input : array(); $session_key = Agentmatica_WooCommerce_Cart::sanitize_session_key( (string) ( $input['session_key'] ?? '' ) ); $order_number = sanitize_text_field( (string) ( $input['order_number'] ?? '' ) ); $email = sanitize_email( (string) ( $input['email'] ?? '' ) ); if ( $session_key === '' || $order_number === '' || $email === '' ) { return array( 'success' => false, 'message' => __( 'session_key, order_number, and email are required.', 'agentmatica-mcp-connector' ), ); } if ( ! is_email( $email ) ) { return array( 'success' => false, 'message' => __( 'A valid email address is required.', 'agentmatica-mcp-connector' ), ); } if ( self::is_rate_limited( $session_key ) || self::is_order_rate_limited( $order_number ) ) { return array( 'success' => false, 'message' => __( 'Too many verification attempts. Please try again later.', 'agentmatica-mcp-connector' ), ); } self::record_attempt( $session_key ); self::record_order_attempt( $order_number ); $order = self::find_order_by_number( $order_number ); if ( ! $order instanceof WC_Order ) { return array( 'success' => false, 'message' => __( 'No order found with that order number.', 'agentmatica-mcp-connector' ), ); } if ( ! self::email_matches_order( $order, $email ) ) { return array( 'success' => false, 'message' => __( 'Order number and email do not match our records.', 'agentmatica-mcp-connector' ), ); } $customer_id = (int) $order->get_customer_id(); self::bind_verified_session( $session_key, array( 'customer_id' => $customer_id, 'email' => strtolower( trim( $email ) ), 'order_id' => (int) $order->get_id(), ) ); return array( 'success' => true, 'message' => __( 'Customer verified for this chat session.', 'agentmatica-mcp-connector' ), 'customer' => array( 'id' => $customer_id > 0 ? $customer_id : null, 'email' => strtolower( trim( $email ) ), 'name' => trim( $order->get_formatted_billing_full_name() ), ), ); } /** * @param array|null $input * @return array */ public static function orders_list( $input = null ): array { $verified = self::require_verified_session( $input ); if ( isset( $verified['success'] ) && $verified['success'] === false ) { return $verified; } $wp_user_id = isset( $verified['wp_user_id'] ) ? (int) $verified['wp_user_id'] : 0; $email = (string) ( $verified['email'] ?? '' ); if ( $wp_user_id <= 0 && $email === '' ) { return array( 'success' => false, 'message' => __( 'Verified email is missing.', 'agentmatica-mcp-connector' ), ); } $query = array( 'limit' => 5, 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ); if ( $wp_user_id > 0 ) { $query['customer_id'] = $wp_user_id; } else { $query['billing_email'] = $email; } $orders = wc_get_orders( $query ); return array( 'success' => true, 'orders' => array_map( static function ( WC_Order $order ): array { return self::format_order_summary( $order ); }, is_array( $orders ) ? $orders : array() ), ); } /** * @param array|null $input * @return array */ public static function order_get( $input = null ): array { $verified = self::require_verified_session( $input ); if ( isset( $verified['success'] ) && $verified['success'] === false ) { return $verified; } $input = is_array( $input ) ? $input : array(); $order_number = sanitize_text_field( (string) ( $input['order_number'] ?? '' ) ); if ( $order_number === '' ) { return array( 'success' => false, 'message' => __( 'order_number is required.', 'agentmatica-mcp-connector' ), ); } $order = self::find_order_by_number( $order_number ); if ( ! $order instanceof WC_Order ) { return array( 'success' => false, 'message' => __( 'Order not found.', 'agentmatica-mcp-connector' ), ); } if ( ! Agentmatica_WooCommerce_Order_Guards::order_owned_by_session( $order, $verified ) ) { return array( 'success' => false, 'message' => __( 'That order is not linked to the verified customer.', 'agentmatica-mcp-connector' ), ); } return array( 'success' => true, 'order' => self::format_order_detail( $order ), ); } /** * @param array|null $input * @return array */ public static function bind_logged_in_customer( $input = null ): array { if ( ! self::is_order_lookup_enabled() ) { return array( 'success' => 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'] ?? '' ) ); $wp_user_id = isset( $input['wp_user_id'] ) ? (int) $input['wp_user_id'] : 0; if ( $session_key === '' || $wp_user_id <= 0 ) { return array( 'success' => false, 'message' => __( 'session_key and wp_user_id are required.', 'agentmatica-mcp-connector' ), ); } $user = get_user_by( 'id', $wp_user_id ); if ( ! $user instanceof WP_User ) { return array( 'success' => false, 'message' => __( 'Customer account not found.', 'agentmatica-mcp-connector' ), ); } $sessions = get_user_meta( $wp_user_id, self::USER_SESSIONS_META, true ); $sessions = is_array( $sessions ) ? $sessions : array(); $sessions = array_slice( array_values( array_unique( array_merge( array( $session_key ), $sessions ) ) ), 0, 20 ); update_user_meta( $wp_user_id, self::USER_SESSIONS_META, $sessions ); self::bind_verified_session( $session_key, array( 'customer_id' => $wp_user_id, 'email' => strtolower( trim( (string) $user->user_email ) ), 'wp_user_id' => $wp_user_id, ) ); return array( 'success' => true, 'customer' => array( 'id' => $wp_user_id, 'email' => strtolower( trim( (string) $user->user_email ) ), 'name' => trim( $user->display_name ), ), ); } /** * @param array|null $input * @return array */ private static function require_verified_session( $input ): array { if ( ! self::is_order_lookup_enabled() ) { return array( 'success' => 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, 'message' => __( 'session_key is required.', 'agentmatica-mcp-connector' ), ); } $verified = self::get_verified_session( $session_key ); if ( $verified === null ) { return array( 'success' => false, 'message' => __( 'Customer is not verified for this chat session.', 'agentmatica-mcp-connector' ), ); } return $verified; } /** * @param array $payload */ private static function bind_verified_session( string $session_key, array $payload ): void { set_transient( self::VERIFY_TRANSIENT_PREFIX . $session_key, $payload, self::VERIFY_TTL ); } /** * @return array|null */ public static function get_verified_session( string $session_key ): ?array { $data = get_transient( self::VERIFY_TRANSIENT_PREFIX . $session_key ); return is_array( $data ) ? $data : null; } private static function is_rate_limited( string $session_key ): bool { $session_count = (int) get_transient( self::RATE_SESSION_PREFIX . $session_key ); if ( $session_count >= self::MAX_ATTEMPTS_SESSION ) { return true; } $ip = self::client_ip(); if ( $ip !== '' ) { $ip_count = (int) get_transient( self::RATE_IP_PREFIX . md5( $ip ) ); if ( $ip_count >= self::MAX_ATTEMPTS_IP ) { return true; } } return false; } private static function record_attempt( string $session_key ): void { $session_key_id = self::RATE_SESSION_PREFIX . $session_key; $session_count = (int) get_transient( $session_key_id ); set_transient( $session_key_id, $session_count + 1, self::RATE_WINDOW ); $ip = self::client_ip(); if ( $ip !== '' ) { $ip_key = self::RATE_IP_PREFIX . md5( $ip ); $ip_count = (int) get_transient( $ip_key ); set_transient( $ip_key, $ip_count + 1, HOUR_IN_SECONDS ); } } /** * Attempts per order number, across all chat sessions. Requests come from the * Agentmatica server, so REMOTE_ADDR cannot tell shoppers apart and a new * session_key resets the per-session count; this caps guessing on one order. */ private static function is_order_rate_limited( string $order_number ): bool { $key = self::RATE_ORDER_PREFIX . md5( strtolower( ltrim( trim( $order_number ), '#' ) ) ); return (int) get_transient( $key ) >= self::MAX_ATTEMPTS_ORDER; } private static function record_order_attempt( string $order_number ): void { $key = self::RATE_ORDER_PREFIX . md5( strtolower( ltrim( trim( $order_number ), '#' ) ) ); set_transient( $key, (int) get_transient( $key ) + 1, HOUR_IN_SECONDS ); } private static function client_ip(): string { $remote = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( (string) $_SERVER['REMOTE_ADDR'] ) ) : ''; return $remote; } public static function find_order_by_number( string $order_number ): ?WC_Order { $order_number = ltrim( trim( $order_number ), '#' ); if ( $order_number === '' ) { return null; } if ( ctype_digit( $order_number ) ) { $order = wc_get_order( (int) $order_number ); if ( $order instanceof WC_Order ) { return $order; } } // Sequential-order-number plugins store the visible number in _order_number; a // single-row lookup by that key is the only way to resolve it back to an order. // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value $orders = wc_get_orders( array( 'limit' => 1, 'meta_key' => '_order_number', 'meta_value' => $order_number, 'return' => 'objects', ) ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value if ( is_array( $orders ) && isset( $orders[0] ) && $orders[0] instanceof WC_Order ) { return $orders[0]; } return null; } public static function email_matches_order( WC_Order $order, string $email ): bool { $expected = strtolower( trim( $email ) ); $billing = strtolower( trim( (string) $order->get_billing_email() ) ); return $expected !== '' && hash_equals( $billing, $expected ); } /** * @return array */ private static function format_order_summary( WC_Order $order ): array { return array( 'id' => (int) $order->get_id(), 'number' => (string) $order->get_order_number(), 'status' => (string) $order->get_status(), 'date' => $order->get_date_created() ? $order->get_date_created()->date( DATE_ATOM ) : null, 'total' => (string) $order->get_total(), 'currency' => (string) $order->get_currency(), 'tracking' => self::extract_tracking( $order ), ); } /** * @return array */ private static function format_order_detail( WC_Order $order ): array { $items = array(); foreach ( $order->get_items() as $item ) { if ( ! $item instanceof WC_Order_Item_Product ) { continue; } $items[] = array( 'name' => $item->get_name(), 'quantity' => (int) $item->get_quantity(), 'total' => (string) $item->get_total(), ); } return array( 'id' => (int) $order->get_id(), 'number' => (string) $order->get_order_number(), 'status' => (string) $order->get_status(), 'date' => $order->get_date_created() ? $order->get_date_created()->date( DATE_ATOM ) : null, 'total' => (string) $order->get_total(), 'currency' => (string) $order->get_currency(), 'shipping_method' => $order->get_shipping_method(), 'tracking' => self::extract_tracking( $order ), 'line_items' => $items, ); } /** * @return list> */ private static function extract_tracking( WC_Order $order ): array { $tracking = array(); if ( function_exists( 'wc_get_order_tracking_items' ) ) { $items = wc_get_order_tracking_items( $order->get_id() ); if ( is_array( $items ) ) { foreach ( $items as $item ) { if ( ! is_array( $item ) ) { continue; } $tracking[] = array( 'provider' => isset( $item['tracking_provider'] ) ? (string) $item['tracking_provider'] : null, 'number' => isset( $item['tracking_number'] ) ? (string) $item['tracking_number'] : null, 'url' => isset( $item['tracking_link'] ) ? (string) $item['tracking_link'] : null, ); } } } return $tracking; } }