4096 || ! CartTokenUtils::validate_cart_token( $token ) ) { return new \WP_Error( 'wacc_cart_token', 'Invalid or expired WooCommerce cart token.' ); } $payload = CartTokenUtils::get_cart_token_payload( $token ); if ( empty( $payload['user_id'] ) || $payload['iss'] !== 'store-api' || (int) $payload['exp'] <= time() ) { return new \WP_Error( 'wacc_cart_token', 'Invalid or expired WooCommerce cart token.' ); } return hash_hmac( 'sha256', (string) $payload['user_id'], wp_salt( 'auth' ) ); } public static function call( $method, $path, $token = '', $body = null ) { $allowed = array( 'GET cart', 'POST cart/add-item', 'POST cart/update-item', 'POST cart/remove-item', 'POST cart/update-customer', 'POST cart/select-shipping-rate', 'POST cart/apply-coupon', 'POST cart/remove-coupon', 'GET checkout', 'POST checkout' ); if ( ! in_array( $method . ' ' . $path, $allowed, true ) ) { return new \WP_Error( 'wacc_route', 'Unsupported WooCommerce operation.' ); } $headers = array( 'Accept' => 'application/json', 'Content-Type' => 'application/json' ); if ( $token !== '' ) { $headers['Cart-Token'] = $token; } // The URL is always constructed from this site's REST URL, never user input. // wp_remote_request permits configured local/private development origins. $response = wp_remote_request( rest_url( 'wc/store/v1/' . $path ), array( 'method' => $method, 'headers' => $headers, 'cookies' => array(), 'redirection' => 0, 'timeout' => 30, 'limit_response_size' => 2097152, 'body' => $body === null ? null : wp_json_encode( $body ), 'sslverify' => true, ) ); if ( is_wp_error( $response ) ) { return new \WP_Error( 'wacc_woocommerce_connection', 'WooCommerce did not return a confirmed response. Do not retry a payment blindly. Check the recorded checkout result or contact the merchant.' ); } $status = (int) wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $data ) || $status < 200 || $status >= 300 ) { // Retain structured native errors but never raw HTML/proxy errors. return new \WP_Error( 'wacc_woocommerce_rejected', is_array( $data ) && isset( $data['message'] ) && is_string( $data['message'] ) ? wp_strip_all_tags( $data['message'] ) : 'WooCommerce rejected the request. Check checkout configuration.', array( 'http_status' => $status ) ); } $new_token = wp_remote_retrieve_header( $response, 'cart-token' ); if ( $new_token === '' ) { $new_token = $token; } if ( $new_token !== '' && is_wp_error( self::identity( $new_token ) ) ) { return new \WP_Error( 'wacc_woocommerce_response', 'WooCommerce returned invalid cart credentials.' ); } if ( $token !== '' && self::identity( $token ) !== self::identity( $new_token ) ) { return new \WP_Error( 'wacc_cart_changed', 'WooCommerce could not load the requested private cart.' ); } $result = array( 'woocommerce' => $data, 'cart_token' => $new_token ); if ( $path !== 'checkout' ) { $result['approval_fingerprint'] = self::fingerprint( $data ); } return $result; } public static function canonical( $value ) { if ( is_array( $value ) ) { if ( array_keys( $value ) !== range( 0, count( $value ) - 1 ) ) { ksort( $value ); } foreach ( $value as $key => $item ) { $value[ $key ] = self::canonical( $item ); } } return $value; } public static function fingerprint( array $cart ) { $quote = array(); foreach ( array( 'items', 'coupons', 'totals', 'shipping_rates', 'shipping_address', 'billing_address', 'fees', 'payment_methods', 'errors', 'extensions' ) as $key ) { $quote[ $key ] = isset( $cart[ $key ] ) ? $cart[ $key ] : null; } return hash( 'sha256', wp_json_encode( self::canonical( $quote ) ) ); } public static function supports_total_guard() { $routes = rest_get_server()->get_routes(); foreach ( isset( $routes['/wc/store/v1/checkout'] ) ? $routes['/wc/store/v1/checkout'] : array() as $endpoint ) { if ( is_array( $endpoint ) && ! empty( $endpoint['methods']['POST'] ) && isset( $endpoint['args']['expected_total'] ) ) { return true; } } return false; } }