2020-08-28 08:13:45 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Determines whether specific gateways need to be disabled.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\WcGateway\Checkout
|
2020-08-28 08:13:45 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Checkout;
|
2020-08-28 08:13:45 +03:00
|
|
|
|
2023-06-15 10:01:05 +03:00
|
|
|
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
2022-07-19 09:20:26 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
2022-11-09 10:11:31 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
2022-12-05 17:05:19 +04:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
|
2020-08-28 08:13:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DisableGateways
|
|
|
|
*/
|
|
|
|
class DisableGateways {
|
2023-06-15 10:01:05 +03:00
|
|
|
use ContextTrait;
|
2020-08-28 08:13:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Session Handler.
|
|
|
|
*
|
|
|
|
* @var SessionHandler
|
|
|
|
*/
|
|
|
|
private $session_handler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Settings.
|
|
|
|
*
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
private $settings;
|
|
|
|
|
2022-12-05 17:05:19 +04:00
|
|
|
/**
|
|
|
|
* The Settings status helper.
|
|
|
|
*
|
|
|
|
* @var SettingsStatus
|
|
|
|
*/
|
|
|
|
protected $settings_status;
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* DisableGateways constructor.
|
|
|
|
*
|
|
|
|
* @param SessionHandler $session_handler The Session Handler.
|
|
|
|
* @param ContainerInterface $settings The Settings.
|
2022-12-05 17:05:19 +04:00
|
|
|
* @param SettingsStatus $settings_status The Settings status helper.
|
2020-08-28 08:13:45 +03:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
SessionHandler $session_handler,
|
2022-12-05 17:05:19 +04:00
|
|
|
ContainerInterface $settings,
|
|
|
|
SettingsStatus $settings_status
|
2020-08-28 08:13:45 +03:00
|
|
|
) {
|
|
|
|
|
2022-01-03 15:15:33 +01:00
|
|
|
$this->session_handler = $session_handler;
|
|
|
|
$this->settings = $settings;
|
2022-12-05 17:05:19 +04:00
|
|
|
$this->settings_status = $settings_status;
|
2020-08-28 08:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2022-07-19 09:20:26 +03:00
|
|
|
if ( $this->disable_all_gateways() ) {
|
2020-08-28 08:13:45 +03:00
|
|
|
unset( $methods[ PayPalGateway::ID ] );
|
|
|
|
unset( $methods[ CreditCardGateway::ID ] );
|
2022-07-19 09:20:26 +03:00
|
|
|
unset( $methods[ CardButtonGateway::ID ] );
|
2020-08-28 08:13:45 +03:00
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $this->settings->has( 'client_id' ) || empty( $this->settings->get( 'client_id' ) ) ) {
|
|
|
|
unset( $methods[ CreditCardGateway::ID ] );
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:51:32 +03:00
|
|
|
if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
|
|
|
|
unset( $methods[ CardButtonGateway::ID ] );
|
|
|
|
if ( ! $this->session_handler->order() && is_checkout() ) {
|
|
|
|
unset( $methods[ PayPalGateway::ID ] );
|
|
|
|
}
|
2020-09-03 11:23:11 +03:00
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
if ( ! $this->needs_to_disable_gateways() ) {
|
|
|
|
return $methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] );
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:17:59 +03:00
|
|
|
/**
|
2022-07-19 09:20:26 +03:00
|
|
|
* Whether all gateways should be disabled or not.
|
2020-09-29 10:17:59 +03:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-07-19 09:20:26 +03:00
|
|
|
private function disable_all_gateways() : bool {
|
2023-01-18 14:39:27 +01:00
|
|
|
if ( is_null( WC()->payment_gateways ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-07 15:35:12 +02:00
|
|
|
foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
|
|
|
|
if ( PayPalGateway::ID === $gateway->id && $gateway->enabled !== 'yes' ) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-29 10:17:59 +03:00
|
|
|
}
|
2022-09-07 15:35:12 +02:00
|
|
|
|
2020-09-29 10:17:59 +03:00
|
|
|
if ( ! $this->settings->has( 'merchant_email' ) || ! is_email( $this->settings->get( 'merchant_email' ) ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-28 08:13:45 +03:00
|
|
|
/**
|
|
|
|
* 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 {
|
2023-06-15 10:01:05 +03:00
|
|
|
return $this->is_paypal_continuation();
|
2020-08-28 08:13:45 +03:00
|
|
|
}
|
|
|
|
}
|