ethod_id( $request ) ); $context->set_payment_data( $this->get_request_payment_data( $request ) ); // Orders are made pending before attempting payment. $this->order->update_status( 'pending' ); $context->set_order( $this->order ); /** * Process payment with context. * * @hook woocommerce_rest_checkout_process_payment_with_context * * @throws Exception If there is an error taking payment, an Exception object can be thrown * with an error message. * * @param PaymentContext $context Holds context for the payment, including order ID and payment method. * @param PaymentResult $result Result object for the transaction. */ do_action_ref_array( 'woocommerce_rest_checkout_process_payment_with_context', [ $context, &$result ] ); if ( ! $result instanceof PaymentResult ) { throw new RouteException( 'woocommerce_rest_checkout_invalid_payment_result', __( 'Invalid payment result received from payment method.', 'woocommerce' ), 500 ); } return $result; } catch ( Exception $e ) { throw new RouteException( 'woocommerce_rest_checkout_process_payment_error', $e->getMessage(), 400 ); } } /** * Gets the chosen payment method ID from the request. * * @throws RouteException On error. * @param WP_REST_Request $request Request object. * @return string */ private function get_request_payment_method_id( WP_REST_Request $request ) { $payment_method_id = isset( $request['payment_method'] ) ? wc_clean( wp_unslash( $request['payment_method'] ) ) : ''; if ( empty( $payment_method_id ) ) { throw new RouteException( 'woocommerce_rest_checkout_missing_payment_method', __( 'No payment method provided.', 'woocommerce' ), 400 ); } return $payment_method_id; } /** * Gets the chosen payment method from the request. * * @throws RouteException On error. * @param WP_REST_Request $request Request object. * @return \WC_Payment_Gateway */ private function get_request_payment_method( WP_REST_Request $request ) { $payment_method_id = $this->get_request_payment_method_id( $request ); $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); if ( ! isset( $available_gateways[ $payment_method_id ] ) ) { throw new RouteException( 'woocommerce_rest_checkout_payment_method_disabled', __( 'This payment gateway is not available.', 'woocommerce' ), 400 ); } return $available_gateways[ $payment_method_id ]; } /** * Gets and formats payment request data. * * @param WP_REST_Request $request Request object. * @return array */ private function get_request_payment_data( WP_REST_Request $request ) { static $payment_data = []; if ( ! empty( $payment_data ) ) { return $payment_data; } if ( ! empty( $request['payment_data'] ) ) { foreach ( $request['payment_data'] as $data ) { $payment_data[ sanitize_key( $data['key'] ) ] = wc_clean( $data['value'] ); } } return $payment_data; } /** * Order processing relating to customer account. * * Creates a customer account as needed (based on request & store settings) and updates the order with the new customer ID. * Updates the order with user details (e.g. address). * * @internal CreateAccount class includes feature gating logic (i.e. this may not create an account depending on build). * @internal Checkout signup is feature gated to WooCommerce 4.7 and newer; Because it requires updated my-account/lost-password screen in 4.7+ for setting initial password. * * @throws RouteException API error object with error details. * @param WP_REST_Request $request Request object. */ private function process_customer( WP_REST_Request $request ) { if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '4.7', '>=' ) ) { try { $create_account = Package::container()->get( CreateAccount::class ); $create_account->from_order_request( $request ); $this->order->set_customer_id( get_current_user_id() ); $this->order->save(); } catch ( Exception $error ) { switch ( $error->getMessage() ) { case 'registration-error-invalid-email': throw new RouteException( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'woocommerce' ), 400 ); case 'registration-error-email-exists': throw new RouteException( 'registration-error-email-exists', __( 'An account is already registered with your email address. Please log in before proceeding.', 'woocommerce' ), 400 ); } } } // Persist customer address data to account. $this->order_controller->sync_customer_data_with_order( $this->order ); } }