← Back
Editing: CheckoutOrder.php
<?php namespace Automattic\WooCommerce\StoreApi\Routes\V1; use Automattic\WooCommerce\Enums\OrderStatus; use Automattic\WooCommerce\StoreApi\Payments\PaymentResult; use Automattic\WooCommerce\StoreApi\Exceptions\InvalidStockLevelsInCartException; use Automattic\WooCommerce\StoreApi\Exceptions\RouteException; use Automattic\WooCommerce\StoreApi\Utilities\OrderAuthorizationTrait; use Automattic\WooCommerce\StoreApi\Utilities\CheckoutTrait; /** * CheckoutOrder class. */ class CheckoutOrder extends AbstractCartRoute { use OrderAuthorizationTrait; use CheckoutTrait; /** * The route identifier. * * @var string */ const IDENTIFIER = 'checkout-order'; /** * The routes schema. * * @var string */ const SCHEMA_TYPE = 'checkout-order'; /** * Holds the current order being processed. * * @var \WC_Order */ private $order = null; /** * Get the path of this REST route. * * @return string */ public function get_path() { return self::get_path_regex(); } /** * Get the path of this rest route. * * @return string */ public static function get_path_regex() { return '/checkout/(?P<id>[\d]+)'; } /** * Get method arguments for this REST route. * * @return array An array of endpoints. */ public function get_args() { return [ [ 'methods' => \WP_REST_Server::CREATABLE, 'callback' => [ $this, 'get_response' ], 'permission_callback' => [ $this, 'is_authorized' ], 'args' => array_merge( [ 'payment_data' => [ 'description' => __( 'Data to pass through to the payment method when processing payment.', 'woocommerce' ), 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => [ 'key' => [ 'type' => 'string', ], 'value' => [ 'type' => [ 'string', 'boolean' ], ], ], ], ], ], $this->schema->get_endpoint_args_for_item_schema( \WP_REST_Server::CREATABLE ) ), ], 'schema' => [ $this->schema, 'get_public_item_schema' ], 'allow_batch' => [ 'v1' => true ], ]; } /** * Process an order as a request to its order-pay endpoint. * * Gateways decide whether they can pay an order from the order-pay endpoint, e.g. the shipping * method restrictions of offline gateways, and fall back to the cart otherwise. This request * pays an order and not the cart, so the endpoint is set for the order while it is processed. * It is restored afterwards because batched requests share it. * * @throws RouteException On error. * @throws InvalidStockLevelsInCartException On error. * * @param \WP_REST_Request $request Request object. * * @return \WP_REST_Response */ protected function get_route_post_response( \WP_REST_Request $request ) { global $wp, $wp_query; $order_id = absint( $request['id'] ); $wp_query_vars = $wp->query_vars; $had_query_var = isset( $wp_query->query_vars['order-pay'] ); $old_query_var = $had_query_var ? $wp_query->query_vars['order-pay'] : null; // is_wc_endpoint_url() looks the endpoint up under its configured slug, get_query_var() under 'order-pay'. $wp->query_vars[ WC()->query->get_query_vars()['order-pay'] ?? 'order-pay' ] = $order_id; $wp->query_vars['order-pay'] = $order_id; set_query_var( 'order-pay', $order_id ); try { return $this->pay_for_order( $request ); } finally { $wp->query_vars = $wp_query_vars; if ( $had_query_var ) { set_query_var( 'order-pay', $old_query_var ); } else { unset( $wp_query->query_vars['order-pay'] ); } } } /** * Process an order. * * 1. Process Request * 2. Process Customer * 3. Validate Order * 4. Process Payment * * @throws RouteException On error. * @throws InvalidStockLevelsInCartException On error. * * @param \WP_REST_Request $request Request object. * * @return \WP_REST_Response */ private function pay_for_order( \WP_REST_Request $request ) { $order_id = absint( $request['id'] ); $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order || ! $order->needs_payment() || $order->has_status( OrderStatus::CHECKOUT_DRAFT ) ) { return new \WP_Error( 'invalid_order_update_status', __( 'This order cannot be paid for.', 'woocommerce' ), array( 'status' => 400 ) ); } $this->order = $order; /** * Process request data. * * The order address is validated before anything is persisted, so a rejected request * cannot mutate the existing order or the customer record. */ $this->update_billing_address( $request ); $this->update_order_from_request( $request ); /** * Process customer data. * * Update order with customer details, and sign up a user account as necessary. */ $this->process_customer( $request ); /** * Validate order. * * This logic ensures the order is valid before payment is attempted. */ $this->order_controller->validate_existing_order_before_payment( $this->order ); /** * Fires after the Checkout Block/Store API request has populated and validated the order. * * The action runs before payment is processed, so callbacks can still act on the order * on its way to the gateway. Do not use this action for payment-completion logic or to * call WC_Order::payment_complete(). Use woocommerce_payment_complete or * woocommerce_order_status_completed instead. * * This is similar to existing core hook woocommerce_checkout_order_processed. We're using a new action: * - To keep the interface focused (only pass $order, not passing request data). * - This also explicitly indicates these orders are from checkout block/StoreAPI. * * @since 7.2.0 * * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/pull/3238 * @example docs/examples/checkout-order-processed.md * * @param \WC_Order $order Order object. */ do_action( 'woocommerce_store_api_checkout_order_processed', $this->order ); /** * Process the payment and return the results. */ $payment_result = new PaymentResult(); $needs_payment = $this->order->needs_payment(); // Callbacks on the action above, or on the needs_payment() filters, may have saved the order. $this->validate_order_was_saved(); if ( $needs_payment ) { $this->process_payment( $request, $payment_result ); } else { $this->process_without_payment( $request, $payment_result ); } return $this->prepare_item_for_response( (object) [ 'order' => wc_get_order( $this->order ), 'payment_result' => $payment_result, ], $request ); } /** * Since this endpoint only operates on existing orders, we don't need to do updates based on * the cart data. * * @param \WP_REST_Request $request Request object. */ protected function cart_updated( \WP_REST_Request $request ) {} /** * Applies the billing and shipping address from the request to the order and customer. * * The address is set on the order and validated before anything is persisted, so a rejected * request cannot mutate the order or the customer. wc()->customer is saved on shutdown (see * WooCommerce::initialize_cart()), so its address fields are only set once validation passes. * * @throws RouteException When the order address fails validation. * * @param \WP_REST_Request $request Full details about the request. */ private function update_billing_address( \WP_REST_Request $request ) { $customer = wc()->customer; // Billing address is a required field. $billing = $request['billing_address']; $shipping = $this->order_controller->update_existing_order_addresses( $this->order, $billing, $request['shipping_address'] ); // Update customer object with validated order addresses. foreach ( $billing as $key => $value ) { if ( is_callable( [ $customer, "set_billing_$key" ] ) ) { $customer->{"set_billing_$key"}( $value ); } } // When the order keeps its own shipping address, the customer's shipping follows billing. foreach ( $shipping ?? $billing as $key => $value ) { if ( is_callable( [ $customer, "set_shipping_$key" ] ) ) { $customer->{"set_shipping_$key"}( $value ); } } /** * Fires when the Checkout Block/Store API updates a customer from the API request data. * * @since 8.2.0 * * @param \WC_Customer $customer Customer object. * @param \WP_REST_Request $request Full details about the request. */ do_action( 'woocommerce_store_api_checkout_update_customer_from_request', $customer, $request ); $customer->save(); $this->order->save(); $this->order->calculate_totals(); $this->validate_order_was_saved(); } /** * Gets the chosen payment method from the request. * * @throws RouteException On error. * @param \WP_REST_Request $request Request object. * @return \WC_Payment_Gateway|null */ private function get_request_payment_method( \WP_REST_Request $request ) { $request_payment_method = wc_clean( wp_unslash( $request['payment_method'] ?? '' ) ); if ( empty( $request_payment_method ) ) { if ( $this->order->needs_payment() ) { throw new RouteException( 'woocommerce_rest_checkout_missing_payment_method', __( 'No payment method provided.', 'woocommerce' ), 400 ); } return null; } $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); if ( ! isset( $available_gateways[ $request_payment_method ] ) ) { throw new RouteException( 'woocommerce_rest_checkout_payment_method_disabled', sprintf( // Translators: %s Payment method ID. __( 'The %s payment gateway is not available.', 'woocommerce' ), esc_html( $request_payment_method ) ), 400 ); } return $available_gateways[ $request_payment_method ]; } /** * Updates the order with user details (e.g. address). * * @throws RouteException API error object with error details. * @param \WP_REST_Request $request Request object. */ private function process_customer( \WP_REST_Request $request ) { $this->order_controller->sync_customer_data_with_order( $this->order ); } }
Save File
Cancel