woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/Checkout/DisableGateways.php

135 lines
3.4 KiB
PHP
Raw Normal View History

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;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
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;
/**
* 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.
* @param SettingsStatus $settings_status The Settings status helper.
2020-08-28 08:13:45 +03:00
*/
public function __construct(
SessionHandler $session_handler,
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;
$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 ] );
}
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-08-28 08:13:45 +03:00
if ( ! $this->needs_to_disable_gateways() ) {
return $methods;
}
return array( PayPalGateway::ID => $methods[ PayPalGateway::ID ] );
}
/**
2022-07-19 09:20:26 +03:00
* Whether all gateways should be disabled or not.
*
* @return bool
*/
2022-07-19 09:20:26 +03:00
private function disable_all_gateways() : bool {
if ( is_null( WC()->payment_gateways ) ) {
return false;
}
foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
if ( PayPalGateway::ID === $gateway->id && $gateway->enabled !== 'yes' ) {
return true;
}
}
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
}
}