cart ) { wc_load_cart(); } nocache_headers(); header( 'Referrer-Policy: no-referrer' ); header( "Content-Security-Policy: default-src 'none'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'" ); $method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET'; $source = 'POST' === $method ? wp_unslash( $_POST ) : wp_unslash( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended -- ticket is validated below; POST also requires session nonce. $ticket = isset( $source['ticket'] ) ? sanitize_text_field( $source['ticket'] ) : ''; if ( ! preg_match( '/^[a-f0-9]{64}$/D', $ticket ) ) { wp_die( 'Invalid cart transfer.', '', array( 'response' => 400 ) ); } if ( 'GET' === $method ) { WC()->session->set_customer_session_cookie( true ); $nonce = bin2hex( random_bytes( 32 ) ); WC()->session->set( 'selectika_agent_transfer_nonce', $nonce ); status_header( 200 ); echo '
Confirm to add the selected items to your existing cart. We will check prices and stock first.
'; exit; } if ( 'POST' !== $method ) { wp_die( 'Method not allowed.', '', array( 'response' => 405 ) ); } $expected = WC()->session->get( 'selectika_agent_transfer_nonce' ); $raw_nonce = isset( $_POST['transfer_nonce'] ) ? wp_unslash( $_POST['transfer_nonce'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- custom one-time session nonce verified with hash_equals below. $nonce = sanitize_text_field( $raw_nonce ); if ( ! is_string( $expected ) || ! hash_equals( $expected, $nonce ) ) { wp_die( 'Transfer confirmation expired.', '', array( 'response' => 403 ) ); } $lock = 'selectika_agent_transfer_' . hash( 'sha256', WC()->session->get_customer_id() ); if ( ! add_option( $lock, time(), '', false ) ) { wp_die( 'Another cart transfer needs to finish before continuing.', '', array( 'response' => 409 ) ); } WC()->session->__unset( 'selectika_agent_transfer_nonce' ); try { $origin = rtrim( SELECTIKA_AGENT_MCP_ORIGIN, '/' ); if ( 'https' !== wp_parse_url( $origin, PHP_URL_SCHEME ) || wp_parse_url( $origin, PHP_URL_USER ) || wp_parse_url( $origin, PHP_URL_QUERY ) ) { throw new RuntimeException( 'Invalid commerce service configuration.' ); } $response = wp_safe_remote_post( $origin . '/retailers/' . rawurlencode( SELECTIKA_AGENT_RETAILER ) . '/handoffs/redeem', array( 'timeout' => 15, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Bearer ' . SELECTIKA_AGENT_TOKEN, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'ticket' => $ticket ) ), ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { throw new RuntimeException( 'Transfer unavailable or already used. Do not retry this link.' ); } $payload = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $payload ) || SELECTIKA_AGENT_RETAILER !== ( $payload['retailer'] ?? '' ) || empty( $payload['lines'] ) || count( $payload['lines'] ) > 50 ) { throw new RuntimeException( 'Invalid cart transfer.' ); } $validated = array(); foreach ( $payload['lines'] as $line ) { if ( ! preg_match( '/^[1-9][0-9]*$/D', (string) ( $line['variantId'] ?? '' ) ) || ! is_int( $line['quantity'] ) || $line['quantity'] < 1 || $line['quantity'] > 99 ) { throw new RuntimeException( 'Invalid variant.' ); } $id = (int) $line['variantId']; $product = wc_get_product( $id ); if ( ! $product || ! $product->is_purchasable() || ! $product->is_in_stock() || ! in_array( $product->get_type(), array( 'simple', 'variation' ), true ) ) { throw new RuntimeException( 'An item is no longer available. Review the cart with your assistant.' ); } $existing = 0; foreach ( WC()->cart->get_cart() as $item ) { $cart_id = (int) ( $item['variation_id'] ? $item['variation_id'] : $item['product_id'] ); if ( $cart_id === $id ) { $existing += $item['quantity']; } } if ( ! $product->has_enough_stock( $existing + $line['quantity'] ) || ( $product->is_sold_individually() && $existing + $line['quantity'] > 1 ) || get_woocommerce_currency() !== $line['currency'] || wc_format_decimal( $product->get_price(), wc_get_price_decimals() ) !== wc_format_decimal( $line['price'], wc_get_price_decimals() ) ) { throw new RuntimeException( 'Price or stock changed. Review the cart with your assistant before transferring again.' ); } $validated[] = array( $product, $line['quantity'] ); } foreach ( $validated as $entry ) { list( $product, $quantity ) = $entry; $variation = $product->is_type( 'variation' ); $result = WC()->cart->add_to_cart( $variation ? $product->get_parent_id() : $product->get_id(), $quantity, $variation ? $product->get_id() : 0, $variation ? $product->get_variation_attributes() : array() ); if ( ! $result ) { throw new RuntimeException( 'Transfer was only partly completed. Inspect your store cart; do not repeat this link.' ); } } WC()->cart->calculate_totals(); WC()->cart->set_session(); delete_option( $lock ); wp_safe_redirect( wc_get_cart_url(), 303 ); exit; } catch ( Throwable $error ) { WC()->cart->set_session(); delete_option( $lock ); wp_die( esc_html( $error->getMessage() ), 'Cart transfer needs attention', array( 'response' => 409 ) ); } } }