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

Commit

Permalink
Store API: Fix payment method validation (fixes COD) (#9375)
Browse files Browse the repository at this point in the history
* Widen enum for validation before cart is loaded

* Improve error message on checkout
  • Loading branch information
mikejolley authored May 17, 2023
1 parent f5f7b73 commit 1c575d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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

0 comments on commit 1c575d4

Please sign in to comment.