diff --git a/includes/class-wc-payments-apple-pay-registration.php b/includes/class-wc-payments-apple-pay-registration.php index 60072c3e6f9..a9b73c0c94b 100644 --- a/includes/class-wc-payments-apple-pay-registration.php +++ b/includes/class-wc-payments-apple-pay-registration.php @@ -129,7 +129,7 @@ public function verify_domain_on_domain_name_change() { * Vefifies if hosted domain association file is up to date * with the file from the plugin directory. * - * @return bool Wether file is up to date or not. + * @return bool Whether file is up to date or not. */ private function is_hosted_domain_association_file_up_to_date() { // Contents of domain association file from plugin dir. diff --git a/includes/class-wc-payments-payment-request-button-handler.php b/includes/class-wc-payments-payment-request-button-handler.php index da906abd5f6..e76adaf3fb2 100644 --- a/includes/class-wc-payments-payment-request-button-handler.php +++ b/includes/class-wc-payments-payment-request-button-handler.php @@ -100,6 +100,36 @@ public function init() { add_filter( 'pre_option_wcpay_is_apple_pay_enabled', [ $this, 'get_option_is_apple_pay_enabled' ], 10, 1 ); } + /** + * Checks whether authentication is required for checkout. + * + * @return bool + */ + public function is_authentication_required() { + // If guest checkout is disabled, authentication might be required. + if ( 'no' === get_option( 'woocommerce_enable_guest_checkout', 'yes' ) ) { + // If account creation is not possible, authentication is required. + return ! $this->is_account_creation_possible(); + } + + return false; + } + + /** + * Checks whether account creation is possible during checkout. + * + * @return bool + */ + public function is_account_creation_possible() { + // If automatically generate username/password are disabled, the Payment Request API + // can't include any of those fields, so account creation is not possible. + return ( + 'yes' === get_option( 'woocommerce_enable_signup_and_login_from_checkout', 'no' ) && + 'yes' === get_option( 'woocommerce_registration_generate_username', 'yes' ) && + 'yes' === get_option( 'woocommerce_registration_generate_password', 'yes' ) + ); + } + /** * Gets total label. * @@ -344,11 +374,16 @@ public function supported_product_types() { } /** - * Checks the cart to see if all items are allowed to used. + * Checks the cart to see if all items are allowed to be used. * * @return boolean */ public function allowed_items_in_cart() { + // Pre Orders compatbility where we don't support charge upon release. + if ( class_exists( 'WC_Pre_Orders_Cart' ) && WC_Pre_Orders_Cart::cart_contains_pre_order() && class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( WC_Pre_Orders_Cart::get_pre_order_product() ) ) { + return false; + } + foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); @@ -356,13 +391,13 @@ public function allowed_items_in_cart() { return false; } - // Trial subscriptions with shipping are not supported. - if ( class_exists( 'WC_Subscriptions_Order' ) && WC_Subscriptions_Cart::cart_contains_subscription() && $_product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $_product ) > 0 ) { + // Not supported for subscription products when user is not authenticated and account creation is not possible. + if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $_product ) && ! is_user_logged_in() && ! $this->is_account_creation_possible() ) { return false; } - // Pre Orders compatbility where we don't support charge upon release. - if ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Cart::cart_contains_pre_order() && WC_Pre_Orders_Product::product_is_charged_upon_release( WC_Pre_Orders_Cart::get_pre_order_product() ) ) { + // Trial subscriptions with shipping are not supported. + if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $_product ) && $_product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $_product ) > 0 ) { return false; } } @@ -530,9 +565,15 @@ public function display_payment_request_button_separator_html() { * @return boolean */ private function should_show_payment_button_on_cart() { + // Not supported when user isn't authenticated and authentication is required. + if ( ! is_user_logged_in() && $this->is_authentication_required() ) { + return false; + } + if ( ! apply_filters( 'wcpay_show_payment_request_on_cart', true ) ) { return false; } + if ( ! $this->allowed_items_in_cart() ) { Logger::log( 'Items in the cart has unsupported product type ( Payment Request button disabled )' ); return false; @@ -558,14 +599,33 @@ private function should_show_payment_button_on_product_page() { return false; } + // Not supported when user isn't authenticated and authentication is required. + if ( ! is_user_logged_in() && $this->is_authentication_required() ) { + return false; + } + + // Not supported for subscription products when user is not authenticated and account creation is not possible. + if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) && ! is_user_logged_in() && ! $this->is_account_creation_possible() ) { + return false; + } + // Trial subscriptions with shipping are not supported. - if ( class_exists( 'WC_Subscriptions_Order' ) && $product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) { + if ( class_exists( 'WC_Subscriptions_Product' ) && $product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) { return false; } // Pre Orders charge upon release not supported. - if ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( $product ) ) { - Logger::log( 'Pre Order charge upon release is not supported. ( Payment Request button disabled )' ); + if ( class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( $product ) ) { + return false; + } + + // Composite products are not supported on the product page. + if ( class_exists( 'WC_Composite_Products' ) && function_exists( 'is_composite_product' ) && is_composite_product() ) { + return false; + } + + // Mix and match products are not supported on the product page. + if ( class_exists( 'WC_Mix_and_Match' ) && $product->is_type( 'mix-and-match' ) ) { return false; }