Enums
7 months ago
Messages
7 months ago
AgenticCheckoutSession.php
7 months ago
CheckoutSessions.php
4 months ago
CheckoutSessionsComplete.php
4 months ago
CheckoutSessionsUpdate.php
4 months ago
Error.php
7 months ago
CheckoutSessions.php
179 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Automattic\WooCommerce\StoreApi\Routes\V1\Agentic; |
| 4 | |
| 5 | use Automattic\WooCommerce\StoreApi\Routes\V1\AbstractCartRoute; |
| 6 | use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Error; |
| 7 | use Automattic\WooCommerce\StoreApi\SchemaController; |
| 8 | use Automattic\WooCommerce\StoreApi\Schemas\V1\AbstractSchema; |
| 9 | use Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic\CheckoutSessionSchema; |
| 10 | use Automattic\WooCommerce\StoreApi\Utilities\CartController; |
| 11 | use Automattic\WooCommerce\StoreApi\Utilities\OrderController; |
| 12 | use Automattic\WooCommerce\StoreApi\Utilities\AgenticCheckoutUtils; |
| 13 | use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\AgenticCheckoutSession; |
| 14 | |
| 15 | /** |
| 16 | * CheckoutSessions class. |
| 17 | * |
| 18 | * Handles the Agentic Checkout API checkout sessions endpoint. |
| 19 | * This endpoint allows AI agents to create and manage checkout sessions. |
| 20 | */ |
| 21 | class CheckoutSessions extends AbstractCartRoute { |
| 22 | /** |
| 23 | * The route identifier. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | const IDENTIFIER = 'agentic-checkout-sessions'; |
| 28 | |
| 29 | /** |
| 30 | * The route's schema type. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | const SCHEMA_TYPE = CheckoutSessionSchema::IDENTIFIER; |
| 35 | |
| 36 | /** |
| 37 | * Cart controller for managing cart operations. |
| 38 | * |
| 39 | * @var CartController |
| 40 | */ |
| 41 | protected $cart_controller; |
| 42 | |
| 43 | /** |
| 44 | * Constructor. |
| 45 | * |
| 46 | * @param SchemaController $schema_controller Schema Controller instance. |
| 47 | * @param AbstractSchema $schema Schema class instance. |
| 48 | */ |
| 49 | public function __construct( $schema_controller, $schema ) { |
| 50 | parent::__construct( $schema_controller, $schema ); |
| 51 | $this->order_controller = new OrderController(); |
| 52 | $this->cart_controller = new CartController(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the path of this REST route. |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | public function get_path() { |
| 61 | return $this->get_path_regex(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the path regex for this REST route. |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public static function get_path_regex() { |
| 70 | return '/checkout_sessions'; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get method arguments for this REST route. |
| 75 | * |
| 76 | * @return array An array of endpoints. |
| 77 | */ |
| 78 | public function get_args() { |
| 79 | return [ |
| 80 | [ |
| 81 | 'methods' => \WP_REST_Server::CREATABLE, |
| 82 | 'callback' => [ $this, 'get_response' ], |
| 83 | 'permission_callback' => [ $this, 'is_authorized' ], |
| 84 | 'args' => $this->get_create_params(), |
| 85 | ], |
| 86 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 87 | ]; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the parameters for creating a checkout session. |
| 92 | * |
| 93 | * @return array Parameters array. |
| 94 | */ |
| 95 | protected function get_create_params() { |
| 96 | $params = AgenticCheckoutUtils::get_shared_params(); |
| 97 | $params['items'] = array_merge( |
| 98 | $params['items'], |
| 99 | [ |
| 100 | 'required' => true, |
| 101 | 'minItems' => 1, |
| 102 | ] |
| 103 | ); |
| 104 | return $params; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Check if the request is authorized. |
| 109 | * |
| 110 | * Validates that the request is signed with Jetpack blog token. |
| 111 | * |
| 112 | * @return bool|\WP_Error True if authorized, WP_Error otherwise. |
| 113 | */ |
| 114 | public function is_authorized() { |
| 115 | return AgenticCheckoutUtils::validate_jetpack_request(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if a nonce is required for the route. |
| 120 | * |
| 121 | * @param \WP_REST_Request $request Request object. |
| 122 | * @return bool False, Jetpack blog token auth used instead. |
| 123 | */ |
| 124 | protected function requires_nonce( \WP_REST_Request $request ) { |
| 125 | // Uses Jetpack blog token authentication via is_authorized(). |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Handle the request and return a valid response for this endpoint. |
| 131 | * |
| 132 | * @param \WP_REST_Request $request Request object. |
| 133 | * @return \WP_REST_Response |
| 134 | */ |
| 135 | protected function get_route_post_response( \WP_REST_Request $request ) { |
| 136 | $checkout_session = new AgenticCheckoutSession( $this->cart_controller->get_cart_instance() ); |
| 137 | |
| 138 | // Clear existing cart to start fresh for POST requests. |
| 139 | $this->cart_controller->empty_cart(); |
| 140 | |
| 141 | // Add items to cart. |
| 142 | $items = $request->get_param( 'items' ); |
| 143 | $error = AgenticCheckoutUtils::add_items_to_cart( $items, $this->cart_controller, $checkout_session->get_messages() ); |
| 144 | // Halt for critical errors. |
| 145 | if ( $error instanceof Error ) { |
| 146 | return $error->to_rest_response(); |
| 147 | } |
| 148 | |
| 149 | // Set buyer information. |
| 150 | $buyer = $request->get_param( 'buyer' ); |
| 151 | if ( $buyer ) { |
| 152 | AgenticCheckoutUtils::set_buyer_data( $buyer, WC()->customer ); |
| 153 | } |
| 154 | |
| 155 | // Set fulfillment address. |
| 156 | $address = $request->get_param( 'fulfillment_address' ); |
| 157 | if ( $address ) { |
| 158 | AgenticCheckoutUtils::set_fulfillment_address( $address, WC()->customer ); |
| 159 | } else { |
| 160 | // Clear address when not provided (POST creates fresh session). |
| 161 | AgenticCheckoutUtils::clear_fulfillment_address( WC()->customer ); |
| 162 | } |
| 163 | |
| 164 | // Calculate totals. |
| 165 | try { |
| 166 | $this->cart_controller->calculate_totals(); |
| 167 | } catch ( \Exception $e ) { |
| 168 | $message = wp_specialchars_decode( $e->getMessage(), ENT_QUOTES ); |
| 169 | return Error::processing_error( 'totals_calculation_error', $message )->to_rest_response(); |
| 170 | } |
| 171 | |
| 172 | // Build response from canonical cart schema. |
| 173 | $response = $this->schema->get_item_response( $checkout_session ); |
| 174 | |
| 175 | // Add protocol headers. |
| 176 | return AgenticCheckoutUtils::add_protocol_headers( rest_ensure_response( $response ), $request ); |
| 177 | } |
| 178 | } |
| 179 |