session_handler = $session_handler; $this->settings = $settings; } /** * Controls the logic for enabling/disabling gateways. * * @param array $methods The Gateways. * * @return array */ public function handler( array $methods ): array { if ( ! isset( $methods[ PayPalGateway::ID ] ) && ! isset( $methods[ CreditCardGateway::ID ] ) ) { return $methods; } if ( ! $this->settings->has( 'enabled' ) || ! $this->settings->get( 'enabled' ) ) { unset( $methods[ PayPalGateway::ID ] ); unset( $methods[ CreditCardGateway::ID ] ); return $methods; } if ( ! $this->settings->has( 'merchant_email' ) || ! is_email( $this->settings->get( 'merchant_email' ) ) ) { unset( $methods[ PayPalGateway::ID ] ); unset( $methods[ CreditCardGateway::ID ] ); return $methods; } if ( ! $this->settings->has( 'client_id' ) || empty( $this->settings->get( 'client_id' ) ) ) { unset( $methods[ CreditCardGateway::ID ] ); } if ( $this->settings->has( 'button_enabled' ) && ! $this->settings->get( 'button_enabled' ) && ! $this->session_handler->order() ) { unset( $methods[ PayPalGateway::ID ] ); } if ( ! $this->needs_to_disable_gateways() ) { return $methods; } if ( $this->is_credit_card() ) { return array( CreditCardGateway::ID => $methods[ CreditCardGateway::ID ] ); } return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] ); } /** * Whether the Gateways need to be disabled. When we come to the checkout with a running PayPal * session, we need to disable the other Gateways, so the customer can smoothly sail through the * process. * * @return bool */ private function needs_to_disable_gateways(): bool { return $this->session_handler->order() !== null; } /** * Whether the current PayPal session is done via DCC payment. * * @return bool */ private function is_credit_card(): bool { $order = $this->session_handler->order(); if ( ! $order ) { return false; } if ( ! $order->payment_source() || ! $order->payment_source()->card() ) { return false; } return true; } }