Store API: Fix payment method validation (fixes COD) (https://github.com/woocommerce/woocommerce-blocks/pull/9375)

* Widen enum for validation before cart is loaded

* Improve error message on checkout
This commit is contained in:
Mike Jolley 2023-05-17 11:17:24 +01:00 committed by GitHub
parent 1b71b06b33
commit bb1f578002
2 changed files with 7 additions and 3 deletions

View File

@ -617,12 +617,14 @@ class Checkout extends AbstractCartRoute {
}
if ( ! isset( $available_gateways[ $request_payment_method ] ) ) {
$all_payment_gateways = WC()->payment_gateways->payment_gateways();
$gateway_title = isset( $all_payment_gateways[ $request_payment_method ] ) ? $all_payment_gateways[ $request_payment_method ]->get_title() : $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.', 'woo-gutenberg-products-block' ),
esc_html( $request_payment_method )
__( '%s is not available for this order—please choose a different payment method', 'woo-gutenberg-products-block' ),
esc_html( $gateway_title )
),
400
);

View File

@ -119,7 +119,9 @@ class CheckoutSchema extends AbstractSchema {
'description' => __( 'The ID of the payment method being used to process the payment.', 'woo-gutenberg-products-block' ),
'type' => 'string',
'context' => [ 'view', 'edit' ],
'enum' => array_values( wp_list_pluck( WC()->payment_gateways->get_available_payment_gateways(), 'id' ) ),
// Validation may be based on cart contents which is not available here; this returns all enabled
// gateways. Further validation occurs during the request.
'enum' => array_values( WC()->payment_gateways->get_payment_gateway_ids() ),
],
'create_account' => [
'description' => __( 'Whether to create a new user account as part of order processing.', 'woo-gutenberg-products-block' ),