Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Store API: Fix payment method validation (fixes COD) #9375

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/StoreApi/Routes/V1/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,14 @@ private function get_request_payment_method( \WP_REST_Request $request ) {
}

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
);
Expand Down
4 changes: 3 additions & 1 deletion src/StoreApi/Schemas/V1/CheckoutSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public function get_properties() {
'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' ),
Expand Down