Automattic\WooCommerce\StoreApi\Routes\V1

CheckoutOrder::get_request_payment_methodprivateWC 1.0

Gets the chosen payment method from the request.

Method of the class: CheckoutOrder{}

No Hooks.

Returns

\WC_Payment_Gateway|null.

Usage

// private - for code of main (parent) class only
$result = $this->get_request_payment_method( $request );
$request(WP_REST_Request) (required)
Request object.

CheckoutOrder::get_request_payment_method() code WC 11.0.1

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 ];
}